chanpkr
chanpkr

Reputation: 915

How to make a responsive controller in SpriteKit?

Working on my new game with iOS SpriteKit, I'm trying to make a simple left, right controller to move my character around on the screen. This is how I do it. This is a SKScene implementation.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    if (touchLocation.x < [UIScreen mainScreen].applicationFrame.size.width/2) {
        _leftPressed = YES;
    } else if (touchLocation.x > [UIScreen mainScreen].applicationFrame.size.width/2){
        _rightPressed = YES;
    }
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    _leftPressed = NO;
    _rightPressed = NO;
}


-(void)update:(CFTimeInterval)currentTime {
    if (_rightPressed) {
        [self.player.physicsBody applyForce:CGVectorMake(speed, 0)];
    } else if (_leftPressed) {
        [self.player.physicsBody applyForce:CGVectorMake(-speed, 0)];
    } else {

    }
}

Soon, I found a problem on this method of controlling my character. If I simultaneously tap down on one finger and let go of my other finger to change direction, the character just stops and doesn't change its direction. I guess it's because method - (void) touchesBegan and method - (void) touchesEnded cannot be called at the same time. I want my controller to be real sensitive and be able to handle multi-touch smoothly. How do I make this work? Is there a nice way of solving this problem?

Upvotes: 1

Views: 460

Answers (1)

dragoneye
dragoneye

Reputation: 701

is SKView.multipleTouchEnabled is enabled inside your ViewController.m file

Upvotes: 1

Related Questions