I am not sure if a class would be the best option but I am using the following code to create a random walking player by using a class that contains it's function. (Soon I will need the class to do hit tests as well).
Sorry, not done as a class, but based off of code from one of your previous posts. Maybe you can adapt it...
Use document set at 30fps.
On the main timeline:
layer1 'as' -- all code below
layer2 'field mcs' -- has: 2 fence movie clips, instance names fence1 and fence2, they are non-walkable obstacles
3 npcs (using your character posted earlier), instance names guy1, guy2, and guy3
layer3 'area mc' -- 1 movie clip, instance name 'area', this mc defines the walkable area, filled shape, but can have open spaces
Code:
var field = new Array();
field.push({mc:fence1, py:fence1._y, tp:2});
field.push({mc:fence2, py:fence2._y, tp:2});
initGuy(guy1);
initGuy(guy2);
initGuy(guy3);
function initGuy(mc) {
mc.xspeed = 1;
mc.yspeed = 1;
mc.goframe = 3;
mc.halted = false;
mc.onEnterFrame = moveGuy;
changedirection(mc);
field.push({mc:mc, py:mc._y, tp:1});
}
function updateDepths() {
for (var i in field) {
field[i].py = field[i].mc._y;
}
field.sortOn("py",Array.NUMERIC);
for (var j = 0; j<field.length; j++) {
field[j].mc.swapDepths(j);
}
}
function moveGuy() {
var mc = this;
if (!mc.halted) {
mc._x += mc.xspeed;
mc._y += mc.yspeed;
updateDepths();
if (!area.hitTest(mc._x, mc._y, true)) {
reverseGuy(mc);
} else {
var other;
for (var i in field) {
other = field[i].mc;
if (other != mc) {
if (other.hitTest(mc._x, mc._y, true)) {
reverseGuy(mc);
}
}
}
}
}
}
function reverseGuy(mc) {
if (mc.xspeed != 0) {
mc.xspeed = 0-mc.xspeed;
mc.goframe = (mc.xspeed<0) ? mc.goframe+2 : mc.goframe-2;
mc.gotoAndStop(mc.goframe);
mc._x += mc.xspeed;
} else if (mc.yspeed != 0) {
mc.yspeed = 0-mc.yspeed;
mc.goframe = (mc.yspeed<0) ? mc.goframe-2 : mc.goframe+2;
mc.gotoAndStop(mc.goframe);
mc._y += mc.yspeed;
updateDepths();
}
}
function changedirection(mc) {
var maxspeed = 3;
var mindelay = 100;
var delayvariance = 3000;
mc.xspeed = 0;
mc.yspeed = 0;
var xy = (Math.floor(Math.random()*3));
if (xy == 1) {
mc.xspeed = Math.floor(maxspeed-(Math.random()*2*maxspeed));
mc.yspeed = 0;
mc.goframe = (mc.xspeed<0) ? 4 : 2;
} else if (xy == 2) {
mc.xspeed = 0;
mc.yspeed = Math.floor(maxspeed-(Math.random()*2*maxspeed));
mc.goframe = (mc.yspeed<0) ? 1 : 3;
}
mc.goframe = (mc.xspeed == 0 && mc.yspeed == 0) ? ((mc.goframe>4) ? mc.goframe-4 : mc.goframe) : mc.goframe+4;
mc.gotoAndStop(mc.goframe);
var delay = Math.floor(Math.random()*delayvariance)+mindelay;
mc.chnge = setTimeout(changedirection, delay, mc);
}
Thank you very much for the response.
This works pretty well.
There is some things I noticed. That I am sure I can fix once I get to it. One was that when the npc stops he doesn't go to a stop position (frames 1234), instead he pauses on one of the walking frames. and when he stops he stays facing the way he was looking, i think that should be random. and when one npc touches the other npc they start changing direction rapidly.And they can not go behind the fence is one big hit test, what if there was clips around the fence that were the "fence1, fence2" clips?
Last edited by brickhouse420; 03-02-2010 at 02:20 PM.
well i seemed to fix most of the above problems. I have added a player and it seems like every thing works, except if you get to close to the npc (when the proxys of the player and npc are touching) he starts to keep switching directions, and if you edge him into a wall or another npc he keeps switching directions too, or he will walk into you change direction walk into the fence and repeat .. I am not to sure how to avoid this except for maybe have another hit test (that is a bit larger of the npcs current hit test) that determines if the path will be walk able (4 hit tests one for all sides). But I am not sure if this is the best way. I have attached my files, maybe some one can give me some insight, id really appreciate it since i seem to be having a challenge.
not bad.
It definitely is improved but still a bit confusing for me.
As I have been messing around with it, I noticed that if the npc walks toward the player and you walk toward the npc you get stuck (halt = true) but its never untrue until the npc moves. And the x and y seems to be a bit buggy because when you walk down you can't get as close as walking up or right. It's a little awkward. And sometimes the npc runs right at you and freezes you. I understand most of the code but when it gets to shape hit tests that's where I get lost. If you test the fla with your code you may notice what I am speaking of
Thanks for your helps so far you have definitely provided me with steps to the right direction and I am going to continue to mess with it and see what I can come up with.