A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [AS3] Arrgghhh Movement

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    30

    [AS3] Arrgghhh Movement

    hey, I am new to AS3, and i am trying to get the character to move (yh i know i am a noob), ive got the character to move in all four directions using the arrow keys but i want him to play a animation while his walking for example when i press the right arrow key, he moves to the right while doing a walking animation...currently when he moves he stops at the start of the walking animation frame

    PHP Code:
    package code 

        
    import flash.display.MovieClip
        
    import flash.events.Event
        
    import flash.events.KeyboardEvent
        
    import flash.ui.Keyboard

        public class 
    GameEngine extends MovieClip 
        


            public var 
    up:Boolean false
            public var 
    down:Boolean false
            public var 
    left:Boolean false
            public var 
    right:Boolean false
            public var 
    speed:Number 5

            public function 
    GameEngine() 
            { 
                
    stage.addEventListener(KeyboardEvent.KEY_DOWNOnPress); 
                
    stage.addEventListener(KeyboardEvent.KEY_UPOnRelease); 
                
    stage.addEventListener(Event.ENTER_FRAMEOnEnterFrame); 
            } 
             
            public function 
    OnEnterFrame(event:Event):void 
            

                
    // Move up, down, left, or right 
                // if(left is true and right is not true) 
                
    if ( left) { 
                    
    player_mc.-= speed
                    
    player_mc.scaleX = -1
                    
    player_mc.gotoAndStop("stand_forward"); 
                } 
                
    // if(right is true and left is not true) 
                
    if( right ) { 
                    
    player_mc.+= speed
                    
    player_mc.scaleX 1
                    
    player_mc.gotoAndStop("stand_forward"); 
                } 
                
    // if(up is true and down is not true) 
                
    if( up ) { 
                    
    player_mc.-= speed
                    
    player_mc.gotoAndStop("stand_forward"); 
                } 
                
    // if(down is true and up is not true) 
                
    if( down) { 
                    
    player_mc.+= speed
                    
    player_mc.gotoAndStop("stand_forward"); 
                } 
                 
                if(!
    left && !right && !up && !down) { 
                    
    player_mc.gotoAndStop("stand"
                } 
            } 
             
            
    //this is the function the KeyboardEvent.KEY_DOWN listener uses 
            
    public function OnPress(event:KeyboardEvent):void 
            

         
                switch( 
    event.keyCode 
                { 
                    case 
    Keyboard.UP
                        
    up true
                        break; 
                    case 
    Keyboard.DOWN
                        
    down true
                        break; 
                    case 
    Keyboard.LEFT
                        
    left true
                        break; 
                    case 
    Keyboard.RIGHT
                        
    right true
                        break; 
                    default: 
                        break; 
                } 
            } 
            
    //this is the function the KeyboardEvent.KEY_UP listener uses 
            
    public function OnRelease(event:KeyboardEvent):void 
            

                switch( 
    event.keyCode 
                { 
                    case 
    Keyboard.UP
                        
    up false
                        break; 
                    case 
    Keyboard.DOWN
                        
    down false
                        break; 
                    case 
    Keyboard.LEFT
                        
    left false
                        break; 
                    case 
    Keyboard.RIGHT
                        
    right false
                        break; 
                    default: 
                        break; 
                } 
            } 
        } 

    //the end 
    thank you in advance

  2. #2
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Make the walking animation into a movieclip and put it on the stand_forward keyframe
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  3. #3
    Member
    Join Date
    Jun 2009
    Posts
    30
    urmm i think its already like that...but ive attached the file so you can have a look
    Attached Files Attached Files

  4. #4
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    stand_forward mc isn't "animated"! It contains only one image, what do you expect it to do?

    Add in the rest of the gif frames you have there
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  5. #5
    Member
    Join Date
    Jun 2009
    Posts
    30
    im not sure what you mean, sorry...within the terrymc, there is two positions, the first one is called stand and the second stand_forward....if you double-click into any of them movieclips placed in the keyframe, there will be a animation within that mc

  6. #6
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    ah looks like I missed it somehow....

    Anyway, replace all
    player_mc.gotoAndStop("stand_forward");

    with

    Code:
    if(player_mc.currentFrame!="stand_forward")
    				{
    					player_mc.gotoAndStop("stand_forward");
    				}
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  7. #7
    Member
    Join Date
    Jun 2009
    Posts
    30
    ive tried that...now ive got a new problem, the code seems to be generating a error

    #1176: Comparison between a value with static type String and a possibly unrelated type int.

    is there a better way of doing what i want?, as this way to me seems problematic.

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    // Move up, down, left, or right
    // if(left is true and right is not true)
    if (left) {
    	player_mc.x-=speed;
    	player_mc.scaleX=-1;
    	if (player_mc.currentLabel!="stand_forward") {
    		player_mc.gotoAndStop("stand_forward");
    	}
    }
    // if(right is true and left is not true)
    if (right) {
    	player_mc.x+=speed;
    	player_mc.scaleX=1;
    	if (player_mc.currentLabel!="stand_forward") {
    		player_mc.gotoAndStop("stand_forward");
    	}
    }
    // if(up is true and down is not true)
    if (up) {
    	player_mc.y-=speed;
    	if (player_mc.currentLabel!="stand_forward") {
    		player_mc.gotoAndStop("stand_forward");
    	}
    }
    // if(down is true and up is not true)
    if (down) {
    	player_mc.y+=speed;
    	if (player_mc.currentLabel!="stand_forward") {
    		player_mc.gotoAndStop("stand_forward");
    	}
    }
    
    if (! left&&! right&&! up&&! down) {
    	if (player_mc.currentLabel!="stand") {
    		player_mc.gotoAndStop("stand");
    	}
    }

  9. #9
    Member
    Join Date
    Jun 2009
    Posts
    30
    it works!, thank you soooo much!! ...back to my other point, is there a easier way of implementing this...also how would i go around making a double key press feature e.g. the player presses right arrow twice and the character runs

  10. #10
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    package code{
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	import flash.events.KeyboardEvent;
    	import flash.ui.Keyboard;
    
    	public class GameEngine extends MovieClip {
    
    		public var up:Boolean=false;
    		public var down:Boolean=false;
    		public var left:Boolean=false;
    		public var right:Boolean=false;
    		public var speed:Number=5;
    		public var bSpeedVal:int=5;
    		private var bSpeed:int=0;
    		private var fLabel:String;
    		private var kLast:int=0;
    
    		public function GameEngine() {
    			stage.addEventListener(KeyboardEvent.KEY_DOWN, OnPress);
    			stage.addEventListener(KeyboardEvent.KEY_UP, OnRelease);
    			stage.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
    		}
    
    		public function OnEnterFrame(event:Event):void {
    			// Move up, down, left, or right
    			if (left) {
    				player_mc.x-=speed;
    				player_mc.scaleX=-1;
    				fLabel="stand_forward";
    			} else if (right) {
    				player_mc.x+=(speed + bSpeed);
    				player_mc.scaleX=1;
    				// need to add frame label and movie clip for 'run_forward'
    				//fLabel= (bSpeed == 0) ? "stand_forward" : "run_forward";
    				fLabel="stand_forward";
    			}
    
    			if (up) {
    				player_mc.y-=speed;
    				fLabel="stand_forward";
    			} else if (down) {
    				player_mc.y+=speed;
    				fLabel="stand_forward";
    			}
    
    			if (! left&&! right&&! up&&! down) {
    				fLabel="stand";
    			}
    			if (player_mc.currentLabel!=fLabel) {
    				player_mc.gotoAndStop(fLabel);
    			}
    		}
    
    		//this is the function the KeyboardEvent.KEY_DOWN listener uses
    		public function OnPress(event:KeyboardEvent):void {
    			bSpeed=0;
    			switch ( event.keyCode ) {
    				case Keyboard.UP :
    					up=true;
    					break;
    				case Keyboard.DOWN :
    					down=true;
    					break;
    				case Keyboard.LEFT :
    					left=true;
    					break;
    				case Keyboard.RIGHT :
    					bSpeed = (kLast == Keyboard.RIGHT) ? bSpeedVal : 0;
    					right=true;
    					break;
    				default :
    					break;
    			}
    		}
    		//this is the function the KeyboardEvent.KEY_UP listener uses
    		public function OnRelease(event:KeyboardEvent):void {
    			kLast=event.keyCode;
    			switch ( event.keyCode ) {
    				case Keyboard.UP :
    					up=false;
    					break;
    				case Keyboard.DOWN :
    					down=false;
    					break;
    				case Keyboard.LEFT :
    					left=false;
    					break;
    				case Keyboard.RIGHT :
    					right=false;
    					break;
    				default :
    					break;
    			}
    		}
    	}
    }
    //the end

  11. #11
    Member
    Join Date
    Jun 2009
    Posts
    30
    would it be more better to use a timer?, e.g. you the user presses the right key and the timer checks to see if the key is pressed within say 2 seconds or sumthing, if this condition is true then the player runs...thank you yet again dawsonk, however when i press the right key, then stop and then press the right key, the player is still running

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