Skip to main content
Known Participant
May 26, 2013
Answered

moving enemy to a random available tile

  • May 26, 2013
  • 1 reply
  • 708 views

I have an enemy hitting a bunch of tiles, and through a loop, I check all the tiles if the enemy is hitting them. I trace them and some will tell me if it's true or false. Although you can't see tileset, it is an array that is equal to a movieclip that is a tile. It is in a for loop itself, so basically, tileset is 49 movie clips of tile. I want to be able to get the enemy to randomly choose from those that are true, and go on those tiles. It traces all the tiles that are in contact with the enemy, and I push those into another array called options. Below is my timer function, where every 5 second, I want the enemy to move to an available tile. My problem is, I'm not sure how I can get the enemy to move randomly into those available tiles.

function timerenemy (event:TimerEvent) {

                    var options:Array = [];

                    for (var j:int = 0; j < tileset.length; j++){
                        if (tileset.hitTestObject(enemy) && ! tileset.tileMiddle.hitTestObject(player)) {
                            tileset.outline.gotoAndStop("attack");
                            options.push(tileset);
                        }

                        if (options.length > 0){
                            var enemyPick:int = Math.floor(Math.random()*options.length)

                        }

                    }
                    trace(enemyPick, options);
            }

This topic has been closed for replies.
Correct answer kglad

use:

function timerenemy (event:TimerEvent) {

                    var options:Array = [];

                    for (var j:int = 0; j < tileset.length; j++){
                        if (tileset.hitTestObject(enemy) && ! tileset.tileMiddle.hitTestObject(player)) {
                            tileset.outline.gotoAndStop("attack");
                            options.push(tileset);
                        }

                        if (options.length > 0){
                            var enemyPick:int = Math.floor(Math.random()*options.length);

enemy.x=options[enemyPick].x;

enemy.y=options[enemyPick].y;

                        }


                    }
                    trace(enemyPick, options);
            }

1 reply

kglad
Community Expert
Community Expert
May 26, 2013

use:

function timerenemy (event:TimerEvent) {

                    var options:Array = [];

                    for (var j:int = 0; j < tileset.length; j++){
                        if (tileset.hitTestObject(enemy) && ! tileset.tileMiddle.hitTestObject(player)) {
                            tileset.outline.gotoAndStop("attack");
                            options.push(tileset);
                        }

                        if (options.length > 0){
                            var enemyPick:int = Math.floor(Math.random()*options.length)

                        }

enemy.x=options[enemyPick].x;

enemy.y=options[enemyPick].y;


                    }
                    trace(enemyPick, options);
            }

ultraruleAuthor
Known Participant
May 26, 2013

Thanks so much! I am getting error 1010, but when I do trace it, it is recongnizing the individual tiles. It tells me that it is picking a tile out of the available ones, but I don't get why I can trace it and it comes up as undefined...