I am writing an app in cocos2d. I hava a sprite and a text in my scene. I have written two separate classes for sprite and text. And I added both of them to another class.
In sprite class I have written- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
And in text class I have written
-(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 g开发者_Go百科etChildByTag: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)];
}
But, only touches in the text are recognized and touch of sprite is not recognized ? How can I differentiate the two touches. Can anyone suggest better method for it than my solution.
I did get what I need by using coordinates . I wrote if-else conditions in textclass(the class which touches are recognized in my program). I differentiated the text and the spriteImage by using the coordinates. Like this in text class, But do I have any better method. I think this is very crude method.
-(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];
}
}
Thank You.
精彩评论