Как я могу различить два разных прикосновения к слою? - PullRequest
0 голосов
/ 14 апреля 2010

Я пишу приложение в 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)];

    }

Но распознаются только прикосновения в тексте, а прикосновение спрайта не распознается? Как я могу различить два прикосновения. Может кто-нибудь предложить лучший метод для этого, чем мое решение.

1 Ответ

0 голосов
/ 15 апреля 2010

Я получил то, что мне нужно, используя координаты. Я написал условия if-else в textclass (класс, касание которого распознается в моей программе). Я дифференцировал текст и 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];
    }
}  

Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...