Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So the first type of asset that I'm going to show you how to include is a simple image asset. So in our game we have a background which consists of some stars, and that's just a simple PNG file. If we look in our assets folder, it's called sky.png and that's what it looks like. So we're going to want to embed this so that we can use it in our game. So the way we embed a PNG or any other type of image asset, and you're used to this if you've been doing Flash for a while, is to use the Embed Metadata.
So we can say Embed and then give a source property and give it the path to the PNG file that we want to include. Well that's in the assets folder, and it's called sky.png. And then next we need to give a class which is going to be used to instantiate that image. So again, we're doing everything as static, but this is going to be a private static var sky and this is going to be of type Class.
Now when we're dealing with images, we're actually going to be creating textures out of these images, because remember, when you're running on the GPU using Stage 3D, everything is essentially triangles and the way in which you display things is to actually put a texture onto those triangles. Now it's important that we don't repetitively create textures. So for instance, this sky image we're going to create a single static texture here in the Assets class and any time we want to use that, anywhere in our game, we're going to use that single texture object, because remember everytime you create a texture, it has to upload it to the GPU and it's also increasing the memory that your game uses.
So you want to create the very least amount of texture as possible and make sure you reuse them. So we're going to create a texture out of this and this is going to be a public static texture, so that we can reference it from outside of this class. So we're going to say public static var and I'm going to call this skyTexture, and this is going to be of type texture, and the package is starling.textures.Texture. Okay, so now with that created inside of our init function, we're going to instantiate that texture or create that texture.
So I'm going to say skyTexture =, now I could say new texture here, but there is some helpful static factory methods on the texture class that we can use. So I'm going to say texture, and there is a fromBitmap static function available, in this we pass in a bitmap instance which is going to be used to create the texture, and for that we pass in a new instance of that sky embedded bitmap. So now after this initialization function is called from our game, main game constructor function, anytime we need to display that sky image, we're going to reference this skyTexture.
So again, it's only going to be in memory one time uploaded to the GPU, and anytime we need to use it we're going to reference that single texture. So again, that's the basics of incorporating an image file such as a PNG and a JPEG and creating it into a texture for Starling.
Get unlimited access to all courses for just $25/month.Become a member