SpriteKIt - 动态对象与墙壁碰撞并走向低谷

时间:2014-05-07 15:48:49

标签: objective-c ios7 sprite-kit

我在简单的测试游戏上工作。我有与墙碰撞的动态物体。如果动态物体的速度很高(我认为这就是原因)物体穿过墙壁。有谁知道如何解决这个问题?

墙:

// Create wall ...
    CGRect rect = CGRectMake(0, 0, 25, self.frame.size.height);
    SKNode *wallNode = [SKNode node];
    wallNode.position = CGPointMake(CGRectGetMidX(self.frame) + rect.size.width * 0.5,                                       250);
    wallNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rect.size];
    wallNode.physicsBody.dynamic = NO;
    wallNode.physicsBody.categoryBitMask = CollisionTypeNet;
    [self addChild:wallNode];

玩家:

SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"player"];

player.name = playerCategoryName;

player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.frame.size];
player.physicsBody.restitution = 0.1f;
player.physicsBody.friction = 0.0f;
player.physicsBody.dynamic = YES;

player.physicsBody.categoryBitMask = CollisionTypePlayer;
player.physicsBody.collisionBitMask = CollisionTypeNet ;
player.physicsBody.contactTestBitMask = CollisionTypeWall;
[self addChild:player];

1 个答案:

答案 0 :(得分:0)

高速"通过"是一个常见的问题。正如Cocos所说,为你的玩家对象设置usesPreciseCollisionDetection为真。

您还应该为速度更快的物体应用速度上限,因为usesPreciseCollisionDetection无法保证能够在速度非常快的物体上工作。

你可以使用这样的速度上限:

if(mySpriteA.physicsBody.velocity.dx > 50)
    mySpriteA.physicsBody.velocity = CGVectorMake(50, mySpriteA.physicsBody.velocity.dy);
if(mySpriteA.physicsBody.velocity.dx < -50)
    mySpriteA.physicsBody.velocity = CGVectorMake(-50, mySpriteA.physicsBody.velocity.dy);
if(mySpriteA.physicsBody.velocity.dy > 50)
    mySpriteA.physicsBody.velocity = CGVectorMake(mySpriteA.physicsBody.velocity.dx, 50);
if(mySpriteA.physicsBody.velocity.dy < -50)
    mySpriteA.physicsBody.velocity = CGVectorMake(mySpriteA.physicsBody.velocity.dx, -50);