Copy link to clipboard
Copied
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
tileset
options.push(tileset
}
if (options.length > 0){
var enemyPick:int = Math.floor(Math.random()*options.length)
}
}
trace(enemyPick, options);
}
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now