Flash Game Development with Flex and Actionscript - Getting Started

Written by:  • Edited by: Linda Richter
Updated Dec 15, 2009
• Related Guides: Adobe | HTML

Get started with Flash game development. This article shows you which tools to download and details the first steps in creating a Flash game using Flex and Actionscript.

Getting Started

With Flash Player and the Flex SDK Adobe has supplied everything you need to start creating your own Flash games. Creating a Flash game using Flex has several advantages like:

  • Cross platform compatibility – there is a flash player for every major platform
  • Easy deployment – publishing the game is as simple as uploading the swf file
  • (Almost) zero installation requirements – an end user only needs a web browser with the Flash player plugin installed
  • Free tools - all you need is the free Flex SDK and a text editor

This article series will step you through the process of creating a 2D Flash game using Flex, with the result being a complete game in the style of the old school top down shooters.

To get started you will first need to download the Flex 3 SDK from http://www.adobe.com/products/flex/flexdownloads/. The SDK contains all the tools you will need to compile the source code presented here into a SWF file that can be added to a web page. You will also need a decent text editor. I quite like Textpad, which can be downloaded from http://www.textpad.com/. Finally you will need to download a Flash debug player from http://www.adobe.com/support/flashplayer/downloads.html. The debug player will allow you to open up a SWF file directly without having to create a web page to contain it. With these three tools you are ready to start coding.

The graphics used in the game are courtesy of spritelib, which you can download from http://www.flyingyogi.com/fun/spritelib.html.

Creating the Application

Conceptually Flex splits up an average program into two sections: the GUI and the Actionscript code. The GUI is created in an MXML file, which is an XML file that contains user interface elements nested in tags very similar to HTML. Note that the MXML file can contain Actionscript code inside an mx:Script tag, but the main focus of the MXML file is to define the user interface.

The top level tag of an MXML file is the mx:Application tag. This Application object is the entry point of the Flex application, and is the most logical place to start.

main.mxml Actionscript Source Code

<?xml version="1.0" encoding="utf-8"?>

<mx:Application

xmlns:mx="http://www.adobe.com/2006/mxml"

layout="absolute"

width="600"

height="400"

frameRate="100"

creationComplete="CreationComplete()"

enterFrame="EnterFrame(event)">

<mx:Script><![CDATA[

public function CreationComplete():void

{

}

public function EnterFrame(event:Event):void

{

}

]]></mx:Script>

</mx:Application>

We start by defining some of the properties of the Application object. These properties can be set through attributes in the mx:Application tag. This should look familiar to anyone who has written HTML.

width and height

Specify the screen size of the program in pixels.

framerate

Specifies a limit on the frames per second. The default is 24, but since we want the game to run as fast as possible it’s best to override this with a much higher number. Note that just setting the framerate to 100 doesn’t guarantee that the frame rate will always be 100 (or even get anywhere near it). This property just sets a ceiling on what the frame rate could be.

creationComplete

Attaches a function to be called when the Application has been created. We use this as the entry point in the program.

enterFrame

Attaches a function to be called every time the screen is redrawn. We will use this to repaint the screen with the next frame of the game.

The mx:Script tag gives us a place to write some Actionscript code. The [CDATA[ ]] tag just means that any special characters inside the mx:Script tag will be interpreted as text rather than XML characters. Inside the mx:Script tag we need to add the two functions which match the values for the creationComplete and enterFrame properties.

Compiling and Running

To compile the program you need to run the command mxmlc main.mxml from the command prompt. You can then open up the resulting main.swf file in the Flash debug player through File->Open.

And the end result? A blank screen that does nothing. Not terribly exciting I’ll admit, but it is a start. We will build off this code in part 2 of the series to start drawing to the screen.

Go back to Flash Game Development with Flex and ActionScript

Images

CompilingThe Flash game

Related Article

Flixel Logo Creating a platform game with Flixel and Adobe Flex

This tutorial series shows you how to use the Flixel ActionScript game engine to create a simple platform game.


Comments

Showing all 51 comments
 
