Quote:
Originally Posted by .mb
Yes, I know that. The problem is that I want to be able to create all these with the same name, for easy handling with collision and stuff, but I don't know how to target one specific instance of newBall.
|
"for(newBall in enemies)" is a loop - it loops through all of the enemies, one at a time, and the code between the brackets changes their position.
You need to do something like this - loop through the enemies and find the one closest to where the touch began. That's your selected enemy.
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
float foundDistance=1000;
for(checkEnemy in enemies)
{
distanceToEnemy = //pythagorean theorem here (distance btwn two points)
if (distanceToEnemy<foundDistance){
selectedEnemy = checkEnemy;
foundDistance = distanceToEnemy;
}
}
}
"selectedEnemy" should be declared in your .h file, so you can use it in the other two touch methods. The other two touch methods should just set selectedEnemy.center.
How does that sound?