A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: character movement and npcs

  1. #1
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    Question 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.

  2. #2
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    This is the player code, it appears after the npc code. this is all on the main frame (except for the sys_messege).

    OH YEA I FORGOT ALL THE CODE HERE IS IN A SWF THAT IS LOADED INTO ANOTHER SWF.

    Actionscript Code:
    _level0.container_mc.player.onEnterFrame = function() {
        var mc = this;
        // make this more like the npc structure
        if (_level0.container_mc.nolta_chest001.hitTest(mc.testhit)) {
            if (Key.isDown(Key.ENTER) && this.halted != true) {
                _level0.playerhalt = true;
                _level0.passtarget = "chest001";
                _level0.container_mc.nolta_chest001.gotoAndStop(2);
                mc.halted = true;
                attachMovie("sys_speech", "sys_speech01", 3006, {_x:125, _y:472});
                if (_level0.nolta_chest001f == true) {
                    _level0.msg01 = "This chest apears to be empty!";
                } else {
                    _level0.nolta_chest001f = true;
                    _level0.msg01 = "This document could contain important information.";
                }
                //level0.msg._visible = true; doesn't exist yet
                //_level0.msgvar = "You have opened a chest, and pear inside...";
            }
        }
        if (Key.isDown(Key.getCode())) {
            var newX = mc._x;
            var newY = mc._y;
            switch (Key.getCode()) {
            case 37 :
                newX -= 3;
                mc.goframe = 8;
                break;
            case 38 :
                newY -= 3;
                mc.goframe = 5;
                break;
            case 39 :
                newX += 3;
                mc.goframe = 6;
                break;
            case 40 :
                newY += 3;
                mc.goframe = 7;
                break;
            default :
                break;
            }
            if (!_level0.playerhalt) {
                mc.halted = false;
            }
            if (!area.hitTest(newX, newY, true)) {
                mc.halted = true;
                mc.goframe = ((mc.goframe>4) ? mc.goframe-4 : mc.goframe);
            } else {
                for (var i in field) {
                    other = field[i].mc;
                    if (other != mc) {
                        if (other.testhit.hitTest(newX, newY, true)) {
                            mc.halted = true;
                            mc.goframe = ((mc.goframe>4) ? mc.goframe-4 : mc.goframe);
                        }
                    }
                }
            }
            if (!mc.halted) {
                mc._x = newX;
                mc._y = newY;
                mc.swapDepths(mc._y);
            }
        } else {
            mc.goframe = ((mc.goframe>4) ? mc.goframe-4 : mc.goframe);
        }
        mc.gotoAndStop(mc.goframe);
    };

  3. #3
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    hmm

    I could really use some help on this, Is there a reason why it was left unanswered?

  4. #4
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    guess not

    Alright fixed. No help to any one here. >_<

    Please Close.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center