chrisstout Jul 21, 2011 9:38 AM
Required RSLs
Anonymous,
The message about the required RSLs isn't an error. It's just telling you that the RSLs are being loaded.

If your background isn't the same colour, it's probably a difference in the main.mxml file. Toward the top there's the line that lists the default colour.
Jim Carrillo May 26, 2011 3:06 PM
required RSL's 5 of (.swz with 1 failover )
Hi Matthew, excellent tutorial and thankyou. While compiling I get this message:

Required RSLs:
http://fpdownload.adobe.com/pub/swz/flex/4.5.0.20967/framework_4.5.0.20967.s
wz with 1 failover.
http://fpdownload.adobe.com/pub/swz/tlf/2.0.0.232/textLayout_2.0.0.232.swz w
ith 1 failover.
http://fpdownload.adobe.com/pub/swz/flex/4.5.0.20967/spark_4.5.0.20967.swz w
ith 1 failover.
http://fpdownload.adobe.com/pub/swz/flex/4.5.0.20967/sparkskins_4.5.0.20967.
swz with 1 failover.
http://fpdownload.adobe.com/pub/swz/flex/4.5.0.20967/mx_4.5.0.20967.swz with
1 failover.

The program still runs but the only difference, is that the splash screen doesn't have a grey color like yours, but the "start" button works and the bounce with the plane. Any suggestions? Thanks again!
Squiffy Apr 13, 2011 6:50 AM
Great stuff.
This is the best tutorial for making a game in AS for beginners that I can find anywhere. Many thanks.
brave zhou Mar 14, 2011 5:04 AM
My MMORPG flash game demo
Great article,
I read this article and built a MMORPG web game demo in spare time, I put it on:http://cd-zdy.vicp.net:8088
Please try and give me some feedback.
my skype is:dayongzhou-china
kevun Mar 4, 2011 1:51 PM
i have problem with step 1
Take the code "<?xml.....<mx:Application..
copy and paste on my Flex , before
and save a MXML file. what els i have to do
chris Moeller Feb 28, 2011 3:56 PM
Use FlashDevelop instead of command line compiling...
'mxmlc.exe' is an executable located in your 'Flex SDK\bin' folder.

To go to the command prompt, go to start->run-> and type "cmd" and press enter. Black window should come up

Next, to get access to the mxmlc executable, you need to type 'path=C:\SDK\4.1.0\bin', but to the path of the bin folder of your sdk.

Now if you type 'mxmlc' it should display 'Adobe Flex Compiler...'.

