SkSpriteNodes
,它们从屏幕的两侧移动。
我为这两个球实现了碰撞检测
。但是我注意到,在添加碰撞检测之前,球可以平稳地移动而没有任何晃动效果,但是在添加碰撞检测之后,球会晃动并沿其路径移动。
对于这两个球,我使用相同的位类别。
static const int ballHitCategory = 1;
不抖动的:
有抖动的:
这是我在代码中尝试的内容:
_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的情况下添加碰撞检测的正确方法?