3

我正在用 Swift 和 SpriteKit 创建一个类似于马里奥的横向卷轴游戏。我目前正在移动播放器,但您必须继续单击它才能移动。我希望的是,如果您握住它,它会移动,而您不必快速单击屏幕。先感谢您 !!!!

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

                    if location.y > 400{

            move = true

        }else{


            if location.x < CGRectGetMidX(self.frame){
                player.physicsBody?.applyImpulse(CGVector(dx: -30, dy: 0))

                //tap somewhere above this to make character jump
                if(location.y > 250) {
                    player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200))
                }

            } else if location.x > CGRectGetMidX(self.frame){
                player.physicsBody?.applyImpulse(CGVector(dx: 30, dy: 0))

                //tap somewhere above this to make character jump

            }
        }

    }

    }



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {


    move = false

}
4

3 回答 3

0

我真的不太明白你想要什么,但我试着写一些代码:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if location.y > 400{
                move = true
            }else{
                 if location.x < CGRectGetMidX(self.frame){
                     movePlayer(-30,dy:0)

                    //tap somewhere above this to make character jump
                    if(location.y > 250) {
                        player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200))
                    }
                } else if location.x > CGRectGetMidX(self.frame){
                    movePlayer(30,dy:0)
                    //tap somewhere above this to make character jump
                }
            }
        }
    }
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if location.y > 400{
                move = false
            }else {
                if location.x < CGRectGetMidX(self.frame) || if location.x > CGRectGetMidX(self.frame) {
                    movePlayer(0,dy:0)
                }
            }
        }
    }

    func  movePlayer(dx:CGFloat,dy:CGFloat) {
        if (player.physicsBody.resting) {
             player.physicsBody?.applyImpulse(CGVector(dx, dy: dy))
        }
    }
于 2016-06-30T10:08:34.410 回答
0

就个人而言,我会创建一个控制器类,它具有一个更新函数,您可以在场景更新时轮询该函数以确定按钮状态是向上还是向下,然后关闭它(触摸开始将按钮置于向下状态,触摸结束将其放入up state. update polls state, and perform actions based on state) 但是在你的情况下,为了在不改变很多代码的情况下保持简单,我会使用SKAction.moveBy

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if location.y > 400{

            move = true

        }else{
            //tap somewhere above this to make character jump
            if(location.y > 250) {
                player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200))
            }
            else
            {  
                let direction = (location.x > CGRectGetMidX(self.frame)) ? 30 : -30
                let move  = SKAction.moveBy(x:direction,y:0,duration:0)
                let rep = SKAction.repeatActionForever(move)
                player.runAction(rep,forKey:"Moving")
            }
        }
    }
}



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    player.removeActionForKey("Moving")
    move = false

}
于 2016-06-30T15:19:18.477 回答
0

我建议使用下面的代码,它很简单。只需在触摸结束时删除移动动作。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if let touch = touches.first {

        let touchPoint = touch.location(in: self)

        let leftmoveAction = SKAction.move(to: CGPoint(x: player.position.x - 200, y: player.position.y), duration: 1)
        let rightMoveAction = SKAction.move(to: CGPoint(x: player.position.x + 200, y: player.position.y), duration: 1)

        if touchPoint.y > 80 {
        }else{
            print(touchPoint)
            if touchPoint.x < self.size.width / 2 {
                if player.position.x > 70 {
                    player.run(leftmoveAction)

                }
            }else{
                if player.position.x < 350 {
                player.run(rightMoveAction)
                }

            }

        }
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {


    player.removeAllActions()
}
于 2018-08-18T10:52:00.053 回答