Groups | Blog | Home
all groups > flash actionscript > july 2006 >

flash actionscript : Moving Characters


gregmax17
7/26/2006 10:04:55 PM
Hello, I am trying to make several computer characters move around the screen.
I am successful in making them walk back and forth between two locations, but
it triggers all characters at once. I am asking if anyone knows what I can do
to the script provided below to make them turn around and walk back at
different 'times' (I guess that is the word to use). here is the script I have
so far:

function turnAround(num) {
villager_speed = -.3; //ameks the characters walk to the right
setInterval(walkAround, 4000);
this['villager_'+num].gotoAndStop(2);
}

function walkAround(num) {
villager_speed = .3; //make them walk to the left
setInterval(turnAround, 4000, num); //after 4 seconds turn around and walk
the other direction
this['villager_'+num].onEnterFrame = function() {
this._x -= villager_speed;
}
}

for(var v = 1; v <= 2; v++) { //just two charactesr at the moment
walkAround(v);
}

stop();

Any help on making them turn around at any random time would be appreciated.
Also, the characters have two frames, one looking left and the other looking
right. I have this:

this['villager_'+num].gotoAndStop(2);

to make them face right when they walk to the right, but it doesn't work...?
Please help me with this. Thanks
kglad
7/26/2006 10:59:27 PM
gregmax17
7/26/2006 11:17:14 PM
Ok, this is what I have now:

function turnAround() {
villager_speed = -.3;
turn = setInterval(walkAround, 2000);
look = 'right';
}

function walkAround(num) {
villager_speed = .3;
look = 'left';
setInterval(turnAround, 2000);
clearInterval(turn);
this['villager_'+num].onEnterFrame = function() {
if(look == 'right') { this.gotoAndStop(2); } else { this.gotoAndStop(1); }
this._x -= villager_speed;
}
}


walkAround(1);
walkAround(2);

stop();

But after a minute (estimating), the villagers stop listening to the function
and start flipping random directions. Also, how do you make it so it will make
each villager turn at a random time? Thanks
kglad
7/27/2006 12:18:51 AM
this will introduce some randomness:



mc = this;
function directionAndSpeed(num) {
clearInterval(mc['villager_'+num].moveI);
mc['villager_'+num].moveI = setInterval(directionAndSpeed,
Math.round(4000*Math.random()), num);
ranNum = Math.round(Math.random());
mc['villager_'+num].look = lookA[ranNum];
mc['villager_'+num].villager_speed = .6*Math.random()*(1-2*ranNum);
}
lookA = ["left", "right"];
ranNum = Math.round(Math.random());
mc['villager_'+1].look = lookA[ranNum];
mc['villager_'+1].villager_speed = .6*Math.random()*(1-2*ranNum);
ranNum = Math.round(Math.random());
mc['villager_'+2].look = lookA[ranNum];
mc['villager_'+2].villager_speed = .6*Math.random()*(1-2*ranNum);
directionAndSpeed(1);
mc['villager_'+2].moveI = setInterval(directionAndSpeed, 2000, 2);
this.onEnterFrame = function() {
for (var v = 1; v<=2; v++) {
if (mc['villager_'+v].look == 'right') {
mc['villager_'+v].gotoAndStop(2);
} else {
mc['villager_'+v].gotoAndStop(1);
}
mc['villager_'+v]._x -= mc['villager_'+v].villager_speed;
}
};
AddThis Social Bookmark Button