通过触摸按钮移动 SKSpriteNode
Move SKSpriteNode by button touch
我在我的GameScene中得到了英雄的SKSpriteNode。我实现了两个按钮(向上和向下)。我需要我的英雄的 SKSpriteNode 通过触摸按钮上下移动。但我只看到 touchesMoved 和 touchesEnded 方法。我需要一些关于 touchesBegan 的东西(当我点击它时感觉按钮被点击)。
您创建某种重复步骤,从 touchesBegan
开始并在 touchesEnded
、touchesCancelled
或当触摸离开节点时结束
通过检查 touchesMoved
override func touchesBegan(...)
{
...
let moveAction = SKAction.repeatForever(SKAction.move(by:CGPoint(x:x,y:y)))
node.run(moveAction,forKey:"moving")
...
}
override func touchesMoved(...)
{
...
//add a check to see if if touch.position is not inside the button
if (...)
{
node.removeAction(withKey:"moving")
}
...
}
override func touchesEnded(...)
{
...
node.removeAction(withKey:"moving")
...
}
override func touchesCancelled(...)
{
...
node.removeAction(withKey:"moving")
...
}
我在我的GameScene中得到了英雄的SKSpriteNode。我实现了两个按钮(向上和向下)。我需要我的英雄的 SKSpriteNode 通过触摸按钮上下移动。但我只看到 touchesMoved 和 touchesEnded 方法。我需要一些关于 touchesBegan 的东西(当我点击它时感觉按钮被点击)。
您创建某种重复步骤,从 touchesBegan
开始并在 touchesEnded
、touchesCancelled
或当触摸离开节点时结束
通过检查 touchesMoved
override func touchesBegan(...)
{
...
let moveAction = SKAction.repeatForever(SKAction.move(by:CGPoint(x:x,y:y)))
node.run(moveAction,forKey:"moving")
...
}
override func touchesMoved(...)
{
...
//add a check to see if if touch.position is not inside the button
if (...)
{
node.removeAction(withKey:"moving")
}
...
}
override func touchesEnded(...)
{
...
node.removeAction(withKey:"moving")
...
}
override func touchesCancelled(...)
{
...
node.removeAction(withKey:"moving")
...
}