Flash Game Development with Flex and Actionscript - Getting Started

Article by Matthew Casperson (4,877 pts ) , published Sep 23, 2009

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

Related Files

Images

CompilingThe Flash game

21 Comments

Showing page 1 of 3 (21 Comments)
Nov 4, 2009 5:20 AM
Pranab
Flash Action Scriipt
I want to develop games using flash action script
Nov 3, 2009 3:21 PM
le thanh tam
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?
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.
Oct 26, 2009 2:51 PM
OmegaWarrior
Controller input?
Do you know if there is any way to get a flex/flash application to accept input from a game controller?
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.
Oct 20, 2009 9:37 AM
Anonymous
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?
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.
Oct 11, 2009 5:55 AM
Jason French
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
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
Oct 6, 2009 12:20 PM
Alex
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
Showing page 1 of 3 (21 Comments)
 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development