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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-21-2011, 11:27 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 7
noahGolm is on a distinguished road
Default Moving a UIImageView to another object's location

Okay, I'm working on an app where, as part of the challenge, the enemy goes to where the player is when it is spawned. Originally, I was moving it using a CoreAnimation tactic, (i.e. "UIView beginAnimation...") but now I need to implement collision detection. Since things like CGRectIntersectsRect() don't work with CoreAnimation, I figured that I have to use an NSTimer to animate the enemy. Somehow, I need to move the image along a line ( I could calculate the slope and y-intercept). Any suggestions? Here's some code samples if this helps....
Code:
-(void)addEnemyAtX:(int)x y:(int)y{
	UIImage *enemyImage = [UIImage imageNamed:@"enemy.png"];
	enemy = [[UIImageView alloc]initWithImage:enemyImage];
	enemy.center = CGPointMake(x, y);
	[self.view addSubview:enemy];
	/*[UIView beginAnimations:@"Move" context:nil];
	[UIView setAnimationDuration:3];
	enemy.center = ball.center;
	[UIView commitAnimations];*/
	[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animateEnemy) userInfo:nil repeats:YES];	

}

-(void)animateEnemy{
	//enemy.center = CGPointMake(); This is where the coordinates go
}
I could figure out how to change the Y coordinate using a simple y=mx+b equation, but how to change the X coordinate is what is bothering me.
noahGolm is offline   Reply With Quote
Old 02-22-2011, 09:27 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

This post has some code to calculate the speedx and speedy required to move an imageview toward a target every frame:

http://www.iphonedevsdk.com/forum/ip...tml#post300645
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-22-2011, 09:35 AM   #3 (permalink)
Registered Member
 
missing_no's Avatar
 
Join Date: Feb 2011
Posts: 41
missing_no is on a distinguished road
Default

Quote:
Originally Posted by noahGolm View Post
Okay, I'm working on an app where, as part of the challenge, the enemy goes to where the player is when it is spawned. Originally, I was moving it using a CoreAnimation tactic, (i.e. "UIView beginAnimation...") but now I need to implement collision detection. Since things like CGRectIntersectsRect() don't work with CoreAnimation, I figured that I have to use an NSTimer to animate the enemy. Somehow, I need to move the image along a line ( I could calculate the slope and y-intercept). Any suggestions? Here's some code samples if this helps....
Code:
-(void)addEnemyAtX:(int)x y:(int)y{
	UIImage *enemyImage = [UIImage imageNamed:@"enemy.png"];
	enemy = [[UIImageView alloc]initWithImage:enemyImage];
	enemy.center = CGPointMake(x, y);
	[self.view addSubview:enemy];
	/*[UIView beginAnimations:@"Move" context:nil];
	[UIView setAnimationDuration:3];
	enemy.center = ball.center;
	[UIView commitAnimations];*/
	[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animateEnemy) userInfo:nil repeats:YES];	

}

-(void)animateEnemy{
	//enemy.center = CGPointMake(); This is where the coordinates go
}
I could figure out how to change the Y coordinate using a simple y=mx+b equation, but how to change the X coordinate is what is bothering me.
You don't give many details as to what enemy and player are doing, but I'll assume the player is stationary and the enemies are gravitating torward said player. (so player is in middle somewhere and the enemy needs to gravitate torwards him from all directions)

if(player.center.x > enemy.center.x){
enemy.center = CGPointMake(enemy.center.x++, enemy.center.y);
}
if(player.center.x < enemy.center.x){
enemy.center = CGPointMake(enemy.center.x--, enemy.center.y);
}
if(player.center.y > enemy.center.y){
enemy.center = CGPointMake(enemy.center.x, enemy.center.y++);
}
if(player.center.y < enemy.center.y){
enemy.center = CGPointMake(enemy.center.x, enemy.center.y--);
}

if player is up against one of the sides then you only need to change one coordinate; x or y depending on if the player is fixed on top/bottom or left/right. Change the increment value to slow down or speed up enemy movement.
missing_no is offline   Reply With Quote
Old 02-22-2011, 04:01 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 7
noahGolm is on a distinguished road
Default

Quote:
Originally Posted by missing_no View Post
You don't give many details as to what enemy and player are doing, but I'll assume the player is stationary and the enemies are gravitating torward said player. (so player is in middle somewhere and the enemy needs to gravitate torwards him from all directions)

