Skip to main content
darens41964140
Participating Frequently
April 17, 2019
Answered

Why a mouse click before my actions will work?

  • April 17, 2019
  • 1 reply
  • 702 views

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;

}

}

}

This topic has been closed for replies.
Correct answer JoãoCésar17023019

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

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
April 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

darens41964140
Participating Frequently
April 20, 2019

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.