[英]Sprite-kit physicsBody applyImpulse
目前,我的应用程序正在执行以下操作:当球移动并且您点击时,球会改变方向并朝相反的方向前进。 (向上和向下)(从左到右否)(0 =向上,1 =向下,2 =不移动)基本上只是希望每次u轻击时球就切换方向。
问题是:我正在使用[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);
您已经知道方向,因此可以在重置身体速度后制定一种方法来将身体设置为朝该方向
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.