Copy link to clipboard
Copied
I have a button which does play/pause animation using mouse event. Now I wanted to do the same thing with spacebar/keyboard, so i scripted (AS3) keyboard event as well. Now the problem is buttons work fine, keyboard works fine if only keyboard is used but if i play using button and then try to pause using keyboard it is not executing. Please help.
Copy link to clipboard
Copied
Hi.
Here is a suggestion:
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
function playAnim(e:MouseEvent):void
{
if (yourAnim.isPlaying)
yourAnim.stop();
else
yourAnim.play();
}
function keyDownHandler(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.SPACE)
playAnim(null);
}
yourButton.addEventListener(MouseEvent.CLICK, playAnim);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
Regards,
JC
Copy link to clipboard
Copied
Hi JC thanks for the suggestion. But can you please tell me what is (null) and how does that function. Thank you
Copy link to clipboard
Copied
He is calling a function that normally is triggered with a mouse action. This line is expecting that mouse event:
function playAnim(e:MouseEvent)
To call the function from somewhere other than a mouse action, he uses playAnim(null), just to keep the function happy. Without null there would be an error message.
Another solution to the same issue is this:
function playAnim(e:MouseEvent=null)
then if you call it with playAnim(), the missing parameter is filled in with a null value.
Copy link to clipboard
Copied
Hi. I have 2 separate buttons one for play (in first keyframe) and other one for pause (in second keyframe). So i have scripted them like when play button is clicked it will start playing animation and also it will gotoAndStop at pause button keyframe (there by hiding play button) similarly vise versa when pause is clicked. Also scripted same code for keyboard event, both work fine in there place but problem comes when I try play by click and pause it by keyboard it fails or vise versa.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @JoãoCésar @Colin Holgate attached here with my working file, there is a playbtn and a pausebtn in there which are working how I want them to. Now i want SPACEBAR to work as these buttons, and both should function simultaneously. Please suggest I'm new to Animate CC and Action Script.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Are you making an ActionScript 3.0 FLA, or are you making a mobile or desktop Adobe AIR application? If you are, could you still save a version of your file as an ActionScript 3.0 FLA? Then I will be able to open the file.
Copy link to clipboard
Copied
@Colin Holgate It is a Action Script 3.0 FLA file