简体   繁体   English

如何在SpriteKit中制作一个响应式控制器?

[英]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. 使用iOS SpriteKit开发新游戏时,我试图创建一个简单的左右控制器来在屏幕上移动角色。 This is how I do it. 这就是我的方法。 This is a SKScene implementation. 这是一个SKScene实现。

- (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. 我猜是因为不能同时调用方法- (void) touchesBegan和方法- (void) touchesEnded 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? 有解决这个问题的好方法吗?

是在ViewController.m文件中启用了SKView.multipleTouchEnabled

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM