Your Ad Here

April 08, 2010

Loading Image by Movie clip

Reactions: 
Title: Loading External Image by using Movie Clip

Category : Advanced
Actionscript : 2.0

In Macromedia Flash we can load external Images (.jpeg , .gif …. files) by using Movie Clips and a Preloader can be applied with this method easily by writing few lines of Script.

1- Open up Macromedia Flash and create a new file.

2- Name the layer as “text”. Create a new text field, donot write anything in it, in its properties select the type as ‘Dynamic Text’ and select the align center option.




3- In the Properties name text field as “loadtext”





4- Create a new layer, name it as “actions”. Insert another layer and name it as “loader”, place it below the ‘actions’ layer.







5- Create a new movie clip and name it as “loader_mc” (Insert> New Symbol> Movie Clip)

6- Don’t place anything in it. Return to ‘Scene 1’. Now open the Library (Window> Library), drag and drop the ‘loader_mc’ from there to the stage. Place it on the origin of the stage, means x-axis=0 and y-axis=0.

7- Now select the actions layer. Select its first frame and open the actions panel. Write the actions as follows:
loadtext.text = "Initializing";
var my_mcl:MovieClipLoader = new MovieClipLoader();
var mclListener:Object = new Object();
mclListener.onLoadStart = function(loader:MovieClip):Void {
loadtext.text = "Loading";
};
mclListener.onLoadProgress = function(target_mc:MovieClip, numBytesLoaded:Number, numBytesTotal:Number):Void {
var pctLoaded:Number = Math.ceil(100*(numBytesLoaded/numBytesTotal));
title_pb.setProgress(numBytesLoaded, numBytesTotal);
loadtext.text = pctLoaded;
};
mclListener.onLoadComplete = function(target_mc:MovieClip, status:Number):Void {
loadtext.text = "Loaded";
};
my_mcl.addListener(mclListener);
my_mcl.loadClip("sample.jpg", loader);

8- Now test the Movieclip by pressing Ctrl+Enter. Make sure the file written is the Actionscript is in the same folder where your main file is. If you donot fully understand the script please read the description below carefully.

Description:
>> This will put the “Initializing” text on the text field:
loadtext.text = "Initializing";

>> This will create an MovieCip Loader and a Listener for the Preloader:
var my_mcl:MovieClipLoader = new MovieClipLoader();
var mclListener:Object = new Object();
mclListener.onLoadStart = function(loader:MovieClip):Void {
loadtext.text = "Loading";
//This will put the “Loading” text on the text field
};

>> This will create a variable which will calculate the Percentage of the Data Loaded:
mclListener.onLoadProgress = function(target_mc:MovieClip, numBytesLoaded:Number, numBytesTotal:Number):Void {
var pctLoaded:Number = Math.ceil(100*(numBytesLoaded/numBytesTotal));
title_pb.setProgress(numBytesLoaded, numBytesTotal);
loadtext.text = pctLoaded;
//This will show “Percentage” on the text field
};
>> When the data is loaded it will show “Loaded” text on the text field, more functions can be added in this function:
mclListener.onLoadComplete = function(target_mc:MovieClip, status:Number):Void {
loadtext.text = "Loaded";
};

>> Here you will name the file which is going to be loaded, like here “sample.jpg”is loaded:
my_mcl.addListener(mclListener);
my_mcl.loadClip("sample..jpg ", loader);

No comments:

Post a Comment

Please use sensible language.....!