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

Stoping and starting animation based on pressing space.

New Here ,
Apr 25, 2017 Apr 25, 2017

Hello i am a beginner at action script so this should be fairly easy to answer. Basically i want the default setting to stop the animation stop(); and then when space is held to play();

Here's what i have:

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyNotPressed);

function KeyPressed(evt:KeyboardEvent):void {

    if (evt.keyCode==Keyboard.SPACE){

       play();

    }

}

function KeyNotPressed(evt:KeyboardEvent):void {

    if (evt.keyCode!=Keyboard.SPACE){

       stop();

    }

}

Thanks for the help

TOPICS
ActionScript
331
Translate
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 25, 2017 Apr 25, 2017

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);

var toggle:Boolean;

function KeyPressed(evt:KeyboardEvent):void {

    if (evt.keyCode==Keyboard.SPACE){

if(toggle){

       play();

} else {

stop();

}

toggle=!toggle;

    }

}

Translate
Community Expert ,
Apr 25, 2017 Apr 25, 2017

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);

var toggle:Boolean;

function KeyPressed(evt:KeyboardEvent):void {

    if (evt.keyCode==Keyboard.SPACE){

if(toggle){

       play();

} else {

stop();

}

toggle=!toggle;

    }

}

Translate
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 ,
Apr 25, 2017 Apr 25, 2017

Hi thanks very much, how would i make it start paused? Not that's its really needed but i would like when space is held instead of pressed.

Translate
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 ,
Apr 25, 2017 Apr 25, 2017

to start paused:

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);

var toggle:Boolean=true;

stop();

function KeyPressed(evt:KeyboardEvent):void {

    if (evt.keyCode==Keyboard.SPACE){

if(toggle){

       play();

} else {

stop();

}

toggle=!toggle;

    }

}

Translate
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 ,
Apr 26, 2017 Apr 26, 2017

Hi thanks very much. Do you know how to do when a key is held not pressed?

Translate
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 ,
Apr 26, 2017 Apr 26, 2017

one way to do that would be to start a timer when a key is pressed and then use the timer listener to determine when and what action to take.

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

Translate
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 ,
Apr 28, 2017 Apr 28, 2017

Why do i have to click first before doing it?

Translate
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 ,
Apr 28, 2017 Apr 28, 2017
LATEST

your swf needs focus to start.  you can use code to do that,

stage.focus=this;

Translate
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