2011-10-14 33 views
1

后期编辑如何检测cocos2d中的多点触摸?

好,我试了一下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的UPS用两个手指屏幕。我是否需要将LearnCocos2D在其他地方所说的话放在那里?

旧帖子

我有一个测试程序,我正在做,并在其中我有3个按钮的HUD,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开发新手,任何帮助将不胜感激。谢谢。

回答

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 
} 
+0

我更新了我的文章。你能检查它吗? – user994620

+0

参考[这个链接..](http://www.qcmat.com/dragging-multiple-sprites-in-cocos2d/)这将帮助你更多 – rptwsthi

5

是否启用多点触摸的cocos2d的看法?

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

对不起,但我在哪里把这个?我不确定cocos2d文件是什么文件。 – user994620

+0

您可以在任何地方调用此方法,即在场景的init方法中或在应用程序委托的applicationDidFinishLaunching方法中。 – LearnCocos2D