Sprite Kit – 使用Impulse在angular色上射击弹丸

我正在开发一个使用Sprite-Kit(Objective-C)的游戏。 这是一个游戏,你在飞行中控制一只鸟,从屏幕的右侧/上侧/下侧向你射击箭头和其他不良射弹。 我使用物理学而不是SKAction来完成这个任务,因为我希望它看起来像生命一样可能。 所以我知道用射弹上的applyImpulse把它射向小鸟,但是我想知道的是怎样才能保证射弹直接射到小鸟身上,不pipe鸟的y坐标和y坐标弹丸之前应用脉冲?

我有一个非常令人沮丧的时间完成这个,所以任何帮助这个问题将不胜感激。 谢谢。

基本的步骤是

  1. 计算从抛射器发射到鸟的vector分量
  2. 标准化组件(可选)
  3. 通过缩放(标准化)组件来创buildvector
  4. 使用vector对射弹施加冲量

这是一个如何做到这一点的例子

OBJ-C

// Calculate vector components x and y CGFloat dx = bird.position.x - launcher.position.x; CGFloat dy = bird.position.y - launcher.position.y; // Normalize the components CGFloat magnitude = sqrt(dx*dx+dy*dy); dx /= magnitude; dy /= magnitude; // Create a vector in the direction of the bird CGVector vector = CGVectorMake(strength*dx, strength*dy); // Apply impulse [projectile.physicsBody applyImpulse:vector]; 

迅速

 // Calculate vector components x and y var dx = bird.position.x - launcher.position.x var dy = bird.position.y - launcher.position.y // Normalize the components let magnitude = sqrt(dx*dx+dy*dy) dx /= magnitude dy /= magnitude // Create a vector in the direction of the bird let vector = CGVector(dx:strength*dx, dy:strength*dy) // Apply impulse projectile.physicsBody?.applyImpulse(vector)