How to make one body stick to another moving object in SpriteKit

て烟熏妆下的殇ゞ 提交于 2019-12-13 03:43:39

问题


I have an object moving along the X-axis, while the other body rests. After a collision is made, I wish the resting object move along with the moving object.

I use this code to move the first body:

    SKAction* moveBlock = [SKAction moveByX:-distanceToMove y:0 duration:0.01 * distanceToMove];
    SKAction* removeBlock = [SKAction removeFromParent];
    _moveBlocksAndRemove = [SKAction sequence:@[moveBlock, removeBlock]];

and in the collision detection I have tried to set a friction:

if (firstBody.categoryBitMask == categoryA && secondBody.categoryBitMask == categoryB) {
            firstBody.friction = 1.0;
            secondBody.friction = 1.0;
}

But that didn't work. applying velocity of the first body won't work since its velocity = 0;

Any ideas?


回答1:


I haven't tried that but it seems you want to add SKPhysicsJoint to the scene:

if (firstBody.categoryBitMask == categoryA && 
                 secondBody.categoryBitMask == categoryB) {
    SKPhysicsJointFixed* pin =
                    [SKPhysicsJointFixed jointWithBodyA:firstBody 
                                                  bodyB:secondBody 
                                                 anchor:firstBody.position];
    [self.physicsWorld addJoint:pin];
}


来源:https://stackoverflow.com/questions/22083604/how-to-make-one-body-stick-to-another-moving-object-in-spritekit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!