Problems with Keyboard property

Cannot get flash to detect keypress

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 15702 Views - Last Post: 22 April 2010 - 12:04 AM

#1 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Problems with Keyboard property

Posted 10 February 2009 - 05:42 PM

I'm pretty new to actionscript, and i'm making a game with some friends and I'm having a bit of trouble.
I'm trying to get it so that if the Shift button is pressed, the health meter and health bar will be displayed above my enemies.

I have an event listener at the top, listening for my 'select' function:
 this.addEventListener(KeyboardEvent.KEY_DOWN, select); 


and then my actual select function is:
protected function select(e :KeyboardEvent) :void
		{
			if ( e.keyCode == Keyboard.SHIFT)
			{	
				this.health_meter.visible = true;
				this.health_bar.visible = true;
			}
		}



I keep getting the error:
1120: Access of undefined property Keyboard.

Any help would be appreciated greatly, thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with Keyboard property

#2 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Re: Problems with Keyboard property

Posted 10 February 2009 - 05:43 PM

I'm pretty new to actionscript, and i'm making a game with some friends and I'm having a bit of trouble.
I'm trying to get it so that if the Shift button is pressed, the health meter and health bar will be displayed above my enemies.

I have an event listener at the top, listening for my 'select' function:
 this.addEventListener(KeyboardEvent.KEY_DOWN, select); 


and then my actual select function is:
protected function select(e :KeyboardEvent) :void
		{
			if ( e.keyCode == Keyboard.SHIFT)
			{	
				this.health_meter.visible = true;
				this.health_bar.visible = true;
			}
		}



I keep getting the error:
1120: Access of undefined property Keyboard.

Any help would be appreciated greatly, thank you!
Was This Post Helpful? 0
  • +
  • -

#3 theRemix  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

Re: Problems with Keyboard property

Posted 10 February 2009 - 05:47 PM

View PostChainsaw_Matt, on 10 Feb, 2009 - 04:43 PM, said:

I'm pretty new to actionscript, and i'm making a game with some friends and I'm having a bit of trouble.
I'm trying to get it so that if the Shift button is pressed, the health meter and health bar will be displayed above my enemies.

I have an event listener at the top, listening for my 'select' function:
 this.addEventListener(KeyboardEvent.KEY_DOWN, select); 


and then my actual select function is:
protected function select(e :KeyboardEvent) :void
		{
			if ( e.keyCode == Keyboard.SHIFT)
			{	
				this.health_meter.visible = true;
				this.health_bar.visible = true;
			}
		}



I keep getting the error:
1120: Access of undefined property Keyboard.

Any help would be appreciated greatly, thank you!


did you import the class yet?

import flash.ui.Keyboard

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Problems with Keyboard property

Posted 10 February 2009 - 05:49 PM

Threads merged, please don't create duplicate topics :)
Was This Post Helpful? 0
  • +
  • -

#5 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Re: Problems with Keyboard property

Posted 10 February 2009 - 05:52 PM

Sorry for creating the duplicate, must've accidently double-clicked!

But I imported the keyboard event class with

import flash.events.KeyboardEvent;

and that got rid of an error I had had before that.
Does 'keyboard' have it's own class that needs to be imported?
Was This Post Helpful? 0
  • +
  • -

#6 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: Problems with Keyboard property

Posted 10 February 2009 - 06:18 PM

View PostChainsaw_Matt, on 11 Feb, 2009 - 01:52 AM, said:

Does 'keyboard' have it's own class that needs to be imported?


View PosttheRemix, on 11 Feb, 2009 - 01:47 AM, said:

did you import the class yet?

import flash.ui.Keyboard;	 // ";" added by skater_00 :p

This post has been edited by skater_00: 10 February 2009 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Re: Problems with Keyboard property

Posted 10 February 2009 - 06:21 PM

alright so I imported the class like you said

import.flash.iu.keyboard

but two things are going wrong here.

The first thing is that I can't put any letters or numbers in using the keyboard. operator, only buttons like Shift and Enter. For instance
protected function showHealth(e :KeyboardEvent) :void
		{
			if (e.keyCode == Keyboard.A)
			{	
				this.health_meter.visible = true;
				this.health_bar.visible = true;
			}
		}



that returns the error 1119: Access of possibly undefined property A through a reference with static type Class.

And when I use a this code
 protected function showHealth(e :KeyboardEvent) :void
		{
			if (e.keyCode == Keyboard.SHIFT)
			{	
				this.health_meter.visible = true;
				this.health_bar.visible = true;
			}
		}


the program will compile and run, but when I press shift (or enter or any other button it will compile with), the health meter/bar still isn't shown. Does anyone know what's going on here? Any help would be great, thank you!

This post has been edited by Chainsaw_Matt: 10 February 2009 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: Problems with Keyboard property

Posted 10 February 2009 - 07:26 PM

First of all, it's not import.flash.iu.keyboard, but import flash.ui.Keyboard;.

Second. Not all key codes can be accessed from the Keyboard class. You may need to use key code ASCII numbers (except for KEYNAME_'s, those are strings). You could try to find a list with all the possible key codes, or you could do what many programmers - including myself - most likely do. I use trace(event.keyCode); in a keyDownHandler to check for key codes. How?! Simply test the movie, press any key and read its key code in the output window. (You may need to go to Control in your Flash Player, then checkmark "Disable Keyboard Shortcuts")

Anyway, I "recreated" your KEY_DOWN concept and everything works bloody fine with the code below:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

mc.visible = false;

function keyDownHandler(event:KeyboardEvent):void
{
	trace(event.keyCode);     // Check keyCode!
	
	switch (event.keyCode)
	{
	case 16: case 37: case 13: case 79:
		mc.visible = true;
		break;
	default:
		break;
	}

	// 16: shift | 37: left arrow | 13: enter | 79: o | ...
}


EDIT: I just checked, and you can also find a list with all key codes in the LiveDocs: ActionScript 3.0 Language and Components Reference (All Classes -> Keyboard -> Public Constants).

Hope this helps..

This post has been edited by skater_00: 10 February 2009 - 07:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Re: Problems with Keyboard property

Posted 10 February 2009 - 08:05 PM

Thanks! That actually helps a lot. I'm going to try and implement that in my code and see how it goes, if I have any problems I'll post back here.
Thanks again for the help!
Was This Post Helpful? 0
  • +
  • -

#10 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Re: Problems with Keyboard property

Posted 12 February 2009 - 11:20 AM

If anybody will read this, I tried what skater_00 said and everything compiled just fine, but it still wont work! I tried it with several different keys with their respective keycodes, but still to no avail. What else could the problem be?
Was This Post Helpful? 0
  • +
  • -

#11 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Re: Problems with Keyboard property

Posted 12 February 2009 - 11:40 AM

Oh, and here's my new code for the function

protected function showHealth(e :KeyboardEvent) :void
		{
			 trace(e.keyCode);	
	 
			 switch (e.keyCode)
   			{
	 			case 37:
		 		 this.health_meter.visible = true;
				 this.health_bar.visible = true;
		 		 break;
	 			 default:
				 break;
	 		}

	 }


Was This Post Helpful? 0
  • +
  • -

#12 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: Problems with Keyboard property

Posted 12 February 2009 - 02:59 PM

Could you possibly ZIP your source files and add it as attachment in one of your posts?
Was This Post Helpful? 0
  • +
  • -

#13 GreenSpiral  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 30-December 08

Re: Problems with Keyboard property

Posted 13 February 2009 - 12:00 PM

What's wrong with
if (key.isDown(key.SHIFT)){}


What's better about functions??
Was This Post Helpful? 0
  • +
  • -

#14 CyanBlue  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 156
  • Joined: 01-September 06

Re: Problems with Keyboard property

Posted 13 February 2009 - 01:13 PM

The 'key' should be 'Key'...
var keyListener:Object = new Object();
keyListener.onkeydown = function()
{
	if (Key.isDown(Key.SHIFT))
	{
		trace("You pressed a Shift key.");
	}
}
Key.addListener(keyListener);

Was This Post Helpful? 0
  • +
  • -

#15 GreenSpiral  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 30-December 08

Re: Problems with Keyboard property

Posted 14 February 2009 - 11:44 AM

My question is why have a function??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2