if(player.center.x > enemy.center.x){
enemy.center = CGPointMake(enemy.center.x++, enemy.center.y);
}
if(player.center.x < enemy.center.x){
enemy.center = CGPointMake(enemy.center.x--, enemy.center.y);
}
if(player.center.y > enemy.center.y){
enemy.center = CGPointMake(enemy.center.x, enemy.center.y++);
}
if(player.center.y < enemy.center.y){
enemy.center = CGPointMake(enemy.center.x, enemy.center.y--);
}

if player is up against one of the sides then you only need to change one coordinate; x or y depending on if the player is fixed on top/bottom or left/right. Change the increment value to slow down or speed up enemy movement.
Well, I have tried that approach. For some reason, none of the enemies are moving! (By the way, there are multiple instances of the enemy moving, by calling a function every second that adds the enemy to a predetermined spawn point). I also tried smasher's approach, but that had the same result. It's very strange! The enemies just pile up on each other, not moving at all...
I used your code, calling an NSTimer in the viewDidLoad as such:
Code:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animateEnemy) userInfo:nil repeats:YES];
noahGolm is offline   Reply With Quote
Old 02-22-2011, 04:07 PM   #5 (permalink)
Registered Member
 
missing_no's Avatar
 
Join Date: Feb 2011
Posts: 41
missing_no is on a distinguished road
Default

if you keep calling that first function then yea they will stack up. I'm hoping you have an array of something that gives each instance a different name and such?

this method works excellent for me (I use it for homing projectiles) so I have definitely tested it in my own games.
missing_no is offline   Reply With Quote
Old 02-22-2011, 04:24 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 7
noahGolm is on a distinguished road
Default

Quote:
Originally Posted by missing_no View Post
if you keep calling that first function then yea they will stack up. I'm hoping you have an array of something that gives each instance a different name and such?

this method works excellent for me (I use it for homing projectiles) so I have definitely tested it in my own games.
Err, I didn't exactly have that. I just tried it though, declaring an NSMutableArray and doing this... Which is not working. This is in the function that creates the enemy every second when called
Code:
UIImage *enemyImage = [UIImage imageNamed:@"enemy.png"];
	enemy = [[UIImageView alloc]initWithImage:enemyImage];
	enemy.center = CGPointMake(x, y);
	//[self.view addSubview:enemy];
	
	[enemyList addObject:enemy]; // enemyList is the array
	position = [enemyList count];
	[self.view addSubview:[enemyList objectAtIndex:position]];
I didn't think this would work, though. Graaah, making multiple enemies is not fun here! I just came off of working with cocos2d, where making these kinds of games is a whole lot easier. I'm at a loss, now...
noahGolm is offline   Reply With Quote
Old 02-22-2011, 05:19 PM   #7 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 7
noahGolm is on a distinguished road
Default

Okay, I don't think handling the images separately, as in each one has its own name, would help this. When looking at a similar game I have made, I was going through basically the same procedure, but that collision worked (the methods of this were different, of course, seeing as it was cocos2d). Also, each of these enemies were moving. I've now found that any form of movement doesn't move. Even if I just make the object move with a timer telling "enemy.center.y++," that doesn't do anything. What is going on???
noahGolm is offline   Reply With Quote
Old 02-23-2011, 12:07 AM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Code:
UIImage *enemyImage = [UIImage imageNamed:@"enemy.png"];
	enemy = [[UIImageView alloc]initWithImage:enemyImage];
	enemy.center = CGPointMake(x, y);
	//[self.view addSubview:enemy];
	
	[enemyList addObject:enemy]; // enemyList is the array
	position = [enemyList count];
	[self.view addSubview:[enemyList objectAtIndex:position]];
That'll probably crash - if you have 5 items it'll try to access objectAtIndex:5 and cause an exception.


Code:
	UIImage *enemyImage = [UIImage imageNamed:@"enemy.png"];
	enemy = [[UIImageView alloc]initWithImage:enemyImage];
	enemy.center = CGPointMake(x, y);
	[self.view addSubview:enemy];
	[enemyList addObject:enemy];
	[enemy release];
That'll work as long as you init'ed enemyList somewhere; probably in the init method of this class.

If it's still not moving post your spawn and move functions.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

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: 424
8 members and 416 guests
chemistry, ChrisYates, hussain1982, Retouchable, skrew88, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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