A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Create a routine.

  1. #1
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976

    Create a routine.

    Would anyone be able to help me with how to create a routine for a movieclip.

    What i mean by routine is the things a mc has to do before its finished. I can code this by doing it step by step but that will take ages. What i want is. For the mc to move from side to side.

    Eg
    step1 = move right till its at a certain point.
    step2 = wait here.
    step3 = move left till its at a certian point.
    step4 = move right till its at a certian point.
    step5 = wait here.
    step6 = move right till its at a certian point.

    And so on. So what i want to know is how would i do this. I have considered a function. to move the mc. Which calls its next destination from an array. But i wouldnt know how to write it properly.

    My thoughts are the in the arry have to _x cordinantes for each move.

    eg (205, 400, 21, 16) ...

    and in the function

    function move(){

    // call the co-ordinates.And move the mc to that co-ordinante.

    }





    ThankS In AdvancE
    Last edited by hooligan2001; 01-29-2003 at 05:56 AM.

  2. #2
    onClipEvent (enterFrame)
    Join Date
    Sep 2002
    Location
    The Outer Rim
    Posts
    104
    There are probably a thousand and one soloutions to this, ranging from multidimensional arrays to OO-arrays, and some soloutions might even include a dash of black-magic . But my first qad (Quick And Dirty) thought is:

    An array for "actions" 1=move to x,y 2=wait for n frames/secs/whatever
    Another array for values; If "action"=1 move to x,y over an animation sequence have a function perform the action over the amount of frames/secs/whatever it will take, when that action is done, go on read the next value from the "action" array and so forth...

    HTH
    Last edited by mastermute; 01-29-2003 at 08:25 AM.
    The future is no longer what it used to be...

  3. #3
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    This is the reason i need help check out what i am doing atm to solve this problem.
    Code:
    onClipEvent(load) {
            move = 1;
    	speed = 2;
    	
    	move1 = 100;
    	move2 = 300;
    	move3 = 50;
    	move4 = 500;
    	move5 = 120;
    	move6 = 220;
    	move7 = 245;
    	move8 = 78;
    	move9 = 55;
    	move10 = 450;
    }
    And here how i use it

    Code:
    	//move1 = 100;
    if(move == 1) {
    	targetX = move1;
    		 _x -= speed;
    		 if(_x < targetX) {
    			 move = 2;
    		 }
    }
    	//move2 = 300;
    if(move == 2) {
    	targetX = move2;
    		 _x += speed;
    		 if(_x >= targetX) {
    			 move = 3;
    		 }
    }
    	//move3 = 50;
    if(move == 3) {
    	targetX = move3;
    		 _x -= speed;
    		 if(_x <= targetX) {
    			 move = 4;
    		 }
    }
    	//move4 = 500;
    if(move == 4) {
    	targetX = move4;
    		 _x += speed;
    		 if(_x >= targetX) {
    			 move = 5;
    		 }
    }
    	//move5 = 120;
    if(move == 5) {
    	targetX = move5;
    		 _x -= speed;
    		 if(_x <= targetX) {
    			 move = 6;
    		 }
    }
    	//move6 = 220;
    if(move == 6) {
    	targetX = move6;
    		 _x += speed;
    		 if(_x >= targetX) {
    			 move = 7;
    		 }
    }
    	//move7 = 245;
    if(move == 7) {
    	targetX = move7;
    		 _x += speed;
    		 if(_x >= targetX) {
    			 move = 8;
    		 }
    }
    	//move8 = 78;
    if(move == 8) {
    	targetX = move8;
    		 _x -= speed;
    		 if(_x <= targetX) {
    			 move = 9;
    		 }
    }
    	//move9 = 55;
    if(move == 9) {
    	targetX = move9;
    		 _x -= speed;
    		 if(_x <= targetX) {
    			 move = 10;
    		 }
    }
    	//move10 = 450;
    if(move == 10) {
    	targetX = move10;
    		 _x += speed;
    		 if(_x >= targetX) {
    			 move = 11;
    		 }
    }

  4. #4
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    So what i want is a function that can tell wether the old loc is > or < then the new loc and either + the X or minus. once it has done this retrieve the next co-ordinants
    Last edited by hooligan2001; 01-29-2003 at 09:18 AM.

  5. #5
    onClipEvent (enterFrame)
    Join Date
    Sep 2002
    Location
    The Outer Rim
    Posts
    104
    Originally posted by hooligan2001
    So what i want is a function that can tell wether the old loc is > or < then the new loc and either + the X or minus. once it has done this retrieve the next co-ordinants
    For arrays check this out:

    http://www.flashkit.com/tutorials/Ac...-582/index.php

    For eliminating the different routines for - and + then do something like this to solve if you shall go + or -

    if(old_x>new_x){
    dir=-1; //go left (-)
    } else {
    dir=1; //go right (+)
    )


    //movement
    this._x+=speed*dir;

    HTH
    The future is no longer what it used to be...

  6. #6
    383,890,620 polygons nGFX's Avatar
    Join Date
    Oct 2002
    Location
    Germany / Ruhrgebiet
    Posts
    901
    heres a quick and dirty hack:
    PHP Code:
    onClipEvent (load) {

        
    this.WayPoint_array = new Array();
        
    this.WayPoint_array[0] = {x:0y:0w50}; // object with x,y, and w=wait
        
    this.WayPoint_array[1] = {x:100y:0w30};
        
    this.WayPoint_array[2] = {x:100y:100w20};
        
    this.WayPoint_array[3] = {x:0y:100w10};
        
    this.WayPoint_array[4] = {x:50y:50w30};

        
    this.MaxVel 2;
        
    this.WaitCount 0;
        
    this.CurrentWayPoint 0;
        
    this.Ease 5;

    }

    onClipEvent (enterFrame) {

        var 
    DistX Math.abs(this._x this.WayPoint_array[this.CurrentWayPoint].x);
        var 
    DistY Math.abs(this._y this.WayPoint_array[this.CurrentWayPoint].y);

        if ( !((
    DistX <= 0.5) && (DistY <= 0.5)) ) { // if not close enough to target

            // move
            
    var VelX = (this.WayPoint_array[this.CurrentWayPoint].this._x) / this.Ease;
            var 
    VelY = (this.WayPoint_array[this.CurrentWayPoint].this._Y) / this.Ease;

            
    this._x += (VelX MaxVel) ? MaxVel VelX;
            
    this._Y += (VelY MaxVel) ? MaxVel VelY;

            
    _parent.Wait_txt "Moving";

        } else {

            
    // wait
            
    this.WaitCount++;

            
    _parent.Wait_txt "Wait :" + (this.WayPoint_array[this.CurrentWayPoint].this.WaitCount);

            if (
    this.WaitCount == this.WayPoint_array[this.CurrentWayPoint].w) {

                
    // set x,y to rounded vals
                
    this._x this.WayPoint_array[this.CurrentWayPoint].x;
                
    this._Y this.WayPoint_array[this.CurrentWayPoint].y;


                
    // next wp
                
    this.CurrentWayPoint ++;
                
    trace(CurrentWayPoint)
                if (
    this.CurrentWayPoint == this.WayPoint_array.length) { // reached end of waypoints_array
                    
    this.CurrentWayPoint 0// reset

                
    }

                
    this.WaitCount 0// reset counter
                

                
    trace("next wp");
            }

        }


    the fla should work more or less but i think it will give you a pretty good idea how it works

    <olli/>
    Attached Files Attached Files
    Last edited by nGFX; 01-29-2003 at 10:39 AM.

  7. #7
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    Thanks alot for the help nGFX. It just canceled out alot of un-wanted code writing. I really appretiate it.

    And thank you to mastermute.

  8. #8
    383,890,620 polygons nGFX's Avatar
    Join Date
    Oct 2002
    Location
    Germany / Ruhrgebiet
    Posts
    901
    Nice i could help.

    but i made a mistake ...

    there are 2 lines that must be changed in the source in order to work properly:
    PHP Code:
            this._x += (Math.abs(VelX) > MaxVel) ? (Math.sgn(VelX) * MaxVel) : VelX;
            
    this._Y += (Math.abs(VelY) > MaxVel) ? (Math.sgn(VelY) * MaxVel) : VelY
    and this must be added to the main timeline:
    PHP Code:
    // returns +1, -1 or 0 for Values gt 1, lt 1 or ==0
    Math.sgn = function(Value) {
        return ((
    value 0) ? -: (Value == 0) ? 1);

    <olli/>

  9. #9
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    Cool Thanks agian

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