Skip to main content
Participant
April 25, 2017
解決済み

Stoping and starting animation based on pressing space.

  • April 25, 2017
  • 返信数 1.
  • 373 ビュー

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

このトピックへの返信は締め切られました。
解決に役立った回答 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

kglad
Community Expert
kgladCommunity Expert解決!
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;

    }

}