-
[RESOLVED] platform game, can't get enemies to face hero
I'm making a platform game. The hero can move around, but the enemies are stationary. My problem is that I want the enemies to face the hero. The first enemy behaves correctly, but the subsequent enemies only behave correctly if I kill all the preceding enemies.
I have added the hero and the enemies as Objects and put the enemies into an array called enemies. Both the hero and enemy objects have a direction property and an mc property that refers to their movie clips.
This for loop gets called every frame.
for(var i:int = 0; i < enemies.length; i++)
{
//face hero
if(hero.mc.x < enemies[i].mc.x) enemies[i].direction = -1;
else if(hero.mc.x > enemies[i].mc.x) enemies[i].direction = 1;
enemies[i].mc.scaleX = enemies[i].direction; }
-
try removing the "else", and see if that works.
-
Total Universe Mod
the for loop looks solid so the only thing I can think of is maybe your enemies array doesn't contain what you think it does. Is there a chance that every element in that array points to just one enemy?
Also, if your hero and enemies are in different display objects, there might be a coordinate offset.
-
I tried removing the else. That didn't work. And both the hero and enemies are inside of another movieclip called gamelevel, so I don't think there should be a coordinate offset. Here is where I create the enemy objects and add them to the enemies array. In the .fla file I have the enemies' instance names as enemy1, enemy2, etc.
Actionscript Code:
public function addEnemies() { enemies = new Array(); var i:int = 1; while (true) { if (gamelevel["enemy"+i] == null) break; var enemy = new Object(); enemy.mc = gamelevel["enemy"+i]; enemy.direction = 1; enemies.push(enemy); i++; } }
-
my bad, I had a random return statement in my for loop somewhere.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|