February 2010
M T W T F S S
« Jan   Mar »
1234567
891011121314
15161718192021
22232425262728

Moving Movie Clip using keyboard.

Problem
Need move movie clip using keyboard. For example need move to left, right, top and bottom. Need click arrows on the keyboard.

Solution
Using event.keyCode.

Detailed explanation

stage.addEventListener(KeyboardEvent.KEY_DOWN, starMoving);

function starMoving(event:KeyboardEvent):void

{

trace(event.keyCode);

switch (event.keyCode)

{

case Keyboard.UP : star_mc.y-=10;

break;

case Keyboard.DOWN : star_mc.y+=10;

break;

case Keyboard.LEFT : star_mc.x=star_mc.x-10;

break;

case Keyboard.RIGHT : star_mc.x+=10;

break;

default:

break;

}

}

This code: Keyboard.UP you can change for number. How understand what number need use? Very easy, just enter trace(event.keyCode); before switch.

For example for UP you can use 38 and your code will looks like:

stage.addEventListener(KeyboardEvent.KEY_DOWN, starMoving);

function starMoving(event:KeyboardEvent):void

{

trace(event.keyCode);

switch (event.keyCode)

{

case 38 : star_mc.y-=10;

break;

case Keyboard.DOWN : star_mc.y+=10;

break;

case Keyboard.LEFT : star_mc.x=star_mc.x-10;

break;

case Keyboard.RIGHT : star_mc.x+=10;

break;

default:

break;

}

}

moving_mc_using_keyboard

Source:
moving_movie_clip_using_keyboard

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>