持续触摸/移动:Swift和SpriteKit

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

}

看起来设置那个移动一次,只会移动你的对象一次?你想在用户没有松开手指的情况下连续调用它吗? - Shripada
是的,就像在javascript中有一个名为mouseIsPressed的bool值,这基本上就是我所需要的。 - ScarletStreak
3个回答

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

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

0

个人而言,我会创建一个控制器类,其中包含一个更新函数,您可以在场景更新时轮询该函数以确定按钮状态是上升还是下降,并根据此进行操作(触摸开始将按钮置于下降状态,触摸结束将其置于上升状态。更新轮询状态,并根据状态执行操作)。但在您的情况下,为了保持简单而不改变很多代码,我只会使用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

}

请注意,您需要处理多个触摸事件,因此您可能希望在touchbegin中删除动作。 - Knight0fDragon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接