BUILDING A SIMPLE GAME WITH FLASH PRO
Adobe MAX 2013 Lab by Tom Krcha (Sr. Creative Cloud Evangelist at Adobe) Follow me on Twitter: @tomkrcha
Adobe
MAX
2013
Lab
by
Tom
Krcha
(Sr.
Creative
Cloud
Evangelist
at
Adobe)
Follow
me
on
Twitter:
@tomkrcha
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>BUILDING</strong> A <strong>SIMPLE</strong> <strong>GAME</strong> <strong>WITH</strong> <strong>FLASH</strong> <strong>PRO</strong><br />
Adobe MAX 2013 Lab by Tom Krcha (Sr. Creative Cloud Evangelist at Adobe)<br />
Follow me on Twitter: @tomkrcha<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 1
Prerequisites:<br />
-‐ Flash Pro CS6<br />
-‐ Flash Builder 4.7<br />
-‐ Knowledge of AS3<br />
-‐ Citrus Engine library (included with demo assets)<br />
Demo Assets:<br />
-‐ Beginner tutorial for Citrus Engine<br />
o MAXBeginner.zip<br />
o Quick intro into Citrus Engine for platformer based games<br />
-‐ Level Building tutorial<br />
o MAXLevelBuilding.zip<br />
o Desiging a level in Flash Professional<br />
o Launching the level with Citrus Engine in Flash Builder<br />
-‐ DragonBones Extension<br />
o dragonbones_v2.0.zip<br />
o install into Flash Pro<br />
-‐ Animations tutorial<br />
o MAXAnimations.zip<br />
o Using Dynamic spritesheets via a SWF animation<br />
o Using Spritesheets<br />
o Using Skeletal animations via DragonBones extension<br />
All the zipped files are ready made Flash Builder projects.<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 2
BEGINNER’S TUTORIAL<br />
You are going to learn Citrus Engine (http://citrusengine.com) basics. Citrus Engine<br />
is lightweight GPU accelerated game engine for Flash and AIR and includes<br />
platformer kit for side-‐scroller platform games. It runs on top of Starling Framework<br />
(2D) and Away3D framework (3D) and you can choose one of the physics engines<br />
that are included as well – Box2D, NAPE, AwayPhysics. For our game we are going to<br />
use Starling + Box2D combination.<br />
Setup the most basic scene with Citrus Engine.<br />
Start Flash Builder 4.7 and later we are going to use Flash Pro for level design and<br />
animations , but project management and coding will be done in Flash Builder as it’s<br />
more suitable for it. Design = Flash Pro, Programming = Flash Builder.<br />
Create a new project in Flash Builder<br />
File -‐> New -‐> ActionScript Project<br />
(name the project MAXBeginner or similar)<br />
Link Citrus library<br />
Right click on the project -‐> Properties -‐> ActionScript Build Path -‐> Add SWC<br />
(locate CitrusEngine’s SWC file) -‐> OK<br />
In the main class add following code:<br />
package<br />
{<br />
import citrus.core.starling.StarlingCitrusEngine;<br />
}<br />
[SWF(frameRate="60", width="1024", height="768")]<br />
public class MAXBeginner extends StarlingCitrusEngine<br />
{<br />
public function MAXBeginner()<br />
{<br />
setUpStarling(true);<br />
}<br />
}<br />
state = new GameState();<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 3
Create new class GameState that extends StarlingState<br />
File -‐> New -‐> ActionScript Class<br />
package<br />
{<br />
import Box2D.Dynamics.Contacts.b2Contact;<br />
import citrus.core.starling.StarlingState;<br />
import citrus.objects.platformer.box2d.Coin;<br />
import citrus.objects.platformer.box2d.Enemy;<br />
import citrus.objects.platformer.box2d.Hero;<br />
import citrus.objects.platformer.box2d.MovingPlatform;<br />
import citrus.objects.platformer.box2d.Platform;<br />
import citrus.physics.box2d.Box2D;<br />
public class GameState extends StarlingState<br />
{<br />
public function GameState()<br />
{<br />
super();<br />
}<br />
override public function initialize():void{<br />
super.initialize();<br />
var physics:Box2D = new Box2D("box2D");<br />
physics.visible = true;<br />
add(physics);<br />
var floor:Platform = new Platform("floor",{x: 512, y: 768, width:1024, height: 40});<br />
add(floor);<br />
var p1:Platform = new Platform("p1", {x:874, y: 151, width: 300, height:40});<br />
add(p1);<br />
var mp:MovingPlatform = new MovingPlatform("mp",{x:220, y:700, width:200, height: 40,<br />
startX: 220, startY: 700, endX:500, endY: 151});<br />
add(mp);<br />
var hero:Hero = new Hero("hero", {x:50, y:50, width: 70, height: 70});<br />
add(hero);<br />
var enemy:Enemy = new Enemy("enemy", {x:900, y:700, height:70, width: 70, leftBound: 10,<br />
rightBound:1000});<br />
add(enemy);<br />
var coin:Coin = new Coin("coin", {x: 967, y: 90, width: 79, height: 79});<br />
coin.onBeginContact.add(function(c:b2Contact):void{<br />
trace("CONTACT");<br />
});<br />
add(coin);<br />
}<br />
}<br />
}<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 4
LEVEL <strong>BUILDING</strong> TUTORIAL<br />
In this part you are going to learn how to build basic levels for your game using<br />
Flash Pro visually.<br />
Open Level1.fla and look into Library panel. You should see Box2D objects:<br />
This gives you a possibility to layout easily<br />
components on the stage.<br />
Hero, Platform, Enemy, Coin, Sensor, Cannon,<br />
Box2DPhysics<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 5
You can set component parameters in<br />
Properties Panel.<br />
Add Hero on stage and set the view property<br />
to a PNG file that will represent the hero.<br />
You can also adjust jumpHeight and<br />
jumpAcceleration.<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 6
Loading a level into your game<br />
In this part you will load a SWF file exported from original Level.FLA file. You can<br />
embed the SWF or use Loader. Loader is better as it makes sure you always have the<br />
latest file.<br />
package<br />
{<br />
import flash.display.Loader;<br />
import flash.events.Event;<br />
import flash.net.URLRequest;<br />
import citrus.core.starling.StarlingCitrusEngine;<br />
[SWF(frameRate="60",width="1024",height="768")]<br />
public class MAXLevelBuilding extends StarlingCitrusEngine<br />
{<br />
public function MAXLevelBuilding()<br />
{<br />
setUpStarling(true);<br />
}<br />
}<br />
var loader:Loader = new Loader();<br />
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _levelLoaded);<br />
loader.load(new URLRequest("../assets/Level1.swf"));<br />
}<br />
private function _levelLoaded(evt:Event):void {<br />
}<br />
state = new Level1(evt.target.loader.content);<br />
evt.target.removeEventListener(Event.COMPLETE, _levelLoaded);<br />
evt.target.loader.unloadAndStop();<br />
Now you need to setup the Level1 class, that consumes<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 7
Initializing the level<br />
Define physics. Use ObjectMaker2D to init the level from a MovieClip. Setup the hero<br />
and the sensor.<br />
package<br />
{<br />
import flash.display.MovieClip;<br />
import Box2D.Dynamics.Contacts.b2PolygonContact;<br />
import citrus.core.starling.StarlingState;<br />
import citrus.objects.platformer.box2d.Hero;<br />
import citrus.objects.platformer.box2d.Platform;<br />
import citrus.objects.platformer.box2d.Sensor;<br />
import citrus.physics.box2d.Box2D;<br />
import citrus.utils.objectmakers.ObjectMaker2D;<br />
public class Level1 extends StarlingState<br />
{<br />
protected var level:MovieClip;<br />
private var hero:Hero;<br />
public function Level1(_level:MovieClip)<br />
{<br />
super();<br />
level = _level;<br />
var objectsUsed:Array = [Hero, Platform, Sensor];<br />
}<br />
override public function initialize():void{<br />
super.initialize()<br />
}<br />
var physics:Box2D = new Box2D("physics");<br />
physics.visible = true;<br />
add(physics);<br />
ObjectMaker2D.FromMovieClip(level);<br />
hero = getObjectByName("hero") as Hero;<br />
hero.acceleration = 0.2;<br />
var sensorGate:Sensor = getObjectByName("sensorGate") as Sensor;<br />
sensorGate.onBeginContact.add(sensorGateOnBeginContact);<br />
private function sensorGateOnBeginContact(contact:b2PolygonContact):void<br />
{<br />
trace("Sensor touched");<br />
}<br />
}<br />
}<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 8
ANIMATIONS’S TUTORIAL<br />
This tutorial will discuss 2D Animations support in Citrus Engine. Basically anything<br />
that is supported by Starling is supported by Citrus as well, however Citrus simplifies the<br />
workflow even more.<br />
There are currently 3 types of supported animations and all of them can be created within<br />
Flash Pro:<br />
• Dynamic sprite sheets from SWFs<br />
• Pregenerated sprite sheets<br />
• Skeletal animations via Dragon Bones extension<br />
DYNAMIC SPRITE SHEETS FROM A SWF FILE<br />
The easiest method for attaching animations in Citrus.Just set a SWF file to a view<br />
parameter in Components parameters in Properties panel and Citrus generates a sprite<br />
sheet on the fly and assigns it to an instance on stage in runtime.<br />
Coin<br />
The actual animation looks like this:<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 9
Hero<br />
-‐ For a hero, you have to define several MovieClips like walk, jump, idle.This is<br />
how the jump animation looks like.<br />
Put all animations on stage and give them an instance name: walk, jump, idle in the<br />
Properties panel.<br />
To make dynamic spritesheets work, you don’t have to write a single line of code, it<br />
just works!<br />
The drawback is of dynamic spritesheets is init time as all animations have to generated<br />
at runtime and possibly the memory usage. The next two approaches are more controlled<br />
environments. This one is the simplest.<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 10
PREGENERATED SPRITE SHEETS<br />
Beginning with Flash Pro CS6, you can export sprite sheets from timeline animations.<br />
Select all MovieClips that you want to export in the Library panel (idle, walk, jump),<br />
right-click and choose Generate Sprite Sheet…<br />
You should get something similar to this image + an XML file, defining the animations<br />
sequence.<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 11
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
As you can see, each sequence begins with an animation name: jump, walk, idle.<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 12
Embed the pregenerated sprite sheet to your level class by creating two variables this<br />
way:<br />
[Embed(source="../levels/hero.png")]<br />
private var heroAnimBitmap:Class;<br />
[Embed(source="../levels/hero.xml",mimeType="application/oc<br />
tet-stream")]<br />
private var heroAnimXML:Class;<br />
Now you can add this animation sequence to a hero using this code:<br />
var ta:TextureAtlas = new<br />
TextureAtlas(<br />
Texture.fromBitmap(new<br />
heroAnimBitmap()), XML(new<br />
heroAnimXML()));<br />
var animationSeq:AnimationSequence = new<br />
AnimationSequence(ta,["walk","idle", "jump"],"idle",24);<br />
hero.view = animationSeq;<br />
Where ["walk", "idle", "jump"] is an array of available animations and “idle” is the<br />
default state, 24 defines the frame rate (fps).<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 13
SKELETAL ANIMATIONS <strong>WITH</strong> DRAGON BONES<br />
Dragon Bones is an open source skeletal animation extension for Flash Pro that allows<br />
you to export your animations into 3 files – texture atlas (XML + PNG) and animation<br />
metadata.<br />
Install Dragon Bones extension (SkeletonAnimationDesignPanel.zxp) using Adobe<br />
Extension Manager and activate the panel in Flash Pro. Then select a MovieClip<br />
animation in the Library panel and click Import button in Dragon Bones. This will import<br />
the timeline animation and create a sprite sheet composed only of separate elements used<br />
within the animation, unlike frame-by-frame snapshots as in the pregenerated or dynamic<br />
spritesheet examples above. Using Dragon Bones is very memory efficient.<br />
The animations sequence for Dragon Bones has to be in a single MovieClip and each<br />
animation begins with a labeled frame (e.g. walk, jump, idle…).<br />
Once you are done, select the MovieClip (“allAnimations” in this case) in Library, open<br />
SkeletalAnimationDesignPanel and hit Import button. You should see this:<br />
Hit Export button and save the exported file as “PNG (XML merged)“, this will encode<br />
all animation information inside PNG, so you will have only a single file that defines<br />
assets and animations.<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 14
In the code, embed the png this way:<br />
// Dragon Bones<br />
[Embed(source="../levels/herodb.png",mimeType="application/<br />
octet-stream")]<br />
private var heroAnimBitmapAndXML:Class;<br />
And assign the hero.view property this way in the initialize function. If your game is<br />
running e.g. 60 fps and your animation was done within a lower frame rate (fps), you can<br />
adjust the animation speed via timeScale property as demonstrated below.<br />
/** SKELETAL ANIMATION **/<br />
var armature:Armature;<br />
var factory:StarlingFactory = new StarlingFactory();<br />
factory.addEventListener(Event.COMPLETE,<br />
function(event:Event):void{<br />
armature = factory.buildArmature("allAnimations");<br />
armature.animation.timeScale = 0.7;<br />
hero.view = armature;<br />
});<br />
factory.parseData(new heroAnimBitmapAndXML());<br />
DragonBones generated spritesheet looks like this, so you can see there are only<br />
elements that are being animated. The PNG file contains also XML file that describes the<br />
animations.<br />
`<br />
Building a simple game with Flash Professional. MAX 2013 Lab by Tom Krcha (@tomkrcha) 15