1 Attachment(s)
[RESOLVED] Clicking on a button destroys KeyboardEvent listener?
I have a basic presentation which will have basic interactivity for the user. i.e. The user clicks on a play button, and the presentation plays, the user clicks on a back button, and the playhead steps back through the animation.
I also want to provide keyboard shortcuts to do the same thing, so the user can also hit the spacebar to play the presentation, or press the left arrow key to step back, etc.
I've successfully gotten both the click interactivity and keyboard interactivity to work... but only separately, when I try to add them to the same file, it breaks.
I also think I've figured out why. The problem seems to be that if I click a button that is only on the timeline for SOME of the frames, the KeyboardEvent listener no longer triggers.
I have a rewind button that will jump the playhead to the start of the presentation. However, from a use design standpoint, I really only want the rewind button visible/clickable if the user has advanced a bit into the presentation (i.e. frame 2 or beyond) because having the rewind button active on the first frame of the presentation can be confusing because it makes it appear to the user as though they can still rewind further, even though they've reached the beginning. I want to take a similar approach to a play button... as long as there are frames to play, the play button should be active; however, once you reach the end, the play button should dim down and not be clickable anymore, which serves as a visual cue to the user that they have reached the end of the presentation.
So, previously (in AS 2.0) this was not a problem. On the frame I wanted the rewind button to be "disabled", I would just turn the Button Symbol into a Graphic Symbol which would make it unclickable and remove any script, and then I would dim the Alpha of the Graphic. So, on frame 1, the rewind button would actually just be a transparent Graphic, and then once the playhead goes to Frame 2, it would be a Button symbol that could be clicked.
This technique apparently will break the new event model in AS3. If you look at the FLA I have attached, you can see that when you first "Test Movie"... any keyboard press outputs a trace statement that shows which key was pressed. However, if you hit the play button to play a little of the timeline, and then click the rewind button (instance name of rewind_btn) which jumps the playhead to frame 1 (where rewind_btn doesn't exist, because it's a Graphic now).
Now my Keyboard listener doesn't trigger!
I've even tried testing to see if the KeyboardEvent listener still exists by using the hasEventListener("keyUp"), and it returns as true, so the listener is there, it just doesn't trigger anymore!?
What am I doing wrong? How would I go about solving this problem? Because I can see this being a huge issue in a more complicated project, because I could see scenarios where you only want certain buttons to exist on certain frames! I just don't understand why a click event would mess with a key up event....
Code:
// Add keyboard listener to stage
this.stage.addEventListener(KeyboardEvent.KEY_UP, fOnKeyUp);
// Add keyboard listener to stage
this.stage.addEventListener(MouseEvent.CLICK, fOnClick);
function fOnKeyUp(evt:KeyboardEvent):void {
trace("Key Released: " + evt.keyCode);
}
function fOnClick(evt:MouseEvent):void {
trace("Button Clicked: " + evt.target.name);
switch(evt.target.name){
case "play_btn":
play();
break;
case "rewind_btn":
gotoAndStop(1);
break;
case "trace_btn":
trace(this.stage.hasEventListener("keyUp"));
break;
}
}