我正在尝试制作一款游戏,其中我有一些 SKSpriteNode,用户可以用手指移动来击中它们,我正在使用苹果的新 Sprite Kit。
为此我尝试了一个技巧 - 放置一个 Sprite - “X” (SKSpriteNode) 在手指所在的位置,当用户移动手指时 - 改变这个 X sprite 的位置,
问题是它只有在不移动时才会撞击其他 Sprite ,我希望其他 Sprite 响应移动手指的当前速度 - 手指移动得越快 - 碰撞应该越强。
你能帮帮我吗?
虽然我觉得这个技巧不是正确的方法,但我也发布了代码。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if([touches count]==1) { self.X= [[SKSpriteNode alloc]initWithImageNamed:@"X"]; self.X.name= @"X"; UITouch *t= touches.allObjects[0]; self.X.position= [t locationInNode:self.GameNode]; self.X.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20]; self.X.physicsBody.dynamic=YES; // Tried NO too... self.X.zPosition=1; [self.GameNode addChild:self.X]; } } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *t = touches.allObjects[0]; self.X.position = [t locationInNode:self.GameNode]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self.X removeFromParent]; }
最佳答案
经过大量的搜索和实验,我终于有了答案!
解决方案的第一步由 AyatollahAndy 提供- 找到影响点:
SKNode *node = [self nodeAtPoint:location];
这行获取我们在指定位置命中的节点,如果它返回 nil 则我们没有命中任何东西。
其次 - 我们想要“击中”物体,因此我们需要 ApplyImpulse:
[node.physicsBody applyImpulse:CGVectorMake(thrustV.x, thrustV.y) atPoint:location];
在这里你可以看到我用我在冲击点创建的一些矢量应用了冲量, 就是这样 - 非常简单,我希望这会帮助其他人。
分享是关怀, 感谢您花时间阅读我的帖子。
关于ios - 如何在 Sprite Kit、Objective C 中用手指移动来击打物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19300365/