如何区分图层上的两个不同的触摸?

问题描述:

我正在编写一个应用程序在cocos2d。 我在我的场景中有一个精灵和一段文字。我为精灵和文本写了两个单独的类。我把他们俩都加到了另一个班上。
在精灵类我写如何区分图层上的两个不同的触摸?

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

而且在文本类我已经写

-(void) registerWithTouchDispatcher 
    { 

    [[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
    } 

    -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
    { 
    return YES; 
    } 

    -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
    { 
NSLog(@"Recognized tOuches in Instructions");// 
CGSize windowSize = [[CCDirector sharedDirector] winSize]; 
CCNode *node = [self getChildByTag:kTagNode]; 
[node setPosition: ccp(text1.contentSize.width/2,text1.contentSize.height/2 - windowSize.height)]; 

    } 

    -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 

    { 
CGPoint touchLocation = [touch locationInView: [touch view]]; 
CGPoint prevLocation = [touch previousLocationInView: [touch view]]; 

touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 
prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation]; 

CGPoint diff = ccpSub(touchLocation,prevLocation); 

CCNode *node = [self getChildByTag:kTagNode]; 
CGPoint currentPos = [node position]; 
[node setPosition: ccpAdd(currentPos, diff)]; 

    } 

但是,只触及文本识别和精灵的触摸不被识别? 我如何区分这两个触摸。任何人都可以提出比我的解决方案更好的方法。

我确实通过坐标得到了我需要的东西。我在textclass中编写了if-else条件(触摸的类在我的程序中被识别)。我通过使用坐标来区分文本和spriteImage。 像这样在文本类,但我有更好的方法。我认为这是非常粗暴的方法。

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 

{ 
CGPoint touchLocation = [touch locationInView: [touch view]]; 
CGPoint prevLocation = [touch previousLocationInView: [touch view]];  

touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 
prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation]; 


if((touchLocation.x < 300)) 
    { 
    CGPoint diff = ccpSub(touchLocation,prevLocation); 
    CCNode *node = [self getChildByTag:kTagNode]; 
    CGPoint currentPos = [node position]; 
    [node setPosition: ccpAdd(currentPos, diff)]; 
    } 
} 

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 

    if((touchLocation.x < 300)) 
    { 
     CGSize windowSize = [[CCDirector sharedDirector] winSize]; 
     CCNode *node = [self getChildByTag:kTagNode]; 
     [node setPosition: ccp(text1.contentSize.width/2,text1.contentSize.height/2 - windowSize.height)]; 
    } 

    else if((touchLocation.x > (gun.position.x - gun.contentSize.width/2)) && (touchLocation.x < (gun.position.x + gun.contentSize.width/2)) && (touchLocation.y > (gun.position.y - gun.contentSize.height/2)) && (touchLocation.y < (gun.position.y + gun.contentSize.height/2))) 
    { 
     CCScene *Scene = [CCScene node]; 
     CCLayer *Layer = [optionPage node]; 
     [Scene addChild: Layer]; 

     [CCDirector sharedDirector] setAnimationInterval:1.0/60]; 
     [[CCDirector sharedDirector] pushScene: Scene]; 
    } 
} 

谢谢。