作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 Sprite-Kit (Objective-C) 开发游戏。在这个游戏中,你控制一只飞翔的鸟,然后从屏幕的右/上/下侧向你射出箭和其他坏射弹。我正在使用物理学而不是 SKAction 来实现这一点,因为我希望它看起来尽可能逼真。所以我知道在射弹上使用 applyImpulse 将它射向鸟,但我想知道的是如何保证射弹将直接射向鸟,无论鸟的 y 定位和 y 定位applyImpulse 之前的射弹?
我在完成这项工作时遇到了非常令人沮丧的事情,因此非常感谢对此问题的任何帮助。谢谢。
最佳答案
基本步骤是
这是一个如何做到这一点的例子
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];
swift
// 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)
关于ios - Sprite Kit - 应用 Impulse 向角色发射炮弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26364921/
我是一名优秀的程序员,十分优秀!