gpt4 book ai didi

swift - 仅在按下时才让 Sprite 移动?

转载 作者:行者123 更新时间:2023-11-30 13:21:21 27 4
gpt4 key购买 nike

屏幕上有一个圆圈,每当我按屏幕右侧时,该圆圈就会向右移动,而当我按屏幕左侧时,该圆圈就会向左移动。现在我需要做到这一点,以便当我开始触摸时,我的圆圈向右移动,但如果我释放触摸,那么圆圈将停止,直到我再次按下屏幕。

这是在我的 TouchesBegan 函数中。

    for touch: AnyObject in touches {


let location = touch.locationInNode(self)
if location.x < self.size.width / 2 {

let moveLeft = SKAction.moveToX(self.frame.width / 3, duration: 1.0)
Ball.runAction(moveLeft)

}
else {

let moveRight = SKAction.moveToX(self.frame.width / 1.445, duration: 1.0)
Ball.runAction(moveRight)


}





}

这是我到目前为止所拥有的。

最佳答案

基本上是这样的:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < self.size.width / 2 {

let moveLeft = SKAction.moveToX(self.frame.width / 3, duration: 1.0)
Ball.runAction(moveLeft, withKey:"moveLeft")
}
else {
let moveRight = SKAction.moveToX(self.frame.width / 1.445, duration: 1.0)
Ball.runAction(moveRight withKey:"moveRight")
}
}
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
Ball.removeActionForKey("moveLeft")
Ball.removeActionForKey("moveRight")
}

我建议您比每次需要它们时将它们硬编码为字符串更聪明地处理这些键,但这就是要点。

关于swift - 仅在按下时才让 Sprite 移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740132/

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