PDA

View Full Version : Enemy AI



Dhryn
January 27th, 2007, 11:50 PM
I wanted to make several different types of AI for enemies on my game, and I wanted to store them in an array so that I can call them when needed easily from the same function.

Is there anyway that I can store and use them from an array. I have tryed this but nothing has happened. This is what I tryed.

array = [func1(),func2(),func3()];
function makeEnemy(){
//enemy code
array[1]();
}
function func1(){
trace("running function 1");
}
function func2(){
trace("running function 2");
}
function func3(){
trace("running function 3");
}


Any help with this would be great.

Dhryn

SacrificialLamb
January 28th, 2007, 12:13 AM
I don't really know why this works and the "var func1:Function" is some thing from a TheCanadian post the other day and had never seen before so I copied it (but it's not necessary). The array has to be defined after the functions or it will not work



function makeEnemy() {
//enemy code
array[1]();
}
//function func1() {
var func1:Function = function ():Void {
trace("running function 1");
}
var func2:Function = function ():Void {
trace("running function 2");
}
var func3:Function = function ():Void {
trace("running function 3");
}
/*onEnterFrame = function() {
array[1]()
//trace(array.length)
}*/
var array:Array = [func1, func2, func3];
makeEnemy()

Joppe
January 28th, 2007, 11:45 AM
Nah, SacrificialLamb is wrong. The only thing you've done wrong is to have


array = [func1(),func2(),func3()];

Instead of having


array = [func1,func2,func3];

So taking your code and just removing the parentheses


array = [func1,func2,func3];
function makeEnemy(){
//enemy code
array[1]();
}
function func1(){
trace("running function 1");
}
function func2(){
trace("running function 2");
}
function func3(){
trace("running function 3");
}
makeEnemy();

SacrificialLamb
January 28th, 2007, 02:35 PM
with the code I had (i.e. that var function format) you do have to have the array at the end, I just did not try it with other formats

Dhryn
January 28th, 2007, 02:55 PM
Thankyou very much to the both of you, this has solved my problem.

Cant believe it was something so simple, but I am glad it has been solved.

Thanks again