栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Swift + SpriteKit将节点移至手指

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Swift + SpriteKit将节点移至手指

使用以下命令可以为您省去很多麻烦

myShip.physicsBody.applyImpluse(vector)
。它的工作方式就像您
myShip
在方向
vector
点上施加了推力一样。如果您计算的
vector
是从上次触摸位置到的x距离
myShip
,则它会加速,减速,改变方向等。与您描述的方式非常接近,因为它只会在正确的方向上进行少量推动每个
update

基本上,您存储了最后一次触摸的位置,然后在

update
函数中计算了
CGVector
myShip
到的指向,
lastTouch
并将其作为对物理身体的冲动。

就像是:

var lastTouch: CGPoint? = niloverride func touchesBegan(touches: NSSet, withEvent event: UIEvent) {    let touch = touches.anyObject() as UITouch    let touchLocation = touch.locationInNode(self)    lastTouch = touchLocation}override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {    let touch = touches.anyObject() as UITouch    let touchLocation = touch.locationInNode(self)    lastTouch = touchLocation}// Be sure to clear lastTouch when touches end so that the impulses stop being appliesoverride func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {    lastTouch = nil}override func update(currentTime: CFTimeInterval) {    // only add an impulse if there's a lastTouch stored    if let touch = lastTouch {        let impulseVector = CGVector(touch.x - myShip.position.x, 0)        // If myShip starts moving too fast or too slow, you can multiply impulseVector by a constant or clamp its range        myShip.physicsBody.applyImpluse(impulseVector)    }}

您可能还想使用

linearDamping
和的
angularDamping
myShip.physicsBody
。它们将帮助确定
myShip
加速和减速的速度。

我最大化了

1.0
我的应用程序中的值:

myShip.physicsBody.linearDamping = 1.0myShip.physicsBody.angularDamping = 1.0

如果

myShip
不能足够快地停止,您还可以尝试对
update
函数进行一些中断:

override func update(currentTime: CFTimeInterval) {    // only add an impulse if there's a lastTouch stored    if let touch = lastTouch {        let impulseVector = CGVector(touch.x - myShip.position.x, 0)        // If myShip starts moving too fast or too slow, you can multiply impulseVector by a constant or clamp its range        myShip.physicsBody.applyImpluse(impulseVector)    } else if !myShip.physicsBody.resting {        // Adjust the -0.5 constant accordingly        let impulseVector = CGVector(myShip.physicsBody.velocity.dx * -0.5, 0)        myShip.physicsBody.applyImpulse(impulseVector)    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/383420.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号