Skip to main content
Participating Frequently
October 8, 2010
Answered

Play movie/frame when key is pressed?

  • October 8, 2010
  • 1 reply
  • 626 views

Hello,
I have a car that is a movieclip with 2 frames looping in scene 1 and I  want the car to switch to another movieclip when I press the space  button and go back to the oroginal car clip when space is released and  if possible add a sound when space is pressed.
Thank you.

This topic has been closed for replies.
Correct answer Ned Murphy

Here's some code for the keyboard controls...

var keyListener:Object = new Object();
Key.addListener(keyListener);

function keyDownF(){
     if(Key.getCode() == 32){ 
         // put switch to other clip code here
         delete keyListener.onKeyDown; // to avoid repeating this function while key is down
     }
}

keyListener.onKeyDown = keyDownF;

function keyUpF(){
     if(Key.getCode() == 32){ 
         // put code to go back to car clip here
         keyListener.onKeyDown = keyDownF; // to restore the press interaction
     }
}

keyListener.onKeyUp = keyUpF;

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
October 8, 2010

Here's some code for the keyboard controls...

var keyListener:Object = new Object();
Key.addListener(keyListener);

function keyDownF(){
     if(Key.getCode() == 32){ 
         // put switch to other clip code here
         delete keyListener.onKeyDown; // to avoid repeating this function while key is down
     }
}

keyListener.onKeyDown = keyDownF;

function keyUpF(){
     if(Key.getCode() == 32){ 
         // put code to go back to car clip here
         keyListener.onKeyDown = keyDownF; // to restore the press interaction
     }
}

keyListener.onKeyUp = keyUpF;

antworkerAuthor
Participating Frequently
October 8, 2010

I'm sorry im a bit slow to understand that, but let's say that i want the original car which name is "car" to switch with another movieclip named "car_shoot" should I just put the text "car_shoot" and replace it were it says " // put switch to other clip code here" ? (Beginner )

Thank you anyway.

Ned Murphy
Legend
October 8, 2010

Yes, besically that's what you need to do... replace that line with whatever you intend.   I have no idea what you intend/mean as far as switching movieclips, so how you do that is up to you.