jeudi 25 novembre 2010

Using the arrow keys

 If you want to move images around, you shift the container of the images, the movieClip. You could do this using keys from the keyboard, but more fun is to use the arrow keys. The code for this is a bit different from the normal keys:

normal keys were triggered like this:
if (String.fromCharCode( event.charCode) == "5")
{
myMovieClip1.x = myMovieClip1.x - 50;
}

but with the arrows we have to do it like this:(using only the event.keyCode)

 if (event.keyCode == 38 )//up
{
myMovieClipWithCar1.y = myMovieClipWithCar1.y - 50;
}

    if (event.keyCode == 40 )//down
{
myMovieClipWithCar1.y = myMovieClipWithCar1.y + 50;
}

    if (event.keyCode == 37 )//left
{
myMovieClipWithCar1.x = myMovieClipWithCar1.x - 50;
}

    if (event.keyCode == 39 )//right
{
myMovieClipWithCar1.x = myMovieClipWithCar1.x + 50;
}
that is because the characters and letters are having a charCode (ASCII code) but the arrows don't represent "normal" characters. The other keys are having keyCode too, of course, but "charCode" is more like "character".

Then using the arrow codes: keyCode = 37 or 38 or 39 or 40 you can steer an image around on the Stage.

Aucun commentaire:

Enregistrer un commentaire