让我的节点跳跃

时间:2014-07-22 23:09:28

标签: sprite-kit

我用这段代码移动我的播放器:沿屏幕向左和向右移动。在屏幕的中央有一个"跳跃的人工制品"这可以让玩家在玩家接触时跳跃。

问题是当我的播放器正在运行并且跳转开始时。玩家失去他的(水平)x.velocity。只是沿着垂直方向跳跃,我需要的是保留他的x.velocity

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self.scene];
    if(touchLocation.x >self.size.width / 2.0){

        player.physicsBody.velocity=CGVectorMake(100, player.physicsBody.velocity.dy);

    }else{

        player.physicsBody.velocity=CGVectorMake(-100, player.physicsBody.velocity.dy);

    }
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self.scene];
    if(touchLocation.x >self.size.width / 2.0){

        player.physicsBody.velocity=CGVectorMake(5, player.physicsBody.velocity.dy);

    }else{

        player.physicsBody.velocity=CGVectorMake(-5, player.physicsBody.velocity.dy);
    }
}

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    if((firstBody.categoryBitMask == playerCategory && secondBody.categoryBitMask == jumpingCategory) ||
       (firstBody.categoryBitMask == jumpingCategory && secondBody.categoryBitMask == playerCategory))
    {
       NSLog(@"player hits jumping");

       player.physicsBody.velocity = CGVectorMake(player.physicsBody.velocity.dx, 390.0f);

    }
}

1 个答案:

答案 0 :(得分:0)

不要设置速度。 改为使用冲动

[player.physicsBody applyImpulse:CGVectorMake(0, 1)];

这样它将保留当前的水平速度值。