精灵套件物理body应用脉冲



目前我的应用程序是做什么的:当球移动时,你点击球改变方向并朝相反的方向前进。(上下)(从左到右不行) (0 = 向上,1 = 向下,2 = 不移动)基本上只是希望球在每次点击时改变方向。

问题是:我正在使用[ball.physicsBody applyImpulse:CGVectorMake(0, 100)];这引起了一些问题,因为为了改变方向,它必须抵消另一个力。

我了解到,将对抗另一种力量的力量加倍是有效的。 但是假设我设置了一个变量"ballSpeed"(尚不存在)ballSpeed * 2(用于反作用力),这将起作用,但是随着您在游戏中走得更远,这个数字不会变高吗?(可能最大整数为 2,147,483,647

-(void)handleDirectionChange{
if(((location.y>self.frame.size.height/2)&&(ball.position.y<self.frame.size.height/2))||((location.y<self.frame.size.height/2)&&(ball.position.y>self.frame.size.height/2))){
    if(canTap == true){
        NSLog(@"TAPPED-TRUE");
        if(direction == 0 && canTap == true){
            NSLog(@"MOVE-0");
            [ball.physicsBody applyImpulse:CGVectorMake(0, 100)];//hereeee
            canTap = false;
            NSLog(@"CANT TAP");
            direction = 1;
        }
        if(direction == 1 && canTap == true){
            NSLog(@"MOVE-1");
            [ball.physicsBody applyImpulse:CGVectorMake(0, -200)];//hereeeee
            canTap = false;
            NSLog(@"CANT TAP");
            direction = 0;
        }
    }
}

}

以防万一您想查看其他代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

     for (UITouch *touch in touches) {
        location = [touch locationInNode:self];
     }
     [self handleDirectionChange];

}

-(void)handleDirection{
[ballParent enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) {
    if([ball.name isEqualToString:@"ball"] && (ball.position.y > self.frame.size.height/2-2 && ball.position.y < self.frame.size.height/2+2) ){//when the ball passes middle
        if(canTap == false) {
            canTap = true;
            NSLog(@"CAN TAP");
        }
    }
}];

}



-(void)update:(CFTimeInterval)currentTime {

[self handleDirection];
}

您可以使用定义的速度而且根本不加倍,然后在你对物理体施加新的冲动之前你可以通过"重置"它的速度来"停止"

physicsBody.velocity=CGVectorMake(0,0);

你已经知道方向了因此,您可以制作一种方法,在重置物体的速度后将身体设置为转到方向

最新更新