Flash Game Development with Flex and Actionscript - Embedding Resources and Adding Game Objects (Page 4 of 4)

Article by Matthew Casperson (4,883 pts ) , published Nov 5, 2009

ResourceManager.as

package

{

import flash.display.*;

public final class ResourceManager

{

[Embed(source="../media/brownplane.png")]

public static var BrownPlane:Class;

public static var BrownPlaneGraphics:GraphicsResource = new GraphicsResource(new BrownPlane());

}

}

In order to use the embedded graphics we need to separate the alpha (or transparent) layer from the original picture. If you look back to the copyToBackBuffer function in the GameObject class you will see that the copyPixels function uses both the bitmap and alphaBitmap properties of a GraphicsResource object. Extracting and exposing these elements from an embedded picture is the sole purpose of the GraphicsResource class. Lets take a look at the GraphicsResource class now.

GraphicsResource.as

package

{

import flash.display.*;

public class GraphicsResource

{

public var bitmap:BitmapData = null;

public var bitmapAlpha:BitmapData = null;

public function GraphicsResource(image:DisplayObject)

{

bitmap = createBitmapData(image);

bitmapAlpha = createAlphaBitmapData(image);

}

protected function createBitmapData(image:DisplayObject):BitmapData

{

var bitmap:BitmapData = new BitmapData(image.width, image.height);

bitmap.draw(image);

return bitmap;

}

protected function createAlphaBitmapData(image:DisplayObject):BitmapData

{

var bitmap:BitmapData = new BitmapData(image.width, image.height);

bitmap.draw(image, null, null, flash.display.BlendMode.ALPHA);

return bitmap;

}

}

}

GraphicsResource has the two properties mentioned above: bitmap and bitmapAlpha. These are extracted from an embedded image passed into the constructor by the createBitmapData and createAlphaBitmapData functions. Once extracted they are in a form that is convenient to use with the copyPixels function used in the GameObject’s copyToBackBuffer function.

So what have we achieved here? We have built on the groundwork laid in parts 1 and 2 of the series to embed some resources (ResourceManager and GraohicsResource), created the base class for more specific game objects (GameObject), and then finally combine it all to add a moving object to the screen (Bounce).

In part 4 of the series we will add an object that the player can control, as well as create a scrolling background for the player to fly over.

Go back to Flash Game Development with Flex and ActionScript

Related Files

Images

The end result
Showing page 4 of 4

11 Comments

Showing page 1 of 2 (11 Comments)
Oct 19, 2009 11:30 PM
Aryadi
It worked
It worked Matthew! it compiled just fine and the swf is playable errorlessly

thanks for the help! :D
Oct 18, 2009 2:49 AM
RE: Flash Game Development with Flex and Actionscript - Embedding Resources and Adding Game Objects
That error just means that Flex can't find the image. Make sure the relative file names are valid, or you could copy the images to the same folder as the *.as files and reference them like:

[Embed(resource="brownplane.png")]
Oct 16, 2009 11:50 PM
Aryadi
A compile time error
wow that was fast!

anyway, it turned out to be a typo (maybe I was sleepy, I coded it last night before go to sleep)

but then another error showed up at compile time, it's this:

D:\Flex app\tutorial dari brighthub\ResourceManager.as(7): col: 4: Error: unable to resolve '../media/brownplane.png' for transcoding

[Embed(resource="../media/brownplane.png")]

D:\flex app\tutorial dari brighthub\ResourceManager.as(7): col: 4: Error: unable to transcode ..media/brownplane.png


there, I don't know what's wrong I copy-pasted your code from line 7 but this compile time error still occured
Oct 16, 2009 11:13 PM
Error
I'm not sure. Try downloading the source code (http://www.brighthub.com/hubfolio/matthew-casperson/media/p/49174.aspx) and see if that works.
Oct 16, 2009 10:52 PM
Aryadi
An error probably in the game state
I can compile the game correctly, but after I clicked on the start button an error showed up

TypeError: Error #1115: BrownPlane is not a constructor.
at ResourceManager$cinit()
at global$init()
at Bounce/startupBounce()
at GameObjectManager/startup()
at main/enterGame()
at main/___main_State1_enterState()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.states::State/http://www.adobe.com/2006/flex/mx/internal::dispatchEnterState()
at mx.core::UIComponent/applyState()
at mx.core::UIComponent/commitCurrentState()
at mx.core::UIComponent/setCurrentState()
at mx.core::UIComponent/set currentState()
at main/startGameClicked()
at main/__btnStart_click()

after that the brown plane didnt' showed up neither the blue color that used to showed up

when I did the previous tutorial it was okay the error came up after I finished with this part of lesson
Oct 15, 2009 5:11 PM
ZOrder
The order of the baseObjects ArrayCollection, held by the GameObjectManager, is determined when new BaseObjects are created and then added to the baseObjects collection in the insertNewBaseObjects function.

To modify the zorder when an object is in use you would have to sort the baseObjects collection again. You could add a function to the GameObjectManager to manually trigger the sorting of the baseObjects collection after you modify an objects zorder.
Oct 15, 2009 2:01 PM
EternalFaith
zOrder
How can i change the zOrder of a element while it is inuse?
Oct 13, 2009 7:58 AM
Federico
Resolved!
Ok I have resolved my issue.

for the first message simply i have missed the "public class GameObject" declaration (!) for the second problem i have deleted this declaration "import mx.effects.easing.Bounce;"

Bye :-)
Oct 12, 2009 1:22 PM
Federico
can't compile
Hi all!

please give my an hand with my problem: i can't compile.
When I try using mxmlc command it responds with "Error: unable to open"
I've also tried with Flex Builder 3 (that I used for writing the code) but it found 2 errors in code:
"A file found in a source-path can not have more than one externally visible definition" (referring to GameObject.as)
"Can not resolve a multiname reference unambiguously. Bounce and mx.effects.easing:Bounce" (referring to GameObjectManager.as)

Please help me :''''(
Thanks anyway for this great tutorial!

Bye
Oct 1, 2009 4:20 AM
aki
brownplane.png
For people coming to see this tutorial in the future, you might want to link the brownplane.png after the ResourceManager.as definition. Had to go download and extract the zip just for that. Of course any png would work, but it's just a minor inconvenience.

(And btw, you typoed "GraohicsResource" in the second to last chapter.)
Showing page 1 of 2 (11 Comments)
 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development