I am trying to randomly call one of the below mentioned function every one second. here is the code that I have.
counter=Math.floor(1+Math.random()*3); // genertate random number from 1 to 3
setTimeout("startenemy"+counter, 1000); // call one function with a delay of one second
function startenemy1()
{
trace("1");
enemy1.gotoAndPlay(2);
}
function startenemy2()
{
trace("2");
enemy2.gotoAndPlay(2);
}
function startenemy3()
{
trace("3");
enemy3.gotoAndPlay(2);
}
The setTimeout function is not working for me in this scenario.
But if I use a very primitive way of using setTimeout, it works just fine.
setTimeout(startenemy1, 1000); // call one function with a delay of one second
function startenemy1()
{
trace("1");
enemy1.gotoAndPlay(2);
}
this implementation works just fine. So I know that I am using the setTimeout function with incorrect arguments.
Please let me know if there is another way to make this happen.
You could use the bracket syntax to obtain a reference to the function:
But it would be more efficient to have a single method that starts an anonymous MovieClip, and reference the enemy movie clips dynamically: