Skip to main content
Participant
April 25, 2017
Answered

Stoping and starting animation based on pressing space.

  • April 25, 2017
  • 1 reply
  • 372 views

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

This topic has been closed for replies.
Correct answer kglad

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;

    }

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 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;

    }

}

Participant
April 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.

kglad
Community Expert
Community Expert
April 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;

    }

}