character movement and npcs
Hi, I am trying to create a working npc that can wonder around and that you can walk up to and speak to. The problem I have run into is that when you talk to the npc and finish he doesn't continue to wonder around. Also when you talk to him you are still able to pick a direction in which you want to move but you don't actually move. That's not a big deal because I can put a flag that stops all actions in the player function.
So, how do I get this 'npc' to be able to pause and then resume?
Actionscript Code:
initGuy(npc001);
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(field[j].mc._y);
}
}
function moveGuy() {
var mc = this;
if (_level0.container_mc.player.hittest2.hitTest(mc.testhit)) {
if (Key.isDown(Key.ENTER) && _level0.playerhalt != true) {
_level0.playerhalt = true;
_level0.passtarget = mc;
trace (_level0.passtarget);
mc.halted = true;
mc.halted2 = true;
attachMovie("sys_speech", "sys_speech01", 3006, {_x:mc._x+88, _y:mc._y-30});
if (_level0.nolta_npc001f == true) {
_level0.msg01 = "Get the flash light!!!!";
} else {
_level0.nolta_npc001f = true;
_level0.msg01 = "Help! I droped the percasets!!";
}
//level0.msg._visible = true; doesn't exist yet
//_level0.msgvar = "You have opened a chest, and pear inside...";
}
}
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;
var tmp;
for (var i in field) {
other = field[i].mc;
if (other != mc) {
if (other.hitTest(mc.testhit)) {
mc.halted = true;
mc.xspeed = 0;
mc.yspeed = 0;
mc.goframe = ((mc.goframe>4) ? mc.goframe-4 : mc.goframe);
mc.gotoAndStop(mc.goframe);
}
}
}
}
}
}
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) {
if (!mc.halted) {
var maxspeed = 1;
var mindelay = 100;
var delayvariance = 3000;
mc.xspeed = 0;
mc.yspeed = 0;
var xy = (Math.floor(Math.random()*3));
if (xy == 1) {
mc.xspeed = 1;
mc.yspeed = 0;
mc.goframe = (mc.xspeed<0) ? 4 : 2;
} else if (xy == 2) {
mc.xspeed = 0;
mc.yspeed = 1;
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);
}
}
This below code is inside the movie clip 'sys_messege'. basicly, a msg fades in and then it types the msg (while paused obn a frame using a type writer script. then you press a btn and it fades out. on the last frame this code is there, to allow the npc to move around. Unfortunitly the change direction of the npc isn't running for some reason.
Actionscript Code:
_level0.playerhalt = false;
if (_level0.passtarget == "_level0.container_mc.npc001"){
_level0.container_mc.npc001.halted = false;
}
removeMovieClip(_level0.container_mc.sys_speech01);
gotoAndStop(1);
it's probably fairly simple, any suggestions? (Also some times when you are walking towards the npc and he is walking toward you, you both glitch into each other. I didn't fully write the code so it's not 100% clear to me, only about 95 XD)
Any help would be greatly appreciated.