我想知道如何根据用户按下的屏幕一侧在spritekit中移动精灵。就像用户点击屏幕的右侧一样,精灵应该向右移动,当用户移开他的手指时,精灵会停止移动。 有什么建议? 非常感谢你
答案 0 :(得分:0)
使用touchesBegan func并获取触摸位置:
for location: AnyObject in touches {
var positionOfTouch = location.locationInNode(self)
//position of touch is of value CGPoint
}
存储xposition和yposition的值
var xPostion = positionOfTouch.position.x
var yPosition = positionOfTouch.position.y
//all in the touchesBegan func
制作一个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)
}
继续设置边的值,然后设置touchesEnded func 在touchesEnded func中,写道:
object.removeAllActions()
希望它有所帮助......如果你发现这个答案有用,请记住将这个答案设置为正确; )