6 Replies - 658 Views - Last Post: 20 March 2011 - 02:35 PM

#1 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Keyboardevent , several buttons at once?

Posted 13 March 2011 - 11:53 PM

Hi,

So i'm fiddling around with some basic controls for a minigame and i was wondering
if there is any specific way to handle several key inputs at once?

For instance at the moment i'm just doing it this way, but when one key is pressed
no other commands will go through so you can't shoot and move at the same time for instance.


			if(e.keyCode == 40)
			{
				//movement code
			}
			if(e.keyCode == 37)
			{
				//movement code
			}



This post has been edited by heady89: 13 March 2011 - 11:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Keyboardevent , several buttons at once?

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Keyboardevent , several buttons at once?

Posted 14 March 2011 - 03:55 AM

If you are programming in Actionscript 3.0, you'll have to first create your own Key class that provides the bool isDown(keycode:int) functionality of Actionscript 2.0.

It returns true if a particular key is pressed that that moment.

With that function done, its simply matter of checking the key combinations like this:

if(isDown(SPACE_KEY) && isDown(ATTACK_KEY)){

// DO something

}

Was This Post Helpful? 0
  • +
  • -

#3 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Keyboardevent , several buttons at once?

Posted 16 March 2011 - 06:26 AM

Not sure im following how the as2 functionality would work in practice, but I'll give it a try.

This post has been edited by heady89: 16 March 2011 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Keyboardevent , several buttons at once?

Posted 16 March 2011 - 08:51 AM

View Postheady89, on 16 March 2011 - 06:26 AM, said:

Not sure im following how the as2 functionality would work in practice, but I'll give it a try.



I seem to be able to move using different combinations now however if i press lets say left , up so that it goes diagonal, if i then release one of the keys lets say the left key, it will not continue to go up, it will simply stop.
The same goes for when im trying to fire, my primitive laser type.

		public function Move(e:KeyboardEvent):void
		{		
			if(this.iKey.isDown(Keyboard.UP))
			{
				this.y --;
			}
			if(this.iKey.isDown(Keyboard.DOWN))
			{
				this.y ++;
			}
			if(this.iKey.isDown(Keyboard.RIGHT))
			{
				this.x ++;
			}
			if(this.iKey.isDown(Keyboard.LEFT))
			{
				this.x --;
			}
			
			if(this.iKey.isDown(Keyboard.SPACE))
			{
				var laser:Sprite = new Sprite();
				laser.graphics.beginFill(0x444444);
				laser.graphics.drawRect(this.x,this.y, 20,3);
				laser.graphics.endFill();
				stage.addChild(laser);
				
				var tween:GTween = new GTween(laser,1,{alpha:0},{swapValues:true});
				tween.proxy.x = 1300;
			}
		}



And in main these event listners are waiting
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, carObjekt.Move);
			this.stage.addEventListener(KeyboardEvent.KEY_UP, carObjekt.Move);


Was This Post Helpful? 0
  • +
  • -

#5 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Keyboardevent , several buttons at once?

Posted 16 March 2011 - 03:11 PM

Show the code for the isDown() function.
Was This Post Helpful? 0
  • +
  • -

#6 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Keyboardevent , several buttons at once?

Posted 16 March 2011 - 03:25 PM

The keyklass isn't mine fully i've only modified it since i'm trying to figure this out, however here it is maybe that will shed some light on the whole thing


// Key.as
package {
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	/**
	 * The Key class recreates functionality of
	 * Key.isDown of ActionScript 1 and 2. Before using
	 * Key.isDown, you first need to initialize the
	 * Key class with a reference to the stage using
	 * its Key.initialize() method. For key
	 * codes use the flash.ui.Keyboard class.
	 *
	 * Usage:
	 * Key.initialize(stage);
	 * if (Key.isDown(Keyboard.LEFT)) {
	 *    // Left key is being pressed
	 * }
	 */
	public class Key 
	{
		
		private static var initialized:Boolean = false;		// marks whether or not the class has been initialized
		private static var keysDown:Object = new Object();	// stores key codes of all keys pressed
		
		
		public function Key()
		{
			
		}
		/**
		 * Initializes the key class creating assigning event
		 * handlers to capture necessary key events from the stage
		 */
		public function initialize(stage:Stage):void
		{
			if (!initialized) {
				// assign listeners for key presses and deactivation of the player
				stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
				stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
				stage.addEventListener(Event.DEACTIVATE, clearKeys);
				
				// mark initialization as true so redundant
				// calls do not reassign the event handlers
				initialized = true;
			}
		}
		
		/**
		 * Returns true or false if the key represented by the
		 * keyCode passed is being pressed
		 */
		public function isDown(keyCode:uint):Boolean
		{
			if (!initialized)
			{
				// throw an error if isDown is used
				// prior to Key class initialization
				throw new Error("Key class has yet been initialized.");
			}
			return Boolean(keyCode in keysDown);
		}
		
		/**
		 * Event handler for capturing keys being pressed
		 */
		private function keyPressed(event:KeyboardEvent):void
		{
			// create a property in keysDown with the name of the keyCode
			keysDown[event.keyCode] = true;
		}
		
		/**
		 * Event handler for capturing keys being released
		 */
		private function keyReleased(event:KeyboardEvent):void 
		{
			if (event.keyCode in keysDown)
			{
				// delete the property in keysDown if it exists
				delete keysDown[event.keyCode];
			}
		}
		
		/**
		 * Event handler for Flash Player deactivation
		 */
		private function clearKeys(event:Event):void
		{
			// clear all keys in keysDown since the player cannot
			// detect keys being pressed or released when not focused
			keysDown = new Object();
		}
	}
}


This post has been edited by heady89: 17 March 2011 - 03:56 AM

Was This Post Helpful? 0
  • +
  • -

#7 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Keyboardevent , several buttons at once?

Posted 20 March 2011 - 02:35 PM

EDIT:
I solved it by creating a timer instead of listening to onkeydown and onkey up and i made the timer constantly run a move function every 10th of a second, this way i managed to get a fluid movement

This post has been edited by heady89: 20 March 2011 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1