SpriteKit弹跳对象

时间:2014-11-06 03:17:01

标签: xcode sprite-kit

我有一个物体,我试图在水平地面上反弹,该地面从屏幕底部的某个距离升高。如何让物体沿着这个地面上下稳定地反弹?我有:

self.physicsWorld.gravity = CGVectorMake(0.0, -5.0);
object.physicsBody.restitution = 1.0;
object.physicsBody.linearDamping = 0.0;

然后在接触方法中,每当我的弹跳物体因重力而坠落并与我的地面发生碰撞时,我都会施加冲动:

[object.physicsBody applyImpulse:CGVectorMake(0.0, 10.0)];

我的问题是这种方法是零星的。重力的影响需要时间来增加每个时间间隔的加速度。物体开始弹跳超高,随着时间的推移,它甚至不会反弹。有没有人有更好的方法可以在不损失能量的情况下上下流动反弹物体?

1 个答案:

答案 0 :(得分:0)

-(void)floor
{
    SKSpriteNode *floor = [SKSpriteNode spriteNodeWithImageNamed:@"floor"];
    floor.position = yourPosition;
    floor.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:floor.frame];
    floor.physicsBody.dynamic = NO;
}

-(void)ball
{
    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
    ball.position = yourPosition;
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];
    ball.physicsBody.dynamic = YES;
    ball.physicsBody.restitution = 0.5;
}