【问题标题】:How to make a responsive controller in SpriteKit?如何在 SpriteKit 中制作响应式控制器?
【发布时间】:2014-09-16 15:37:48
【问题描述】:

在使用 iOS SpriteKit 开发我的新游戏时,我正在尝试制作一个简单的左右控制器来在屏幕上移动我的角色。我就是这样做的。这是一个 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 {

    }
}

很快,我发现这种控制我的角色的方法有问题。如果我同时按下一根手指并松开另一根手指来改变方向,角色就会停止并且不会改变方向。我想这是因为方法- (void) touchesBegan 和方法- (void) touchesEnded 不能同时调用。我希望我的控制器真正灵敏并且能够流畅地处理多点触控。我该如何进行这项工作?有没有解决这个问题的好方法?

【问题讨论】:

    标签: ios xcode ios7 sprite-kit game-physics


    【解决方案1】:

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

    【讨论】:

    • 这对我有用。在我的 GameViewController : UIViewController 中,我刚刚在 viewDidLoad() 中添加了 self.view.multipleTouchEnabled = true。
    猜你喜欢
    • 2021-05-18
    • 2015-04-23
    • 2012-06-08
    • 1970-01-01
    • 2012-07-27
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多