Highlighted

Why a mouse click before my actions will work?

New Here ,
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;

}

}

}

Adobe Community Professional
Correct answer by JoãoCésar | Adobe Community Professional

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

TOPICS
Discussions

Views

320

Likes

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

Why a mouse click before my actions will work?

New Here ,
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;

}

}

}

Adobe Community Professional
Correct answer by JoãoCésar | Adobe Community Professional

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

TOPICS
Discussions

Views

321

Likes

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
Apr 16, 2019 0
Adobe Community Professional ,
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

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

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
Reply
Loading...
Apr 17, 2019 1
New Here ,
Apr 20, 2019

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Apr 20, 2019 0