gpt4 book ai didi

swift - 通过触摸按钮移动 SKSpriteNode

转载 作者:行者123 更新时间:2023-11-28 07:59:05 38 4
gpt4 key购买 nike

我的 GameScene 中有 hero 的 SKSpriteNode。我实现了两个按钮(向上和向下)。我需要我的英雄的 SKSpriteNode 通过触摸按钮上下移动。但我只看到 touchesMoved 和 touchesEnded 方法。我需要一些关于 touchesBegan 的东西(当我点击它时感觉按钮被点击)。

最佳答案

您创建某种重复步骤,从 touchesBegan 开始到 touchesEndedtouchesCancelled 或当触摸离开节点通过检查 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")
...
}

关于swift - 通过触摸按钮移动 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47191110/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com