• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Problem executing mouse event and key board event simultaneously

New Here ,
Jan 01, 2022 Jan 01, 2022

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. 

TOPICS
ActionScript , Code , Error , Timeline

Views

225

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 02, 2022 Jan 02, 2022

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 02, 2022 Jan 02, 2022

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 02, 2022 Jan 02, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 02, 2022 Jan 02, 2022

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 02, 2022 Jan 02, 2022

Copy link to clipboard

Copied

Thanks, @Colin Holgate!

 

@vikramkamath37, do you mind showing your code/file?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 02, 2022 Jan 02, 2022

Copy link to clipboard

Copied

  1. @JoãoCésar its not with me now currently its at office. Its something like I have 2 buttons inside a movieclip one for play(in first keyframe) and another one for pause(2nd keyframe). Initially animation is in stop() ; stage, when I hit play button it starts playing also inside the movie clip it goes to keyframe 2 and stops (where pause button is placed). When i hit pause now it stop() ; animation and inside the movieclip it goes to keyframe 1 and stops (where play button is placed). Now I want the same thing to happen with keyboard spacebar (play/pause). And both should work with click and also with keyboard. Please suggest. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 02, 2022 Jan 02, 2022

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 02, 2022 Jan 02, 2022

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 03, 2022 Jan 03, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 03, 2022 Jan 03, 2022

Copy link to clipboard

Copied

LATEST

@Colin Holgate It is a Action Script 3.0 FLA file

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines