Sprite-kit physicsBody apply脉冲
Sprite-kit physicsBody applyImpulse
目前我的应用程序的功能: 当球在移动时,您点击球会改变方向并朝相反的方向前进。 (上下)(不从左到右)(0 = 向上,1 = 向下,2 = 不移动)基本上只是希望球在每次点击时切换方向。
问题是:我正在使用[ball.physicsBodyapplyImpulse: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];
}
您可以使用定义的速度
而不是加倍,
然后在你对物理学施加新的推动力之前 body
你可以通过“重置”它的速度
来让它 "stop"
physicsBody.velocity=CGVectorMake(0,0);
你已经知道方向了
所以你可以做一个方法来设置 body 在你重置 body 的速度
之后去的方向
目前我的应用程序的功能: 当球在移动时,您点击球会改变方向并朝相反的方向前进。 (上下)(不从左到右)(0 = 向上,1 = 向下,2 = 不移动)基本上只是希望球在每次点击时切换方向。
问题是:我正在使用[ball.physicsBodyapplyImpulse: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];
}
您可以使用定义的速度 而不是加倍, 然后在你对物理学施加新的推动力之前 body 你可以通过“重置”它的速度
来让它 "stop"physicsBody.velocity=CGVectorMake(0,0);
你已经知道方向了 所以你可以做一个方法来设置 body 在你重置 body 的速度
之后去的方向