PDA

View Full Version : [AS2] question about progrqamming AI


white-zilla
07-10-2009, 03:16 AM
Hello, i have a question regarding movie clips that have their own individual functions that are controlled by the computer.

let me start by explaining what I wish to do:
I want to make 2 different kinds "enemies" spawn at random at random intervals at one of 9 different points at random. At this moment, I have this much working.

What I want to happen once they spawn is for each individual "enemy" to move around in random directions.

My problem:
my "enemies" seem to be of a hive mind. one will spawn and move how I wish. But when another one spawns from the same spot, they will be in the same loop as the other one, so they will both move perfectly parallel to each other.

I have been following this tutorial.
http://www.tutorialhero.com/click-27540-make_a_turret_game_part_2.php

also here is a snippet of my code. I copied and pasted my problem areas. Right now i'm only working with what I have designated at "spawn location 1"

_root.onEnterFrame=function(){
zombiea=[];//string for enemy type a
zombieb=[];

(imagine a function for random genration)

one=({_x:-11,_y:315})//spawn point one


if(place==0){ //this is an integer returned by my random function
zombie1=attachMovie("bad","bad",_root.getNextHighestDepth(),one);
zombiea.push(_root.bad._name);}

zombie1.onEnterFrame=function(){
if(slow==0){
entor=2;
this._x=this._x+entor;}

if(this._x>=78)
{
trace("arrived");
slow=1;
entor=0;
this._x=this._x+entor;
}
}



any help is greatly appreciated :D

dialectric
07-10-2009, 06:38 PM
Hi,

Random movement is simpler than a true AI. You should be able to get it just by adding more random(#)'s in your code. The problem might be that your zombies should be more distinct from one another, generated from, and stored in, arrays.

Here's how I would do it:


//on start, _root.zombie_count = 0;
//on start, _root.zombie_array = [];

make_zombie=function(){
_root.zombie_count++;
var a_zombie = attachMovie( "zombie","zombie"+zombie_count, depth++);

a_zombie.random_move = function(){
switch(random(4)){
case 0: ghost.mover(-1,0); break;
case 1: ghost.mover(0,1); break;
case 2: ghost.mover(1,0); break;
case 3: ghost.mover(0,-1); break;
default: break;
};
};

a_zombie.mover = function(dx,dy){
//change these vars to set how much the zombie moves
var x_move_distance = 5;
var y_move_distance = 5;
a_zombie._x = a_zombie._x + (dx * x_move_distance);
a_zombie._y = a_zombie._y + (dy * x_move_distance);
};
zombie_array.push(a_zombie)
};

//then, use only 1 enter frame, and loop through the zombie array to move them

onEnterFrame = function(){
for (i = 0; i < zombie_array.length; i++){
zombie_array[i].random_move();
};
};

- dialectric