一千萬個為什麽

搜索

如何區分圖層上的兩個不同觸摸?

我在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];
    }
}  

謝謝。

轉載註明原文: 如何區分圖層上的兩個不同觸摸?