添加SKPhysicsBody会导致SKSpriteNode抖动

时间:2015-11-30 18:27:49

标签: ios objective-c sprite-kit skspritenode skphysicsbody

我有两个SkSpriteNodes名为left-ballright-ball。来自屏幕两侧的流量。

我为这两个球实施了collision detection。然而我注意到在添加碰撞检测之前,球在没有任何振动效果的情况下平稳移动,但在我添加碰撞检测后,球在他们的路径中摇晃并移动。

我对这两个球使用相同的位类别:

   static const int ballHitCategory = 1;

不动摇:

enter image description here

摇晃:

enter image description here

以下是我在代码中尝试的内容:

_leftBall.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:_leftBall.frame.size];
_leftBall.physicsBody.categoryBitMask=ballHitCategory;
_leftBall.physicsBody.contactTestBitMask=ballHitCategory;
_leftBall.physicsBody.collisionBitMask=ballHitCategory;
_leftBall.physicsBody.dynamic=YES;
_leftBall.physicsBody.usesPreciseCollisionDetection=YES;

 _rightBall.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:_rightBall.frame.size];
_rightBall.physicsBody.categoryBitMask=ballHitCategory;
_rightBall.physicsBody.contactTestBitMask=ballHitCategory;
_rightBall.physicsBody.collisionBitMask=ballHitCategory;
_rightBall.physicsBody.dynamic=YES;
_rightBall.physicsBody.usesPreciseCollisionDetection=YES;




-(void)didBeginContact:(SKPhysicsContact *)contact
{

    _leftBall = (SKSpriteNode*)contact.bodyA.node;
    _rightBall = (SKSpriteNode *)contact.bodyB.node;

    if(_leftBall.physicsBody .categoryBitMask == ballHitCategory ||  _rightBall.physicsBody.categoryBitMask == ballHitCategory)
    {

        NSLog(@" hit the ");
        //setup your methods and other things here
        [_leftBall removeFromParent];
        [_rightBall removeFromParent];


    }
}

请注意,只有在我添加了碰撞检测后才会发生抖动。如果我删除上面的代码,一切都很好。

在不影响skspritenode的情况下添加碰撞检测的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

所以你使用非物理代码移动精灵......但它们仍然是物理模拟的一部分。我猜他们正在反抗引力。

将每个精灵上的affectedByGravity设置为false

或者你可以完全关闭游戏中的重力

[self.physicsWorld setGravity:CGVectorMake(0, 0)];

答案 1 :(得分:0)

不要担心重力。你应该设置你的spritenode:

_leftBall.physicsBody.dynamic=YES;
_leftBall.physicsBody.affectedByGravity=NO;

_rightBall上的相同