• Advertisement
Sign in to follow this  

Why does this only detect touch in the center of my CCSprite?

This topic is 2676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

So right now I have been working on some code that will let the user drag one CCSprite. There are currently two CCSprites on the field. So, I need to determine what CCSprite the user is touching, if is he is touching any at all. To do so I have tried this:


-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedCoordinate = [[CCDirector sharedDirector] convertToGL:location];

//NSLog(@"%f", redCircle.contentSize.width);

CGRect redCircleRect = CGRectMake(redCircle.position.x, redCircle.position.y, 40.0, 40.0);
CGRect redCircle2Rect = CGRectMake(redCircle2.position.x, redCircle2.position.y, 40.0, 40.0);

if(CGRectContainsPoint(redCircleRect, convertedCoordinate)) {
//redCircle.position = ccp(convertedCoordinate.x, convertedCoordinate.y);
NSLog(@"Touched");
spriteIndex = 1;
}
else if(CGRectContainsPoint(redCircle2Rect, convertedCoordinate)) {
//redCircle2.position = ccp(convertedCoordinate.x, convertedCoordinate.y);
NSLog(@"Touched2");
spriteIndex = 2;
}
else {
spriteIndex = 0;
}
return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedCoordinate = [[CCDirector sharedDirector] convertToGL:location];

if (spriteIndex == 1) {
redCircle.position = ccp(convertedCoordinate.x, convertedCoordinate.y);
}
else if (spriteIndex == 2) {
redCircle2.position = ccp(convertedCoordinate.x, convertedCoordinate.y);
}

//CGRect RedCircleRect = CGRectMake(redCircle.position.x, redCircle.position.y, redCircle.contentSize.width, redCircle.contentSize.height);
//CGRect RedCircle2Rect = CGRectMake(redCircle.position.x, redCircle.position.y, redCircle.contentSize.width, redCircle.contentSize.height);
}



Essentially, my logic behind this was as follows:

In ccTouchesBegan start off by getting and then converting the coordinates of the touch. Then, make two CGrects with the same dimensions as my sprites. Then using the method provide by apple CGRectContainsPoint I tried finding out which CCSprite the user was touching, if any at all. Then, I made a global integer called spriteIndex which keeps track of which CCSprite and then I leave ccTouchesMoved to do the rest.

Now in ccTouchesMoved I check if there is a sprite being pressed by checking spriteIndex's value and then moving it accordingly.

The problem is that the touches are only detected in the center of each sprite, other wise the sprite is not moved.

Thanks for your help in advanced.

Share this post


Link to post
Share on other sites
Advertisement
Are redCircle.position.x and redCircle.position.y the center of your circle ? If so your rect is starting at the center of your circle and growing right and up (IIRC on Mac OS 0, 0 is bottom left) like this:

|-------|
| |
/-|-\ |
/ | \ |
| |--|----|
\ /
\---/



sorry for the bad drawing, but you should get the point.

i think it should be more like this:
CGRectMake(redCircle.position.x - 20.0, redCircle.position.y - 20.0, 40.0, 40.0);


hope that helps!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement