Monday, 5 September 2011

FlashDevelop embedding images

In the Flash IDE images can be embedded by importing into the library and using the linkage properties, or by dragging onto the stage. In FlashDevelop the method is slightly different, you need to use the Flex [embed] tag to embed images.

I assume you have a project setup with the usual 3 folders (bin, src, and lib) and you have an image file inside the lib folder. Don’t worry if you don’t have this project setup as this will still work without the folder structure.

Right click on the image and select the ‘Generate Embed Code’ from the drop down menu.



The embed code should automatically be loaded in the class file. Underneath the the Embed code add the variable declaration for the image. This class variable is used to instantiate the embedded image. The code should look like this.

[Embed(source = "../lib/test-image.jpg")]
private var TestImage:Class;

To add the image onto the stage, you need to create a new instance of the TestImage class. You can use the following code:

package 
{
 import flash.display.Bitmap;
 import flash.display.Sprite;
 import flash.events.Event;
 
 public class Main extends Sprite 
 {
  [Embed(source = "../lib/test-image.jpg")]
  private var TestImage:Class;
  
  public function Main():void 
  {
   if (stage) init();
   else addEventListener(Event.ADDED_TO_STAGE, init);
  }
  
  private function init(e:Event = null):void 
  {
   removeEventListener(Event.ADDED_TO_STAGE, init);
   
   var myImage:Bitmap = new TestImage();
   addChild(myImage);
  }
  
 }
}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP