Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
I have an app that randomly creates a UIImageView (enemy) at one of three "spawn points." To move them towards their destination (a player on the screen), I used CoreAnimation with things such [UIView beginAnimation...]. Now, though, I've begun to need to implement collision detection. Unfortunately, there is no way to do this between the animated enemy and the player using something like CGRectIntersectsRect(). To solve this, I merely decided to animate this using an NSTimer (calling a function every .01 seconds moving the image forward). Strangely, the image never moves, and the enemy images just pile on top of each other. I've tried numerous methods, but here's the most basic one (which also doesn't work):

Code:
-(void)animateEnemy{
	CGPoint eCenter = enemy.center;
	enemy.center = CGPointMake(eCenter.x, eCenter.y++);
	
	
}

I have an NSTimer in the viewDidLoad method, too:
Code:
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animateEnemy) userInfo:nil repeats:YES];

Any ideas as to what is causing the problem?
 
Code:
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animateEnemy) userInfo:nil repeats:YES];

Try this, if that doesn't fix it, it is the code within the function call.

Code:
[NSTimer scheduledTimerWithTimeInterval:0.1/60 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
 
What happens if you try this?

Code:
-(void)animateEnemy{
	CGPoint eCenter = enemy.center;
	eCenter.y++;
	enemy.center = eCenter;
}
 
Code:
-(void)animateEnemy{
	CGPoint eCenter = enemy.center;
	enemy.center = CGPointMake(eCenter.x,[COLOR="Red"] eCenter.y++[/COLOR]);	
}

The red-hilited code is using the original value of eCenter.y to make a CGPoint. The y value of eCenter is then incremented. Sadly, the post-incremented value isn't used for anything else.

This happens because this is what a postfix ++ does: it evaluates to the current value, then increments the variable.

This should work better:
Code:
enemy.center = CGPointMake(eCenter.x,eCenter.y+1);

Might want to review the ++ and -- operators again, and how postfix vs. prefix placement affects things.
 
Combining the NSTimer solution with changing ++ to +1 seemed to work. Thanks, I'll definitely review my prefix vs. postfix stuff now, as that seemed to make a major difference in this event. Thank you all! I wish I knew other mac programmers I could consult with outside of the internet, as that would help me with problems upfront rather than asking everyone online.
 
One of the most useful things you can do is learn how to use the debugger, and especially how to single-step through lines of code.

If you know how to use the debugger, then many more things become possible. For example, instead of passively wondering why the enemies didn't move, or having to ask someone here, you could look at what's actually happening.

If the enemies aren't animating, set a breakpoint on animateEnemy, then step through what actually happens. You'd be able to see that the enemy.center wasn't changing, even if it wasn't immediately clear why. Maybe you'd change the code to break it down more like PhoneyDeveloper showed. Or more like I showed. Or even if you didn't get an epiphany on y++, you'd still be able to come here and provide all that information as a starting point.

All of this is only possible by learning to use the debugger. And the best part is that you'll then know how to use the debugger in all your other programs, to help solve all those other puzzling bugs you haven't written yet. It's a benefit that keeps on paying off.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.