1
votes

I want to make right side of the screen to make the node jump, first half of the left part to left and second half to move right.

Some stuff that aren't okay:

  • when I tap fast left,right,left,right many times (it will stop and will not move the node)

  • when I tap the right part of the screen And tap of the left or right section not every time the node moves when is falling, some times the first problem will repeat

Here is an example of game with the same mechanics:

https://itunes.apple.com/us/app/staying-together/id923670329?mt=8

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let tapper = UILongPressGestureRecognizer(target: self, action: "tappedScreen:");
    tapper.minimumPressDuration = 0.1
    view.addGestureRecognizer(tapper)
}

func jumpAction(){
    if(!isJumping){
        hero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: ySpeed))
    }   
    isJumping = true
}

func tappedScreen(recognizer: UITapGestureRecognizer){
    if(recognizer.state == UIGestureRecognizerState.Began){     
        let moveLeft = SKAction.moveByX(-15, y: 0, duration: 0.1)
        let moveRight = SKAction.moveByX(15, y: 0, duration: 0.1)

        let touchY = self.convertPointFromView(recognizer.locationInView(self.view)).y

        // only the bottom part of the screen, that way the UI button will be able to touch
        if(touchY < self.frame.size.height/4){      
            if(touchX > 0){
                println("up - longer");
            } else if(touchX < -(self.frame.size.width/4)){      
                println("left - longer");
                hero.runAction(SKAction.repeatActionForever(moveLeft), withKey: "longTap")
            } else {
                println("right - longer");
                hero.runAction(SKAction.repeatActionForever(moveRight), withKey: "longTap")
            }
        } 
    } else {
        if(touchX <= 0){
            if (recognizer.state == UIGestureRecognizerState.Ended) {
                println("ended, also stops the node from moving");
                hero.removeActionForKey("longTap")
            }
        }
    }
}

// I think this is need to mmove the node on single tap
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    let touch: UITouch = touches.first as! UITouch;
    let location:CGPoint = touch.locationInNode(self);

    let moveLeft = SKAction.moveByX(-15, y: 0, duration: 0.1)
    let moveRight = SKAction.moveByX(15, y: 0, duration: 0.1)

    // only the bottom part of the screen, that way the UI button will be able to touch
    if(location.y < self.frame.size.height/4){
        if(location.x > 0){
            println("up");
            self.jumpAction();
        } else if(location.x < -(self.frame.size.width/4)){
            hero.runAction(moveLeft);
            println("left");
        } else {
            hero.runAction(moveRight);
            println("right");
        }
    }
}
1

1 Answers

0
votes

Its because tappedScreen has an argument that refers to UITapGestureRecognizer. you have to set it to UILongPressGestureRecognizer.

func tappedScreen(recognizer: UILongPressGestureRecognizer){

}

Hope it helps :)