A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] platform game, can't get enemies to face hero

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    5

    resolved [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;
    }

  2. #2
    Member
    Join Date
    Sep 2010
    Posts
    37
    try removing the "else", and see if that works.

  3. #3
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    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.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    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++;
                }
            }

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center