Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-28-2011, 10:26 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 35
Kenenth is on a distinguished road
Default Help with animations and collision detection?

I am writing a game in objective c and I have come across a problem, which involves collision detection and animations. So basically my game consists of the user firing the gun aimed towards enemies. The actions of the enemies moving and the bullets moving are taken care by animation code. Every 1/12 of a second I check to see if any objects (stored in the bullet array) have collided with any of the enemies (stored in the enemy array), and if they do, then I delete both objects. But, When I delete any of these objects in the arrays due to collision, I guess the iphone perceives that the animation of that object is done, and calls the [animationDidStopSelector], which I do not want to happen. If a bullet and an enemy collide, then I want to release both of them, and if they do not, the bullet is to go to the side of the screen and disappear and the enemy UIImageView changes its image. How can I fix this? Like is there a way to detect if an image's animation has been interrupted and then go about it differently?
Thanks a bunch.
Here is my code:
collision detector
Code:
for (int bulletCounter1=[bulletArray count]-1; bulletCounter1>= 0; bulletCounter1--){
			for (int enemyCounter=[enemyArray count]-1; enemyCounter>= 0; enemyCounter--){
				UIImageView *enemyImg = [enemyArray objectAtIndex:enemyCounter];
				UIImageView *bulletImg = [bulletArray objectAtIndex:bulletCounter1];
				if( CGRectIntersectsRect(bulletImg.frame, enemyImg.frame)) {
					[bulletImg removeFromSuperview];
					[bulletArray removeObjectAtIndex:bulletCounter1];
					bulletImg = nil;
					[enemyImg removeFromSuperview];
					[enemyArray removeObjectAtIndex:enemyCounter];
					enemyImg = nil;
					
					break;
				}
			}
		}

enemy move animation:
Code:
UIImageView *enemy = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"enemy.png"]];
	enemy.frame = CGRectMake(0, 0, 41.5, 81.0);
	int enemyx = random() % 480;
        enemy.center = CGPointMake(enemyx, -40);
	[enemyArray addObject:enemy];
	[self.view addSubview:enemy];
	[UIView beginAnimations:@"enemy move" context:NULL];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(objectMoveFinished:)];
	[UIView setAnimationDuration:3.0];
	[UIView setAnimationCurve:UIViewAnimationCurveLinear];
	enemy.center = CGPointMake(parax, 200);
	[UIView commitAnimations];
	
	[enemy release];
	enemy = nil;
code for the firing bullets method is very similar.
Kenenth is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, collision detection, objective c, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 392
13 members and 379 guests
7twenty7, AppsBlogger, Creativ, Dalia, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0