How to use touch and hold for a set of sprites?

I recently started working with sprite-kit. I know it touchesBeganworks with just one tap, but is there something I can use that recognizes the touch that is being held?

+3
source share
3 answers

If you want to implement something like shooting, you need to start shooting in the method touchesBeganand stop shooting in the method touchesEnded:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self startShooting];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self stopShooting];
}

For other purposes, you can add UILongPressGestureRecognizertoSKScene

+5
source

Alternatively, you can use a boolean with the update method:

bool isTouching = false;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    isTouching = true;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    isTouching = false;
}

-(void)update:(CFTimeInterval)currentTime {
    if(isTouching){
        //shooting! 
    }
}

isTouching, , touchhesBegan, .

, , isTouching == true

+4
var isTouching = false

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    handleTouches(touches) 
    isTouching = true;
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    handleTouches(touches)
    isTouching = false;
}

override func update(currentTime: NSTimeInterval) {
    if isTouching{
        //Shoot CODE!
    }
}
+3
source

All Articles