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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 04-03-2009, 12:06 PM   #44 (permalink)
smasher
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by .mb View Post
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?
__________________

Free Games!
smasher is offline   Reply With Quote
 

» Advertisements
» Online Users: 284
21 members and 263 guests
ADY, apatsufas, BdR, catedawn, Duncan C, F_Bryant, Graham, Gurpartap Singh, iDifferent, iph_s, jorge599, kampftrinker, MarkC, mer10, Naughty_Ottsel, RoryHarvey, sadr1an1, superg, syver, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,876
Threads: 89,225
Posts: 380,700
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jorge599
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:20 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.