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

时间:2010-04-14 13:40:26

标签: cocoa-touch cocos2d-iphone

我正在用cocos2d编写一个应用程序。 我在场景中有精灵和文字。我为精灵和文本编写了两个单独的类。我把它们都加到了另一个班级 在sprite类中,我写了

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

    }

但是,只识别文本中的触摸并且无法识别精灵的触摸? 我怎样才能区分这两种触摸。任何人都可以提出比我的解决方案更好的方法。

1 个答案:

答案 0 :(得分:0)

我确实通过使用坐标得到了我需要的东西。我在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];
    }
}  

谢谢。