A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Class speed

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

    Class speed

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

    When I run this code I get lots of lag.
    class
    Actionscript Code:
    class Person {
        private var h:Number;
        private var g:Number;
        private var j:Number;
        private var h2:Number;
        private var g2:Number;
        private var j2:Number;
        private var h3:Number;
        private var g3:Number;
        private var j3:Number;
        private var h4:Number;
        private var g4:Number;
        private var j4:Number;
        private var i:Number;
        private var e:Number;
        private var x2:Number;
        private var x3:Number;
        private var y2:Number;
        private var y3:Number;
        private var ran0:Number;
        private var ran1:Number;
        private var ran2:Number;
        private var ran3:Number;
        private var ran4:Number;
        private var ran5:Number;
        private var ran6:Number;
        private var dontrun:Boolean;
        private var npcx:Number;
        private var npcy:Number;
        private var npcgtas:Number;
        private var npcdocon:Boolean;
        private var npcdofcon:Boolean;
        private var objectMaxfdcon:Number;
        private var npcupcon:Boolean;
        private var npcupfcon:Boolean;
        private var objectMaxfucon:Number;
        private var npcricon:Boolean;
        private var npcrifcon:Boolean;
        private var objectMaxfrcon:Number;
        private var npclecon:Boolean;
        private var npclefcon:Boolean;
        private var objectMaxflcon:Number;
        private var pausemovenpc:Boolean;
        private var ires:Number;
        private var ires2:Number;
        private var ires3:Number;
        private var ires4:Number;
        private var dontrunrandisrdir:Boolean;
        private var npcdr:Boolean;
        private var initia:Boolean;
        function Person(dontruni:Boolean, npcxi:Number, npcyi:Number, npcgtasi:Number, objectMaxfdconi:Number, objectMaxfuconi:Number, objectMaxfrconi:Number, objectMaxflconi:Number, pausemovenpcl:Boolean) {
            trace("initiate defults");
            dontrun = dontruni;
            npcx = npcxi;
            npcy = npcyi;
            npcgtas = npcgtasi;
            objectMaxfdcon = objectMaxfdconi;
            objectMaxfucon = objectMaxfuconi;
            objectMaxfrcon = objectMaxfrconi;
            objectMaxflcon = objectMaxflconi;
            pausemovenpc = pausemovenpcl;
            h = 0;
            g = 0;
            j = 0;
            h2 = 0;
            g2 = 0;
            j2 = 0;
            h3 = 0;
            g3 = 0;
            j3 = 0;
            h4 = 0;
            g4 = 0;
            j4 = 0;
            i = 0;
            e = 0;
            x2 = 0;
            x3 = 0;
            y2 = 0;
            y3 = 0;
            ran0 = 0;
            ran1 = 0;
            ran2 = 0;
            ran3 = Math.floor(Math.random()*1);
            ran4 = 0;
            ran5 = 0;
            ires = 0;
            ires2 = 0;
            ires3 = 0;
            ires4 = 0;
        }
        function npcMove(dontruni:Boolean, npcxi:Number, npcyi:Number, npcgtasi:Number, pausemovenpcl:Boolean, npcdrl:Boolean, npcupconl:Boolean, npcriconl:Boolean, npcdoconl:Boolean, npcleconl:Boolean, initial:Boolean):Array {
            initia = initial;
            npcupcon = npcupconl;
            npcricon = npcriconl;
            npcdocon = npcdoconl;
            npclecon = npcleconl;
            trace("npcMove");
            trace(npcx);
            trace(npcy);
            trace("trace:"+initia);
            npcx = npcxi;
            npcy = npcyi;
            npcgtas = npcgtasi;
            dontrun = dontruni;
            pausemovenpc = pausemovenpcl;
            npcdr = npcdrl;
            if (initia == true) {
                if (npcdr != true) {
                    ran1 = Math.floor(Math.random()*4);
                    ran2 = Math.floor(Math.random()*100);
                    ran5 = Math.floor(Math.random()*24);
                    ran2 += 15;
                    ran5 += 6;
                    ran0 = Math.floor(Math.random()*25);
                    x2 = npcx-ran2;
                    y2 = npcy-ran2;
                    x3 = npcx+ran2;
                    y3 = npcy+ran2;
                    dontrun = true;
                    npcdr = true;
                    e = 0;
                }
                if (ran1 == 0) {
                    if (npcy<=y2) {
                        dontrun = false;
                        npcgtas = 1;
                    } else {
                        if (npcupcon != false) {
                            npcgtas = 5;
                            npcy = (npcy -= 1);
                        } else if (npcupcon == false) {
                            y2 = npcy;
                        }
                    }
                }
                if (ran1 == 1) {
                    if (npcx>=x3) {
                        dontrun = false;
                        npcgtas = 2;
                    } else {
                        if (npcricon != false) {
                            npcgtas = 6;
                            npcy = (npcx -= 1);
                        } else if (npcricon == false) {
                            x3 = npcx;
                        }
                    }
                }
                if (ran1 == 2) {
                    if (npcy>=y3) {
                        dontrun = false;
                        npcgtas = 3;
                    } else {
                        if (npcdocon != false) {
                            npcgtas = 7;
                            npcy = (npcy += 1);
                        } else if (npcdocon == false) {
                            y3 = npcy;
                        }
                    }
                }
                if (ran1 == 3) {
                    if (npcx<=x2) {
                        dontrun = false;
                        npcgtas = 4;
                    } else {
                        if (npclecon != false) {
                            npcgtas = 8;
                            npcx = (npcx -= 1);
                        } else if (npclecon == false) {
                            x2 = npcx;
                        }
                    }
                }
                /*3*/
                if (ran1<4) {
                    if (dontrun == false) {
                        ran1 = -1;
                        if (e == ran5) {
                            npcdr = false;
                        }
                        e++;
                    }
                }
                //Updates player positions.
                trace("Return: "+npcx);
                return [dontrun, npcx, npcy, npcgtas, pausemovenpc, npcdr];
            }
        }
    }

    main frame

    Actionscript Code:
    var npc001prop = new Array();
    var npc001prop2 = new Array();
    var initia:Boolean;
    npc001prop[0] = false;
    npc001prop[1] = npc001._x;
    npc001prop[2] = npc001._y;
    npc001prop[3] = 0;
    npc001prop[4] = false;
    npc001prop[5] = false;
    npc001prop[6] = false;
    npc001prop[7] = false;
    npc001prop[8] = false;
    npc001prop[9] = false;
    npc001prop2[0] = false;
    npc001prop2[1] = 1;
    npc001prop2[2] = 1;
    npc001prop2[3] = 1;
    npc001prop2[4] = 1;
    import Person;
    var npc001controler = new Person(npc001prop[0], npc001prop[1], npc001prop[2], npc001prop[3], npc001prop2[0], npc001prop2[1], npc001prop2[2], npc001prop2[3], false);
    initia = false;
    var r:Object = npc001controler.npcMove(dontrun, npcx, npcy, npcgtas, pausemovenpc, npcdr, initia);
    initia = true;
    npc001.onEnterFrame = function() {
        npc001prop = npc001controler.npcMove(npc001prop[0], npc001prop[1], npc001prop[2], npc001prop[3], npc001prop[4], npc001prop[5], npc001prop[6], npc001prop[7], npc001prop[8], npc001prop[9], initia);
        this._x = npc001prop[1];
        this._y = npc001prop[2];
        this.gotoAndStop(npc001prop[3]);
        trace("Don't Run = "+npc001prop[0]);
        trace("\nNpc._x = "+npc001prop[1]);
        trace("\nNpc._y = "+npc001prop[2]);
        trace("\ngotoAndStop = "+npc001prop[3]);
        trace("\nPause Npc Movement = "+npc001prop[4]);
        trace("\nStart a New Direction = "+npc001prop[5]);
    };

    any suggests to achieve the same effect but with a performance boost!

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    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);
    }

  3. #3
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    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.

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

    update

    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.
    Attached Files Attached Files

  5. #5
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    this.attachMovie("fence 01d", "hh_fence_d", 135, {_x:190, _y:135});
    this.attachMovie("fence 01e", "hh_fence_dd", 0, {_x:122, _y:280});
    var field = new Array();
    field.push({mc:hh_fence_d, py:hh_fence_d._y, tp:2});
    field.push({mc:hh_fence_dd, py:hh_fence_dd._y, tp:2});
    
    initGuy(guy1);
    initGuy(guy2);
    initGuy(guy3);
    
    // -----------------
    var speed = 3;
    field.push({mc:player, py:player._y, tp:0});
    
    player.onEnterFrame = function() {
    	var mc = this;
    	if (Key.isDown(Key.getCode())) {
    		var newX = mc._x;
    		var newY = mc._y;
    		switch (Key.getCode()) {
    			case 37 :
    				newX -= speed;
    				mc.goframe = 8;
    				break;
    			case 38 :
    				newY -= speed;
    				mc.goframe = 5;
    				break;
    			case 39 :
    				newX += speed;
    				mc.goframe = 6;
    				break;
    			case 40 :
    				newY += speed;
    				mc.goframe = 7;
    
    				break;
    			default :
    				break;
    		}
    		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.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);
    };
    
    // -----------------
    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;
    			var tmp;
    			for (var i in field) {
    				other = field[i].mc;
    				if (other != mc) {
    					if (other.hitTest(mc._x, mc._y, true)) {
    						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) {
    	var maxspeed = 3;
    	var mindelay = 100;
    	var delayvariance = 3000;
    	mc.xspeed = 0;
    	mc.yspeed = 0;
    	mc.halted = false;
    	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);
    }

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

    hmm

    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.

  7. #7
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    maybe another person can shed some light on this and perhaps with a little explanation. thanks very much

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