如何检测cocos2d中的多点触控?

时间:2011-10-14 02:37:53

标签: cocos2d-iphone multi-touch

已编辑的帖子

好的,我试过了rptwsthi在另一个项目中说的只是为了测试它......

-(id) init
{
if( (self=[super init])) {

    self.isTouchEnabled = YES;
}
return self;
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSArray *touchArray=[touches allObjects];

    if ([touchArray count] == 2) {
        NSLog(@"touch 2");
    }
    else if([touchArray count]==1) {
        NSLog(@"touch 1");
    }
}

但是当我用两根手指按屏幕时,只有“触摸1”NSLog弹出。我是否需要在其中的某处放置LearnCocos2D所说的内容。

旧帖子

我有一个我正在制作的测试应用程序,其中有3个按钮,2个用于左右移动,另一个用于拍摄。这就是我目前所拥有的......

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint loc = [touch locationInView:[touch view]];
loc = [[CCDirector sharedDirector] convertToGL:loc];

//Move Left
CGRect left = CGRectMake(leftButton.position.x-50/2, leftButton.position.y-50/2, 50, 50);
if (CGRectContainsPoint(left, loc)) {
    [self schedule:@selector(moveLeft)]; 
}

//Move Right
CGRect right = CGRectMake(rightButton.position.x-50/2, rightButton.position.y-50/2, 50, 50);
if (CGRectContainsPoint(right, loc)) {
    [self schedule:@selector(moveRight)];
}

//Shoot
CGRect shoot = CGRectMake(shootButton.position.x-50/2, shootButton.position.y-50/2, 50, 50);
    if (CGRectContainsPoint(shoot, loc)) {
    bullet = [CCSprite spriteWithFile:@"bullet.png"];
    bullet.position = ccp(plane.position.x, plane.position.y+20);
    [self addChild:bullet];
    }
}

-(void) ccTouchesEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    [self unschedule:@selector(moveLeft)];
    [self unschedule:@selector(moveRight)];
}

但我一次只能按一个按钮。我希望能够按住右或左按钮并使用拍摄按钮进行拍摄。任何人都可以修复我的代码或向我展示多点触控的基本示例吗?

此外,我是iOS开发的新手,我们非常感谢任何帮助。感谢。

2 个答案:

答案 0 :(得分:5)

您是否在cocos2d视图中启用了多次触摸?

[[CCDirector sharedDirector].openGLView setMultipleTouchEnabled:YES];

答案 1 :(得分:3)

您只需使用allObject代替anyObject,并将其检查为:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSArray *touchArray=[touches allObjects];

    if ([touchArray count] == 2)
        //DO ONE THING 
    else if([touchArray count]==1)
        //DO ANOTHER THING
}