In my iphone app i need to be able to detect tiuches to drag around different sprites/b2bodys (im not sure which one to use for collision detection), at the moment when i drag them around they all stack on top of each other and im not sure how to detect individual sprites/b2bodys , any help is appreciated, thanks :D
Detecting Touches
(3 posts) (2 voices)-
Posted 5 months ago #
-
You could just use a NSMutableArray to keep track of each sprite and then check for collision before moving the sprite to a new location.
Maybe this will help.
Every Balloon sprite created is added to the _Balloons NSMutableArray and then checked when a touch occurs.
The game can be found here that this code was taken from. http://itunes.apple.com/us/app/balloon/id481173300?ls=1&mt=8
Cheers
Shane
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
if ((!CanTouch)||([hud IsPaused])) {
return;
}UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];NSMutableArray *balloonstoDelete = [[NSMutableArray alloc] init];
for (CCSprite *Balloons in _Balloons) {CGRect BallonRect = CGRectMake(Balloons.position.x - (Balloons.contentSize.width/2),
Balloons.position.y - (Balloons.contentSize.height/2),
Balloons.contentSize.width,
Balloons.contentSize.height);CGRect TouchRect = CGRectMake(location.x,
location.y,
10,
10);if (CGRectIntersectsRect(BallonRect, TouchRect)) {
[balloonstoDelete addObject:Balloons];
}
}int BalloonsTouched = [balloonstoDelete count];
if (BalloonsTouched > 0) {
CanTouch = NO;
}for (Balloon *balloon in balloonstoDelete) {
Balloon *balloon2 = (RedBalloon *)balloon;int CorrectAnswer = (balloon2.q1 + balloon2.q2);
if (CorrectAnswer == balloon2.Answer) {
[self createExplosionCorrectX :balloon2.position.x y:balloon2.position.y];
FoundCorrectAnswer = YES;User *user = [User sharedUser];
[user setOpponentScoreValue:[user getOpponentScoreValue] + 100];
[hud SetScoreLabel:[user getOpponentScoreValue]];
} else{
[self createExplosionIncorrectX :balloon2.position.x y:balloon2.position.y];
}[_Balloons removeObject:balloon];
[self removeChild:balloon cleanup:YES];
}
[balloonstoDelete release];
balloonstoDelete = nil;
}Posted 5 months ago # -
thanks :D
Posted 5 months ago #
Reply
You must log in to post.