dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [Problem] Smoother Movement Control

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    18

    [Problem] Smoother Movement Control

    Hello, The movement controls I am currently using have the desired movement effects, however they do not work if you press more than one key at a time, I need to for example be able to roll and yaw at the same time. Any suggestions on how I can redo this code would be great.

    Here is a snippy

    Code:
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
    			this.stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
    			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    
    		}
    		private function onEnterFrame(e:Event):void
    		{
    			if(keyIsDown){
    					// if the key is still pressed, just keep on moving
    					switch(lastKey){
    						case 87				: fighter.moveForward(10); break;
    						case 83				: fighter.moveBackward(5); break;
    						case 65				: fighter.roll(2); break;
    						case 68				: fighter.roll(-2); break;
    						case Keyboard.UP	: fighter.pitch(2); break;
    						case Keyboard.DOWN	: fighter.pitch(-2); break;
    						case Keyboard.RIGHT : fighter.yaw(2); break;
    						case Keyboard.LEFT  : fighter.yaw(-2); break;			
    					} 
    				}
    			ltarget.transform = fighter.transform;
    			ltarget.moveForward(1000);
    			ltarget.moveDown(100);
    			view.camera.transform = fighter.transform;
    			view.camera.pitch(10);
    			view.camera.moveForward(28);
    			view.camera.moveUp(5);
    			view.render();
    		}
    		private function keyDown(e:KeyboardEvent):void
    		{
    			lastKey = e.keyCode;
    			keyIsDown = true;
    		}
    		private function keyUp(e:KeyboardEvent):void
    		{
    			keyIsDown = false;
    		}
    	}
    }
    Thanks up front

    BTW if you want to see how it is working currently the test is at http://branchdepartment.net/test/World.html

  2. #2
    Senior Member
    Join Date
    May 2009
    Posts
    138
    I use senocular's KeyObject which lets you easily test multiple keys down.

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    18
    Thanks for that but maybe I am doing it wrong. I keep getting type was not found or was not compile time consistant: KeyObject

    i put the file in my src in flash builder. i named it keydown.as

    i did

    import keydown;

    then tried this for the movements

    var key:KeyObject = new KeyObject(stage);
    if(key.isDown(key.w)){fighter.moveForward(10)}
    if(key.isDown(key.s)){fighter.moveBackward(5)}
    if(key.isDown(key.a)){fighter.roll(2)}
    if(key.isDown(key.d)){fighter.roll(-2)}
    if(key.isDown(key.UP)){fighter.pitch(2)}
    if(key.isDown(key.DOWN)){fighter.pitch(-2)}
    if(key.isDown(key.RIGHT)){fighter.yaw(2)}
    if(key.isDown(key.LEFT)){fighter.yaw(-2)}

    I also get call to possibly undefined method KeyObject

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You should not have changed the name of the file. It needs to be the same as the class it defines. Also, it should be in a directory which matches the package structure;
    Code:
    src
      com
        senocular
          utils
            KeyObject.as
    edit: and of course, you should import using that package

    Code:
    import com.senocular.utils.KeyObject;
    Last edited by 5TonsOfFlax; 07-16-2009 at 06:16 PM. Reason: Edit:

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    18
    OK I made some changes, I get no errors, the movie loads, but it does no movement.

    Code:
    		stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    		}
    		private function onEnterFrame(e:Event):void
    		{
    
    			var key:KeyObject = new KeyObject(stage);
    			if(key.isDown(key.w)){fighter.moveForward(10);}
    			if(key.isDown(key.s)){fighter.moveBackward(5);}
    			if(key.isDown(key.a)){fighter.roll(2);}
    			if(key.isDown(key.d)){fighter.roll(-2);}
    			if(key.isDown(key.UP)){fighter.pitch(2);}
    			if(key.isDown(key.DOWN)){fighter.pitch(-2);}
    			if(key.isDown(key.RIGHT)){fighter.yaw(2);}
    			if(key.isDown(key.LEFT)){fighter.yaw(-2);}
    
    			
    			ltarget.transform = fighter.transform;
    			ltarget.moveForward(1000);
    			ltarget.moveDown(100);
    			view.camera.transform = fighter.transform;
    			view.camera.pitch(10);
    			view.camera.moveForward(28);
    			view.camera.moveUp(5);
    			view.render();
    		}

  6. #6
    Senior Member
    Join Date
    May 2009
    Posts
    138
    I think you need to use the ASCII code instead of the letters themselves. There are shortcuts for the arrows (UP, DOWN, etc) and SPACE, but for example to detect A you want to use key.65. ASCII Table.

  7. #7
    Junior Member
    Join Date
    Jul 2009
    Posts
    18
    Ya I finally got it figured out....

    Code:
    		this.stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    		}
    		private function onEnterFrame(e:Event):void
    		{
    
    			if(key.isDown(87)){fighter.moveForward(20);}
    			if(key.isDown(83)){fighter.moveBackward(10);}
    			if(key.isDown(65)){fighter.roll(5);}
    			if(key.isDown(68)){fighter.roll(-5);}
    			if(key.isDown(key.UP)){fighter.pitch(.5);}
    			if(key.isDown(key.DOWN)){fighter.pitch(-.5);}
    			if(key.isDown(key.RIGHT)){fighter.yaw(.5);}
    			if(key.isDown(key.LEFT)){fighter.yaw(-.5);}
    
    			
    			ltarget.transform = fighter.transform;
    			ltarget.moveForward(1000);
    			ltarget.moveDown(100);
    			view.camera.transform = fighter.transform;
    			view.camera.pitch(10);
    			view.camera.moveForward(28);
    			view.camera.moveUp(5);
    			view.render();
    		}
    thanks for the help

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