Sprite Kit游戏没有检测到两个同时触摸

时间:2014-09-02 18:32:27

标签: ios xcode sprite-kit

我正在开发一款Sprite Kit游戏,需要对屏幕左侧或右侧的触摸做出反应。我在另一个问题中找到了这个解决方案:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];

if (touchLocation.x < self.size.width / 2.0f) {
    touching = YES;
}

if (touchLocation.x > self.size.width / 2.0f) {
    [self drawRocket];
}

而且,

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];

if (touchLocation.x < self.size.width / 2.0f) {
    touching = NO;
}

if (touchLocation.x > self.size.width / 2.0f) {
    //do nothing
}

}

我想,很棒。有用。好吧,似乎,直到我碰巧用不同的手指同时触摸两侧。它会调用drawRocket,但不会将touching的值更改为YES。这个问题反复发生,可以复制。

怎么办?

编辑:

我试过了:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];



    if (touchLocation.x < self.size.width / 2.0f) {
        touching = YES;
    }

    if (touchLocation.x > self.size.width / 2.0f) {
        if (![rocketFlame parent]) {
            [self fireRocket];
        }
    }
}

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];


for (UITouch *touch in touches) {
    if (touchLocation.x < self.size.width / 2.0f) {
        touching = YES;
    }

    if (touchLocation.x > self.size.width / 2.0f) {
        if (![rocketFlame parent]) {
            [self fireRocket];
        }
    }
}

}

我仍然遇到同样的问题。

2 个答案:

答案 0 :(得分:0)

在初始化方法self.view.multiple中添加以下行Touch Enabled = YES;

for (UITouch *touch in touches) {

CGPoint touchLocation = [touch locationInNode:self];
if (touchLocation.x < self.size.width / 2.0f) {
    touching = YES;
}

if (touchLocation.x > self.size.width / 2.0f) {
    [self drawRocket];
}
}

使用鼠标和键盘进行模拟器多点触控手势

答案 1 :(得分:0)

您正在定义一个隐藏用于循环触摸事件的变量的局部变量。请参阅下面代码中的注释。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {

        // The following statement is causing the issue. Delete it.
        //UITouch *touch = [touches anyObject];

        CGPoint touchLocation = [touch locationInNode:self];

        if (touchLocation.x < self.size.width / 2.0f) {
            touching = YES;
        }

        if (touchLocation.x > self.size.width / 2.0f) {
            if (![rocketFlame parent]) {
                [self fireRocket];
            }
        }
    }
}