加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – Sprite Kit – 使用Impulse在角色上射击射弹

发布时间:2020-12-15 01:42:50 所属栏目:百科 来源:网络整理
导读:我正在使用Sprite-Kit(Objective-C)开发游戏.这是一个你在飞行中控制一只鸟的游戏,箭头和其他坏的射弹从屏幕的右/上/下侧向你射击.我使用物理而不是SKAction来实现这一目标,因为我希望它看起来尽可能像生活一样.因此我知道在弹丸上使用applyImpulse将它射向
我正在使用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];

迅速

// 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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读