2010-04-14 4 views
0

J'écris une application dans cocos2d. J'ai un sprite et un texte dans ma scène. J'ai écrit deux classes séparées pour le sprite et le texte. Et j'ai ajouté les deux à une autre classe.
En classe sprite j'ai écritComment puis-je différencier deux touches différentes sur une couche?

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

Et dans la classe de texte j'ai écrit

-(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)]; 

    } 

Mais, ne touche dans le texte sont reconnus et le toucher de sprite est pas reconnu? Comment puis-je différencier les deux touches. Quelqu'un peut-il suggérer une meilleure méthode pour cela que ma solution.

Répondre

0

J'ai obtenu ce dont j'avais besoin en utilisant des coordonnées. J'ai écrit des conditions if-else dans textclass (la classe qui touche est reconnue dans mon programme). J'ai différencié le texte et le spriteImage en utilisant les coordonnées. Comme ceci dans la classe de texte, mais ai-je une meilleure méthode. Je pense que c'est une méthode très grossière.

-(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]; 
    } 
} 

Merci.

Questions connexes