Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

moving enemy to a random available tile

New Here ,
May 26, 2013 May 26, 2013

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);
            }

TOPICS
ActionScript
676
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 26, 2013 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.f

...
Translate
Community Expert ,
May 26, 2013 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);
            }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 26, 2013 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2013 May 26, 2013
LATEST

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);
            }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines