4 Replies - 1554 Views - Last Post: 18 February 2016 - 12:02 PM

#1 retched   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Keyboard event not firing...

Posted 04 February 2016 - 11:36 AM

I have an object that is hidden on my stage with a .visible property set to the false. What I wanted to do was have it reappear when I hit a particular key stroke but ONLY on Frame 2.

So far I've tried (in AS3 via CS6 Flash Professional) to add an event listener with the following code in Frame 2:

// Keyboard events
addEventListener(KeyboardEvent.KEY_DOWN, ringin);


In my class file, I've written the following up:

		public function ringin(event:KeyboardEvent) {
			// Only do this if no one has buzzed in...
						
			// If key pressed first is the ~, ring in player 1.
			if ((buzzin_bln == false) && (event.keyCode == 192)) {
				t1Indicator.visible = true;
				buzzin_bln = true;
				trace("Player 1 buzzed in.");
			}
			
			// If key pressed first is the |, ring in player 2.
			if (buzzin_bln == false && event.keyCode == 220) {
				t2Indicator.visible = true;
				buzzin_bln = true;
				trace("Player 2 buzzed in.");

			}
			
			// If key pressed is the "END" key, clear the lock out and indicator.
			if (buzzin_bln == true && event.keyCode == 230){
				t1Indicator.visible = false;
				t2Indicator.visible = false;
				buzzin_bln = false;
			}
			
		}



However, the code is not firing. I know the Indicator is indeed on the top most layer on the stage and I've seen a Stack Overflow response that would indicate to place this within the constructor of the class. However, that would cause the entire program to react that way and that's not what I want. Where did I go wrong with this idea? Am I placing the code in the wrong location?

Again, I wrote the addEventHandler on the frame itself while the function above on the class file.

If I'm using class files, should I still be writing codes directly on the frame or no?

Is This A Good Question/Topic? 0
  • +

Replies To: Keyboard event not firing...

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2351
  • View blog
  • Posts: 7,185
  • Joined: 15-January 14

Re: Keyboard event not firing...

Posted 04 February 2016 - 12:31 PM

Maybe using "ringin" as the function to call for the handler doesn't work because that function is part of an object and is not a global function. Maybe it's just a scope issue, you need to refer to the object which has that method defined.
Was This Post Helpful? 0
  • +
  • -

#3 retched   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Re: Keyboard event not firing...

Posted 04 February 2016 - 12:45 PM

Well, here's what I changed and its now "somewhat" working.

On the 2nd frame itself, I added the following code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, ringin);


Everything else remained the same. (Except I found charCode of the ~ and | and used that instead.) One small problem is that the stage doesn't seem to want to accept keyboard input the first few seconds. Thought I had this before but for somewhere it didn't exactly work.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2351
  • View blog
  • Posts: 7,185
  • Joined: 15-January 14

Re: Keyboard event not firing...

Posted 04 February 2016 - 02:19 PM

Are you running that in debug mode so you can see error messages show up? Unless you have a fractional frame rate then a few seconds is a bit long for event handlers to get registered.
Was This Post Helpful? 1
  • +
  • -

#5 retched   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 25-January 16

Re: Keyboard event not firing...

Posted 18 February 2016 - 12:02 PM

View PostArtificialSoldier, on 04 February 2016 - 04:19 PM, said:

Are you running that in debug mode so you can see error messages show up? Unless you have a fractional frame rate then a few seconds is a bit long for event handlers to get registered.


This fixed it for me... For some reason I thought 1 Frame per second was a good thing in regards to file size but yeah... Turns out not really. When I switched it back to 30 frames per second, everything worked accordingly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1