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

Why a mouse click before my actions will work?

Community Beginner ,
Apr 16, 2019 Apr 16, 2019

Copy link to clipboard

Copied

I've built a very simple animation

stop - immediately on the first frame

mouse click on button - go to the next frame and stop

and then I have an image that I have set to move (scroll) - Move with Keyboard Arrows

plus another button - go to previous frame and stop

That's it. Super basic.

My question is why do I have to do a mouse click before the "Move with Keyboard Arrows" action will work??

and

How do I make the "Move with Keyboard Arrows" action work without having to do a mouse click first?

_________________________________

FRAME 1

stop();

button_5.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_3);

function fl_ClickToGoToNextFrame_3(event:MouseEvent):void

{

nextFrame();

}

FRAME 2

stop();

button_3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousFrame);

function fl_ClickToGoToPreviousFrame(event:MouseEvent):void

{

prevFrame();

}

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

movieClip_1.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);

stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)

{

if (upPressed)

{

movieClip_1.y -= 5;

}

if (downPressed)

{

movieClip_1.y += 5;

}

if (leftPressed)

{

movieClip_1.x -= 5;

}

if (rightPressed)

{

movieClip_1.x += 5;

}

}

function fl_SetKeyPressed(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = true;

break;

}

case Keyboard.DOWN:

{

downPressed = true;

break;

}

case Keyboard.LEFT:

{

leftPressed = true;

break;

}

case Keyboard.RIGHT:

{

rightPressed = true;

break;

}

}

}

function fl_UnsetKeyPressed(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = false;

break;

}

case Keyboard.DOWN:

{

downPressed = false;

break;

}

case Keyboard.LEFT:

{

leftPressed = false;

break;

}

case Keyboard.RIGHT:

{

rightPressed = false;

break;

}

}

}

Views

476

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

correct answers 1 Correct answer

Community Expert , Apr 17, 2019 Apr 17, 2019

Hi.

This is a focus issue.

This happens when you test from within Animate CC.

Try going to your OS file explorer and run the SWF from there. The keyboard events are probably going to be fired without a previous mouse click.

You can also try to use...

stage.focus = this;

... in the main timeline to reinforce the needed focus.

Please let us know if this works.

Regards,

JC

Votes

Translate

Translate
Community Expert ,
Apr 17, 2019 Apr 17, 2019

Copy link to clipboard

Copied

Hi.

This is a focus issue.

This happens when you test from within Animate CC.

Try going to your OS file explorer and run the SWF from there. The keyboard events are probably going to be fired without a previous mouse click.

You can also try to use...

stage.focus = this;

... in the main timeline to reinforce the needed focus.

Please let us know if this works.

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
Community Beginner ,
Apr 20, 2019 Apr 20, 2019

Copy link to clipboard

Copied

LATEST

Fixed. Thank you!!

You can also try to use...

1.stage.focus = this

... in the main timeline to reinforce the needed focus.

I added the code you suggested, and that worked perfectly. Thank you.

-------------------

Try going to your OS file explorer and run the SWF from there. The keyboard events are probably going to be fired without a previous mouse click.

I have found that simply running the file in a browser isn't the perfect solution because different browsers may or may not fire the events without a previous mouse click. So I went with the more certain fix of adding the code.

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