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.
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 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.
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 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:
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.
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...
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???