简体   繁体   English

Swift 如何移动精灵?

[英]Swift how to move sprite?

I would like to know how to move a sprite in spritekit depending on the side of the screen the user is pressing down on.我想知道如何根据用户按下的屏幕一侧在 spritekit 中移动精灵。 Like if the user taps the right side of the screen the sprite should move to the right and when the user removes his finger the sprites stops moving.就像用户点击屏幕右侧一样,精灵应该向右移动,当用户移开手指时精灵停止移动。 Any suggestions?有什么建议? thank you so much太感谢了

use the touchesBegan func and get position of touch with:使用 touchesBegan 函数并通过以下方式获取触摸位置:

for location: AnyObject in touches {
var positionOfTouch = location.locationInNode(self)
//position of touch is of value CGPoint
}

store the value of xposition and yposition存储 xposition 和 yposition 的值

var xPostion = positionOfTouch.position.x
var yPosition = positionOfTouch.position.y
//all in the touchesBegan func

make an if else statement to see if the user touches a side or not做一个 if else 语句来查看用户是否触摸了一侧

if xPosition < 10 && yPosition > 10 && yPosition < self.size.height-10 {
//left side
let moveToLeftSide = SKAction.moveTo(x: 0, duration: 10)
object.run(moveToLeftSide)
}

continue setting values for the sides, then set the touchesEnded func inside the touchesEnded func, write:继续设置边的值,然后在 touchesEnded func 中设置 touchesEnded func,写:

object.removeAllActions()

hope it helped... if you found this answer helpful remember to set this answer to correct ;希望它有所帮助...如果您发现此答案有帮助,请记住将此答案设置为正确; ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM