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.


Alex says: Monday, July 21, 2008

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: Monday, April 14, 2008

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


Twitch says: Monday, April 14, 2008

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!!!!


Admin Bob says: Monday, March 24, 2008

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.


megha says: Friday, March 21, 2008

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: Saturday, March 08, 2008

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


Frustrated says: Friday, March 07, 2008

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: Sunday, March 02, 2008

my guy will only go down whats the problem


EagleVision says: Thursday, February 28, 2008

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


Admin Bob says: Sunday, February 24, 2008

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

  Next 


 

 

 

 4

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact