This is the third part of Making Games in ActionScript 3 using FlashDevelop.
Last time we got our first enemy on the stage. Now we’ll take a quick look at embedding other formats into your classes. Grab the gnome here to the right and put him in your assets folder.
The syntax is basically the same to embed other formats (png/jpg).
[Embed(source='../../../../../../assets/gardengnome.png')]
FlashDevelop will generate this little string for you if you doubleclick your wanted image in the Project panel. Place the [Embed] tag right before the class declaration as we did last time.
If you compile and run now you will get a runtime exception from the player. This is because our enemy is still defined as a Sprite, and sprites are vector based. We need to change the base class to Bitmap. Do that, and make sure your imports are in order before you compile. Now it all should run just fine again.
Flash does not resize your image, it will be embedded in it’s full resolution. So make sure that it’s a proper size before embedding it, resizing it in code will just add unnecessary bloat to your file and will affect performance.
Having to change the base class like this can be a bit of a hassle, especially when you have multiple levels of inheritance going (maybe a baseclass like GameObject). So there’s a cleaner way to do this. Create these two properties on your enemy:
private static const GfxGnome:Class; private var gfx:Bitmap;
The first one is both a static constant, this means this property is attached to the actual class, and not to the instances of it you create. Wether you make them private or public is up to you, I prefer having things private until otherwise needed, so that’s what I went with.
For now we set the base class for the enemy back to Sprite. But instead of having the [Embed]-tag before the Enemy class we put it before the GfxGnome class. This will make the gardengnome.png attach to that class instead.
Now we need to instantiate that into our gfx property. We do that in the constructor like this:
gfx = new Enemy.GfxGnome();
Since GfxGnome is attached to the class and not this specific instance we need to specify that by using Enemy before it’s name.
If you run your code now you will notice that nothing is showing up. This is because we need to add our graphics as a child, right now they’re just hiding out in memory somewhere.
Adding the graphics as a child works the same way here as it did in our Main class where we first added the Enemy:
addChild(gfx);
Now you should be all set for getting graphics into your games. If you got lost somewhere on the way, the project files are available for download. Next time we’ll make stuff move around!







This is my first experience of programming, so it was right about here that I started to think “Huh? Add the what to the what? Where am I supposed to put this code?” I was very relieved to see the project files download link at the end there!
Thank you for having the patience to think like a newcomer.
Nice tut
clear enough to understand
thx
only one thing: remember that many people are going to be moving through these sequentially, like me.
Since I try to follow all tutorials to the letter (not bringing in habits from other IDEs and APIs with which I’m already familiar), I didn’t know I was going to be deleting the leek picture.
I also didn’t know where to put the properties, or whether I had to change the extension once I added the Gfx properties.
The source code helps, but when you use a new word (property, in this case), I would suggest showing the whole class so the student can know that they’re headed in the right direction.
All in all, these have been great, thanks!
I’m planning to do a second sweep on all these tutorials eventually and correct any mistakes pointed out in the comments, so your feedback is much appreciated!
Very Good, I didn’t get it all so I just downloaded the source code file before I started this part. It helped so much! Thanks and keep putting your file down at the bottom for us to download because without it, I would have been lost WAY ago! Thanks!