Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Flash AS Tutorial: Load External Image to Loader and Sprite (1)

There are some ways to load external images into a Flash Movie with Flash ActionScript. In this Flash tutorial, we create a Sprite and a new Loader. Then load the external image into the Loader, add the Loader into the Sprite. And finally display the Sprite on the MainTimeline of Flash Movie.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Actually you can also use Flash ActionScript to create a new Sprite and create a new Loader. Then load the external image into the Loader, add the Loader into the Sprite. And finally display the Sprite on the MainTimeline of Flash Movie.

The complete Flash Movie is shown as above, you may try how it works before you start this ActionScript tutorial.

Flash ActionScript Codes:

var imageLoaded:Boolean = false;

 

// Create a new Sprite for the content to load into
var logo:Sprite = new Sprite();

 

// Set display location of the Sprite
logo.x = 160;
logo.y = 50;

 

// Create a new Loader to load the image
var myLoader = new Loader();


image1_btn.addEventListener(MouseEvent.CLICK, loadImage1);

 

function loadImage1(evt:MouseEvent):void {

// URL of the external image content
myLoader.load(new URLRequest("logo.png"));

}

 

// Listen error events for the loading process
myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError);
myLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError);

 

function loadError(event:ErrorEvent):void {

// Display error message to user in case of loading error.
output_txt.text = "Sorry that there is an error during the loading of an external image. The error is:" + "\n" + event;

}

 

// Listen when the loading of image is completed
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

 

function loadComplete(event:Event):void {

// Add the Loader on the Sprite when the loading is completed
logo.addChild(myLoader);
// Display the Sprite on the MainTimeline
addChild(logo);
// Set imageLoaded to true so that the Image can be unloaded
imageLoaded = true;

}

 

// Function to remove the Loader
function removeImage(evt:MouseEvent):void {

if (imageLoaded == true) {

myLoader.unload();
removeChild(logo);
// Set imageLoaded to false to prevent error when no Image loaded
imageLoaded = false;
}

}

 

remove_btn.addEventListener(MouseEvent.CLICK, removeImage);

Download Flash Source File:

Flash Source File load-image-2.fla

Remarks:

After the Sprite has been displayed on the MainTimeline of Flash Movie, you can do some control on it, for example, move it, rotate it, etc. The next Flash ActionScript tutorial will show how to rotate the Sprite.