【问题标题】:Move SKSpriteNode by button touch通过按钮触摸移动 SKSpriteNode
【发布时间】:2018-04-21 19:31:09
【问题描述】:
我在我的 GameScene 中获得了英雄的 SKSpriteNode。我实现了两个按钮(向上和向下)。我需要我的英雄的 SKSpriteNode 通过按钮触摸上下移动。但我只看到 touchesMoved 和 touchesEnded 方法。我需要一些关于 touchesBegan 的东西(当我点击它时感觉按钮点击)。
【问题讨论】:
标签:
swift
xcode
sprite-kit
mobile-development
【解决方案1】:
您创建某种重复步骤,从 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")
...
}