所以,我在SpriteKit中有一个涉及圆形物体的游戏,一个由玩家控制。
问题:
当玩家沿着屏幕轻击或拖动时,第一个物体随之移动。 第二个对象不是由播放器或计算机控制,而是一个可以反弹的物理对象。然而,启用物理机构只允许我推动它,但它不会反弹。我已将两个物体的恢复原状设定为1.0,摩擦力设为0.0。没有引力。我移动玩家对象的方式是在玩家点击或拖动时通过移动动作调用runAction,这似乎让第二个对象被推动。我觉得我错过了一些东西,可能很明显。
答案 0 :(得分:1)
使用SKAction移动只会使您的节点移动到特定点。如果您希望播放器节点向touchPoint方向移动,则需要使用applyImpulse。以下是您如何实现这一目标:
在Ray Wenderlich的教程中查看射击射弹部分:
http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners
从教程中复制rwAdd,rwSub,rwMult,rwLength和rwNormalize方法。
然后,在您的-touchesEnded
方法中:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 1 - Choose one of the touches to work with
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
// 2 - Set up initial location of projectile
SKSpriteNode * projectile = [self childNodeWithName:@"desirednode"];
//make projectile variable point to your desired node.
// 3- Determine offset of location to projectile
CGPoint offset = rwSub(location, projectile.position);
// 4 - Get the direction of where to shoot
CGPoint direction = rwNormalize(offset);
// 5 - Make it shoot far enough to be guaranteed off screen
float forceValue = 200; //Edit this value to get the desired force.
CGPoint shootAmount = rwMult(direction, forceValue);
//6 - Convert the point to a vector
CGVector impulseVector = CGVectorMake(shootAmount.x, shootAmount.y);
//This vector is the impulse you are looking for.
//7 - Make the node ignore it's previous velocity (ignore if not required)
projectile.physicsBody.velocity = CGVectorMake(0.0, 0.0);
//9 - Apply impulse to node.
[projectile.physicsBody applyImpulse:impulseVector];
}
代码中的射弹对象代表您的节点。此外,您需要编辑forceValue以获得所需的冲动。