Ymines
Ymines

Reputation: 1

Flash as3: Making ennemies with the same IA move differently from each other

So I'm making a game (at least trying to) and I have some ennemies that I spawn at a random position, and then I put them in an array. Then a function in a loop enterframe make the ennemies in that array move randomly around the map. So their movements are random, but they are all sync so they make the same patern of moves at the same time. I'm trying tu figure out how to make them move differently, or at least spawn with a random delay so that they are not all sync. That might be a basic question but I'm trying to make a game with what I currently understand and am able to explain, so I would really appreciate your explanations and advices.

Here is my code (with some of my french notes in it sorry about that, but i need to remember stuff so I can explain them in the test :d)

So the ennemies are the Dementors, from a movieclip "DementorAllSprite" which contains sprites of the ennemy facing different directions

Here is the code out of the loop enterframe

//set up var Dementor and array
//duration: random number between 0 et 150
//Facing: random number arrondi en dessous, donc entre 0 et 3
var DementorTimer = 0;
var DementorDuration = Math.random() * 150;
var DementorFacing: Number = Math.floor(Math.random() * 4);
var DementorSpeed: Number = 13;
var enemies_arr:Array = [];

Here is the loop function

PlayContainer.addEventListener(Event.ENTER_FRAME, PlayLoop);

function PlayLoop(loopEvent:Event):void

{

    addDementor();
    moveDementor();

    function addDementor():void
    {
        //max number of ennemies
        if(enemies_arr.length < 20)
        {
            //add le dementor if conditions check
            var Dementor:DementorAllSprite = new DementorAllSprite();

            //positions random on a grass container
            var startX:int = Math.random() * 5760;
            var startY:int = Math.random() * 3600 ;
            Dementor.x = startX;
            Dementor.y = startY;

            //add Dementor to grass container and set their transparency (they gain alpha when they hit the Player )
            GrassContainer.addChild(Dementor);
            Dementor.alpha=0.4;

            //store the object in an array
            enemies_arr.push(Dementor);
        }
    }


    //---Mouvements Dementors---//
    //Timer = 0, Duration entre 0 et 25, chaque loop rajoute 1 au timer (DementorTimer ++;) 
    //jusqu'a ce que le if ne match plus, puis reset
    //Facing mvmt: 0= back / 1= front / 2= right / 3= left 
    //Frames Dementor: 1= front / 2= back / 3= left / 4= right 
    //switch = if, else if, else if, .... 

    function moveDementor():void
    {
        //check les dementors de l'array (de 0 a leur nombre)
        for (var j:int = 0; j < enemies_arr.length; j++)
        {
            if (DementorTimer < DementorDuration)
            {
                switch (DementorFacing)
                {
                    case 0 :
                    enemies_arr[j].y-=DementorSpeed;
                    enemies_arr[j].gotoAndStop(2)
                    break;

                    case 1 :
                    enemies_arr[j].y+=DementorSpeed;
                    enemies_arr[j].gotoAndStop(1)
                    break;

                    case 2 :
                    enemies_arr[j].x+=DementorSpeed;
                    enemies_arr[j].gotoAndStop(4)
                    break;

                    case 3 :
                    enemies_arr[j].x-=DementorSpeed;
                    enemies_arr[j].gotoAndStop(3)
                }
                DementorTimer++;
            }
            //reset
            else 
            {
                DementorDuration = Math.random() * 150;
                DementorFacing = Math.floor(Math.random() * 4);
                DementorTimer = 0;
            }
        }
    }   

}

Also the dementors movement are pretty short since i put them in an array (originally there was only 1 and he did move like a lot before changing direction, now they change position pretty fast, i augmented the Duration to 150 ( it was way down before) and there a was a little change, but that's still weird)

Anyway thanks for your help and your attention

Upvotes: 0

Views: 42

Answers (1)

Organis
Organis

Reputation: 7316

Well, I guess you need a few tweaks and changes.

var DList:Array = ["up", "down", "left", "right"];
var DHash:Object =
{
    "up":    {"frame":2, "x": 0, "y":-1},
    "down":  {"frame":1, "x": 0, "y": 1},
    "left":  {"frame":3, "x":-1, "y": 0},
    "right": {"frame":4, "x": 1, "y": 0}
}

// Decides how many steps Dementor should take before next reset.
function resetDementor(target:DementorAllSprite):void
{
    target.stepsLeft = int(10 + 10 * Math.random());
}

// Turns Dementor to a random direction.
function randomDirection(target:DementorAllSprite):void
{
    target.direction = DList[int(DList.length * Math.random())];
    target.gotoAndStop(DHash[target.direction]['frame']);
}

PlayContainer.addEventListener(Event.ENTER_FRAME, PlayLoop);

// First of all, for goodness sake, don't define functions inside functions.
function PlayLoop(e:Event):void
{
    if (enemies_arr.length < 20) addDementor();

    moveDementor();
}

function addDementor():void
{
    //add le dementor if conditions check
    var Dementor:DementorAllSprite = new DementorAllSprite();

    //positions random on a grass container
    var startX:int = Math.random() * 5760;
    var startY:int = Math.random() * 3600 ;
    Dementor.x = startX;
    Dementor.y = startY;

    // I guess DementorAllSprite is a MovieClip.
    // Otherwise you need to add stepsLeft:int and direction:String to the class definition.
    randomDirection(Dementor);
    resetDementor(Dementor);

    //add Dementor to grass container and set their transparency (they gain alpha when they hit the Player )
    GrassContainer.addChild(Dementor);
    Dementor.alpha=0.4;

    //store the object in an array
    enemies_arr.push(Dementor);
}

function moveDementor():void
{
    //check les dementors de l'array (de 0 a leur nombre)
    for (var j:int = 0; j < enemies_arr.length; j++)
    {
        // Get the reference to Dementor.
        var Dementor:DementorAllSprite = enemies_arr[j];

        // Check if Dementor needs a reset.
        if (Dementor.stepsLeft < 0)
        {
            randomDirection(Dementor);
            resetDementor(Dementor);
        }

        // Get the directions object with regard to Dementor's direction.
        var aDir:Object = DHash[Dementor.direction];

        // Move the Dementor.
        Dementor.x += aDir['x'] * DementorSpeed;
        Dementor.y += aDir['y'] * DementorSpeed;

        // Reduce the number of steps to go.
        Dementor.stepsLeft--;
    }
}

Upvotes: 1

Related Questions