Now just go to your folder with main.mxml (like 'cd c:\projects\myproject\src\'), and run the command, which should compile your flex program.

But really, just download and use FlashDevelop - it's an integrated development Environment, so it has code completion, auto imports, and compiles for you :-o

Plus it's FREE and easier to use than the flash IDE.
makar Feb 28, 2011 8:08 AM
laymans terms
Please Explain:

To compile the program you need to run the command mxmlc main.mxml from the command prompt.
Chris Moeller Feb 25, 2011 2:40 PM
Great tutorial!
Great tutorial for beginners! I liked how you put the tiles together, and included scrolling(which a lot of others don't).

I created two tutorials on my site for creating a flash asteroids game:

http://www.chrismweb.com
damian Feb 16, 2011 4:40 AM
error
Im getting error in 3rd lesson: "...\ResourceManager.as<11>: col: 2: Error: Unable to transcode ../media/brownplane.png.
Arvinder Singh Jan 30, 2011 6:11 AM
cant complile the file
'mxmlc' is not recognized as an internal or external command,
operable program or batch file.
omegaman66 Jan 10, 2011 3:15 AM
Help
I am an idiot apparently keep that in mind.

"Then where the tutorial says input "mxmlc main.mxml" into the command line- replace this with "mxmlc ~/Desktop/game/main.mxml" (or wherever you game project file is located)- your main.swf file will appear in the same folder"

Thanks for the help but I am so stupid what is this command line thing a majig you are talking about. How do I get to it. I had to search to find the mxmlc.exe file. It was in the bin folder. I tried dragging and dropping the file on the executable but that didn't work. Do I need to install this before it will work or something.

I downloaded the sdk and then I copied the contents to a folder but there is no install file and I get and error that dll's can't be found when I try to do anything.
Lindsay Dec 29, 2010 9:55 PM
Can't open swf
I downloaded debug player 10 but I don't see it anywhere and have no idea how to file->open the swf. Any help?
Adovid Dec 24, 2010 10:15 AM
Creation Complete = thank u
This is one of the best flex tutorials out there!

I spent hours trying to figure out the right way to instantiate a main method (CreationComplete), because embedding as3 in mxml isn't the same as embedding js in html-- embeded code is assumed to be it's own abstract class or something(can't do anything outside of functions) when its inside a <mx:Script> tag.
Matteo Dec 18, 2010 4:51 PM
Try my new flash game
hi guys i've just create this flash game, i'll appreciate feedback. You will find also the .FLA source on the website

http://matteoinvernizzi.it/projects/babbonatale/
Astruvis Sep 8, 2010 5:36 PM
For people who don't get this
Look for mxmlc.exe, drag the mxml file into this, and it will compile it for you.
I couldn't figure our how to use the command prompt to do this, but I assumed this would pass the file to the compiler, and it worked.
Shittu Mansoor Aug 17, 2010 2:17 PM
A newbie here !!!!
all of my life i have dream to be come what many pple think and even say i cannot be .... i dream to be a Programmer that write a code which is an hot-cake world wide.......but i am still getting it confusing cos the whole thing is fussing-up dont even know what to say or do.....but all i know and believe is that one day i shall assume and aspire this Unprecendented Dream of mine.
yakcamydna Jul 13, 2010 6:49 PM
completing step1. the initial main.mxml file
Hi

If your having trouble getting past step one please read on..

Take the code "<?xml.....<mx:Application.." from the tutorial. Save it in a text editor as main.mxml (I saved my mine in ~/Destop/game/ )

Then where the tutorial says input "mxmlc main.mxml" into the command line- replace this with "mxmlc ~/Desktop/game/main.mxml" (or wherever you game project file is located)- your main.swf file will appear in the same folder

Hope this is clear and helps someone. Its the first thing iv ever worked out for myself in Linux so feeling quite chuffed :)

In fact I almost want to give extra props to Matthew because not only has he given us a great tutorial- but by not taking us in baby steps he's actually made me engage my brain for the first time tonight rather than simply cut and pasting code.

on to step two- wish me luck :)

thanks, andy
Flash Game Developer Jun 30, 2010 4:59 AM
Need Basic Understanding
this is great post but need to know basics to work with useful information.
kavita Jun 29, 2010 8:19 AM
rotating benzene ring
i want to rotate benzene ring on my canvasor either tell me how to rotate canvas
b.sanjay Jun 19, 2010 2:58 AM
hai
it is very nice
Matthew Casperson Apr 1, 2010 8:42 PM
RE: Flash Game Development with Flex and Actionscript - Getting Started
Take a look at the FlashPunk Acrobat class (http://www.brighthub.com/hubfolio/matthew-casperson/articles/66964.aspx). It has the ability to rotate an image.
Alin Apr 1, 2010 9:34 AM
Rotation
Hi,
How can I rotate an image using the knowledge from this tutorial?
Thanks in advance,
Alin
Mark Feb 22, 2010 7:43 AM
FG
I don't like flash games because they are to simple. I think that children like them essays ...
shaguf Feb 22, 2010 2:07 AM
India Game Developer Summit Announces Program and Speakers
Carl Jones of Crytek, Ashu Rege of NVIDIA, Harish Sivaramakrishnan of Adobe, Keita Iida of NVIDIA, Philippe Vachey of DSK Supinfocom, Robin Alter of Kreeda Games, Tridib Chowdhury of Adobe, Rev Lebaredian of NVIDIA, Sumit Gupta of BitRhymes, Hemanth Sharma of Adobe, Simon Green of NVIDIA, Varun Nair of Blue Frog, Krishna Pediredla of Drona Labs, Jithin Rao of Ubisoft and Imran Khan of FXLabs is coming this february to India's first and independent annual summit for the game development ecosystem - India Game Developer Summit (gamedevelopersummit dot com) to speak on various topics. For more info please log on to gamedevelopersummit dot com
madhuka Dec 9, 2009 4:29 AM
Great work!!
I was searching such one it gr8!!
keep it Up!!

Super Work Mathew
Matthew Casperson Dec 1, 2009 5:20 PM
RE: Flash Game Development with Flex and Actionscript - Getting Started
I don't actually have much experience with Flash itself, but from what I can gather its strength lies in its animation and creating fancy user interfaces. So I guess it depends on what sort of game you were making. If it was a point and click game, Flash would probably be the better choice. If it is something more involved, then most of the logic will be implemented in ActionScript anyway, which means Flash or Flex would do.

I prefer Flex simply because the development flow is more familiar to other development platforms that I had experience with.
AlexC Dec 1, 2009 12:08 PM
Great article!
Thank you for the great article. I'm tasked with building about 5 different games for kids 6 - 10, showing them how to eat healthier. I have Flex experience building RIA but never games.

Some folks say Flash is better for building games but my Flash experience is very basic. You think my task is doable in Flex alone? Thanks.
Mat Nov 24, 2009 1:02 PM
i have great ideas
hey i have great ideas , my only failing is im somewhat of a dreamer , and i have very ittle programming skills , so i want to link up with someone who could make my games , i wouldnt even care about credit , i just want to have some good game to play :P
Mark Gale Nov 24, 2009 10:39 AM
Monetizing
very good article! i'd like to add that monetizing is an important part of game development... i've tried at www.gameknob.com and have good earnings at my game
Nikos Nov 20, 2009 7:49 AM
RTS game in flex 4
I'm developing a RTS game in flex 4. I'm using the new spark components to base my game objects on. I was wondering if your code(before I sit down and work through theis tut) will help me with the RTS collision detection system I'm having a hard time coding, or could you point me to other resources?

thanks
Pranab Nov 4, 2009 5:20 AM
Flash Action Scriipt
I want to develop games using flash action script
le thanh tam Nov 3, 2009 3:21 PM
how to create webgame by using flex 3 and as3
I have just leant to deveplop webgame.I don't know where to start.Can you help me?
Matthew Casperson Oct 26, 2009 5:15 PM
RE: Flash Game Development with Flex and Actionscript - Getting Started
Use the Wii remote in Flash:
http://wiiflash.bytearray.org/

As for other controllers, I'm not sure. I have never done it myself.
OmegaWarrior Oct 26, 2009 2:51 PM
Controller input?
Do you know if there is any way to get a flex/flash application to accept input from a game controller?
Matthew Casperson Oct 20, 2009 5:44 PM
Not for the inexperienced?
You are probably best using something like coppercube (http://www.brighthub.com/hubfolio/matthew-casperson/articles/46942.aspx). That will let you create a Flash application with no programming.
Anonymous Oct 20, 2009 9:37 AM
Not for the inexperienced?
I was so excited when I read this and went through all the download steps, just to find that this is not for someone with zero experience.

I got lost at the very first step, not knowing how to even open Flex SDK, where to, or if to copy paste the the first bit of pragramming etc etc.

I've always wanted to be a games programmer, but that was non-existant in my country when i was a youngster. I have so many ideas... any suggestions on where to go for step-by-step instructions for someone with no experience whatsoever?
Matthew Casperson Oct 11, 2009 5:48 PM
Keyboard input
Check out the demo at http://www.brighthub.com/hubfolio/matthew-casperson/blog/archive/2009/09/13/keycode-in-flash-flex.aspx

It is a simple application the responds to keyboard input, and displays the corresponding key codes. Generally speaking though it should be as simple as attaching a listener to the keyboards events for the stage object.
Jason French Oct 11, 2009 5:55 AM
Capturing Keyboard events
hi there,

great tutorial by the way. I'm a C/C++/BlitzMax coder, and I was initially disappointed at Flex's lack of support for the tried and true double-buffering / clear screen, update, render, swap buffers style of development. After digging through this tutorial I found that Flex does support it, you just have to know the right classes to use and whatnot.

One thing that's still left puzzling me is Keyboard input. All my mouse events trigger correctly, but it seems as though trying to add a listener for KeyboardEvent's isn't working. I've looked up all the common solutions (stage.addEventListener, setFocus, yadda yadda) and those aren't seeming to work yet. Just wondering if this something that you have some known tricks for.

Thanks in advance,

Jason
Matthew Casperson Oct 6, 2009 6:27 PM
translation
I'm glad you found the articles useful. I have no objection with the translation, as long as you link back to my blog: http://www.brighthub.com/hubfolio/matthew-casperson.aspx
Thanks
Alex Oct 6, 2009 12:20 PM
Great job!
Thank you!
I'v been looking for something like this for some time. I have even deside to spend some time to translate you article into russian (any objections?).

PS: look for russian version at http://makeyourgame.org.ua/flexfighters/part01.php
Soshimo Oct 1, 2009 12:12 PM
Outstanding Tutorial
As a long time J2ME/BREW developer I am impressed with your OO principles and design here. Excellent tutorial to show the capabilities of AS as well as to show the RIGHT way to design and code in an OOP language. Great techniques here that I have only seen in production level code (and sometimes production level code doesn't even come close to this level). Out freaking standing!
Matthew Casperson Sep 28, 2009 1:49 AM
Bounce
You might find that your IDE has automatically imported the Bounce class in the mx.effects.easing package. If so just remove the import statement. Otherwise you can change the name of the Bounce class mentioned in the article to something like MyBounce. The Bounce class is only used in that one article and doesn't form part of the final game.
Matthew Casperson Sep 28, 2009 1:40 AM
Code issue
I would recommend using Flex Builder - a trial copy can be found on the Adobe website. However if you want the code can be copied into any text editor and saved as a normal file. The compiler that comes with the free Flex SDK can then compile these text files with the source code into a SWF file.
Socapex_2K Sep 28, 2009 12:17 AM
multiname reference
Hello, I've just started the tutorial and I get an error after completing section 3. When trying to compile (flex builder), the Bounce class causes this error (in the main.mxml): Can not resolve a multiname reference unambiguously. It is referring to Bounce and mx.effects.easing:Bounce... Is there a way to specify I want to startup Bounce.as?
Jaime Sep 27, 2009 11:30 PM
Help
This may sound like a stupid question, but where are you instructing that the actionscript code be pasted? Into text pad? I think I may be having problems getting Flex to install, does it come with some sort of development environment that pertains to these tutorials?
asimkh Sep 7, 2009 8:23 AM
Flash gaming
Good Article - am looking for multiplayer flash CS3 gaming, if you have knowledge do send mail
Asim Kh Sep 7, 2009 7:55 AM
GooD info
am looking forward for multiplayer flash game development tutorial. I came across your article and its very good.

if any person knows Flash Multiplayer game tutorials and learning articles, flash game engines do send links to

multimedia99[at]gamil[dot]com
Reuben Sep 3, 2009 12:59 AM
Sweet
Thaks a million for the tutorial., Matthew. I've gotta make a game using Flex. This helped loads.
john Aug 9, 2009 3:12 PM
excellent job
Been looking hard for a tutorial like this. Yr a dang Prometheus.

I second the request for backend server java stuff examples.
Sumaia Jul 25, 2009 2:25 AM
thank you
Thank you very much, , It's a dream to learn to make a game using action script. Since I am a teacher, I need to make learning games for my students.
We are waiting for more.. go on!
Avais Shaikh Jul 3, 2009 4:11 AM
good tutorial
Hi Mathew,
Very nice tutorial. Flex and ActionScrpt are indeed very powerful. I learned a lot form this tutorial. It would be great if you could provide some examples for 2.5D animating and like integrating with some back-end server like Java. Would be very helpful.

Thanks
 
blog comments powered by Disqus
Email to a friend