3

I am creating a Side Scroller Mario-like game in Swift and SpriteKit. I am currently moving the player yet you have to keep clicking and then it moves. What I am wishing is if you hold it it will move and you will not have to rapid click the screen. Thank you in advance !!!!

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

}
  • Looks like setting that move once, will move your object only once? Do you want to invoke it continuously as long as user has not lifted his finger ? – Shripada Jun 30 '16 at 03:58
  • Yes like in javascript there is a bool value called **mouseIsPressed** that is basicly what I need. – ScarletStreak Jun 30 '16 at 12:14

3 Answers3

0

I really don't understand very well what do you wish, but I try to write some code:

    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))
        }
    }
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0

Personally, I would create a controller class that has an update function that you poll on your scene update to determine whether a button state is up or down, and go off of that (Touch begins puts button in down state, touch end puts it in up state. update polls state, and performs actions based on state) But in your case, to keep things simple without changing a lot of code, I would just use 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

}
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
0

I suggest using below Code, it just Simple. just remove moving action when your touch ended.

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()
}
Hanryang
  • 231
  • 2
  • 12