RSS link icon

.

Flash game moving controls


In this Flash actionscript tutorial you will see the basics of how to assign keyboard key values to move an object around, a very useful effect for games and other interactive flash projects.

First of we will draw or import an object to move around, I just imported mine as shown below.

Flash move controls

Now right click your new object and convert it to a movie clip, in the properties panel give our new movie clip an instance name, I called mine move_obj.

Flash move controls

Select your movie clip and hit F9 to get to the action script panel and type in the following code and I will explain below.

Flash move controls

First we have an event handler called onClipEvent (enterframe) wrapped around our code, this event handler tells flash to "update" or repeat/check the code repeatly.

Then we have the same functionallity for 4 different keys, the up, down, left and right. An if statement telling flash, if one of those buttons is down, move the selected object from its position and add or subtract 15 px.

Its as simple as that.


teddy says: 2010-04-07

thanks I need that code :D :D

Alex says: 2008-07-21

if you guys are getting syntax errors thanks for speaking up. I tried this code and it didnChr(34)t work but i found out that if you put "onClipEvent(EnterFrame) or whatever before every directional key it should work.

Admin Bob says: 2008-04-14

hi Twitch, can you tell me what error message flash gives you in the output window

Twitch says: 2008-04-14

Yeah, this is interesting, about the adobe flash cs3 on the mac, my school just got it... in my multimedia class, we have a huge mac computer mac osX or whatever... and yeah, I even converted it to a movie clip, and I still couldnChr(34)t get it to have any actionscript applied to it... I even tried using the ActionScript2.0 library, and it still didnChr(34)t work!!!! any suggestions, my teacher just put it on there, and I got to test it out. I was so excited!!!! but, well... :( was my face when I came upon that slight simple error.... is it only mac that it happens to anyways, if you have an answer, please post!!!!

megha says: 2008-03-21

please help me!! IChr(34)ve tried everything....everytime i try to test the movie/game it gives me a syntax error. The error says "1087: Syntax error: extra characters found after end of program." please help me i donChr(34)t know how to fix this. so someone please help me and please mail me the response if possible..... my mail is parihar.saurabh.sonu@gmail.com

Admin Bob says: 2008-03-24

hi megha, as the error says, you have an extra character in the code, a tip is to double click the error message, this will bring you to the actionscript line where the extra character is.

Admin Bob says: 2008-03-08

hi Frustrated, well thats because you are using actionscript 3.0, and this code is for 2.0

Frustrated says: 2008-03-07

I am using Adobe Flash CS3 and I have tried to add an action to my object but it says that the "current selection cannot have actions applied to it". IChr(34)m not sure what to do... I am using a Mac by the way.

dan says: 2008-03-02

my guy will only go down whats the problem

EagleVision says: 2008-02-28

Hey! I like the look of it. Would have helped me a year ago :)

Admin Bob says: 2008-02-24

hi Stressed, IChr(34)m not quite sure I know what your problem is, but I will try to explain anyway. In the flash code you refere to the object as this._y you should give it a unique name like bob._y, an remember to give the specific movieclip that same instance name of cause. I hope that helps you

Stressed! says: 2008-02-24

This works nicely, thanks, But I tried using the same block of code for another object which i want to move as well on the same layer, how do you specify which object to move cause now I have both moving together when I move the arrow keys! and need to only move one at a time! Please help as its for a project and Im new to Flash & ActionScript! Thanks. Ps. im using flash mx pro 2004

Harrison says: 2008-02-21

Great tutorial. You should make another very short one and tell me who to close when you hit escape. Or E-mail me at htwagstaff@gmail.com. Thanks.

Kaleem says: 2008-02-18

Newbie at flash. It was simple to understand and do. Works for me. Speed can be controlled by changing the value from 15 to something else. Used Flash 5.

Drew says: 2008-02-13

Awsome. I have the trial version of CS3, so IChr(34)m trying to make a few games in the next 30 days=P (canChr(34)t really afford the full version, at least not right now). I have access to MX 2004 at school, though=).

OmegaNine says: 2008-02-12

Pfff never mind, this is Action Script 2.0 not 3.0....I was figuring on 3.0 since its the newest. (Might want to say something in the top of the show on the net one though) Thanks for the tutorial. Look forward to reading threw more of your work.

OmegaNine says: 2008-02-12

Giving me the compiler error "1087" Sytax Error: extra char found after end of program." Any idea what this means This is was the first tut i came across after installing Flash CS3 Pro.

Ketku says: 2008-02-11

I got it working on the first try. Very simple. Thanks!

Admin Bob says: 2008-02-09

try telling me wha error flash gives you..

dr pain says: 2008-02-06

i didnt work ive got flash v.5

jelly sac says: 2008-02-05

i cant get actionscript up i got flash mx pro 2004

kazzzie says: 2008-02-02

it didn?t work , must iv got a other version KSziieee

Lemony says: 2008-02-02

It didnt work :( It didnt help that i took ages retyping that big long code...

haxan says: 2008-02-01

it should not go over the sides. There should also be a speed controller. Anyways a simple nice tutorial.

Dark says: 2008-01-31

itChr(34)s working thanks.

Berishman says: 2008-01-31

you should probably go over else, and moveSpeed. this is extremely basic.

casey says: 2008-01-27

it wont work

Trav says: 2008-01-23

Mines not working ?.?

Jix says: 2008-01-09

Thanks really useful!

Jix says: 2008-01-09

Thanks really useful!

Karthik says: 2008-01-13

Awesome
make money with photoshop - book