【问题标题】:Making a Sprite move only when pressing?仅在按下时才使精灵移动?
【发布时间】:2018-07-30 09:06:50
【问题描述】:

我在屏幕上有一个圆圈,当我按下屏幕右侧时,它会向右移动,当我按下屏幕左侧时,它会向左移动。现在我需要做到这一点,当我开始触摸时,我的圆圈向右移动,但如果我松开触摸,圆圈将停止,直到我再次按下屏幕。

这是在我的 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)


        }





    }

这是我目前所拥有的。

【问题讨论】:

    标签: swift xcode sprite game-physics


    【解决方案1】:

    基本上是这样的:

    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")
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-15
      • 2015-07-17
      • 2022-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多