Thursday, 1 October 2009

create a apple preloader



package classes
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.events.ProgressEvent;

package classes
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.events.ProgressEvent;
Step 10 - Extending the Class
view plaincopy to clipboardprint?
public class Preloader extends MovieClip
{

public class Preloader extends MovieClip
{
Since our preloader is a MovieClip and it's using a timeline, we're going to extend this class using the MovieClip class.

Step 11 - Variables
We only need to use one variable in this class. This variable will store the instance name of the textfield we're using to show the loading information.

view plaincopy to clipboardprint?
private var dataTextField:TextField;

private var dataTextField:TextField;Step 12 - Start Function
view plaincopy to clipboardprint?
public function start(dataTextField:TextField):void
{
this.dataTextField = dataTextField; //Sets the dataTextField var to the parameter value

/* The loaderInfo Object is in charge of the loading process, in this code we add
listeners to check the progress and when the movie is fully loaded */

this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);

}

public function start(dataTextField:TextField):void
{
this.dataTextField = dataTextField; //Sets the dataTextField var to the parameter value

/* The loaderInfo Object is in charge of the loading process, in this code we add
listeners to check the progress and when the movie is fully loaded */

this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);

}Step 13 - The Progress Function
view plaincopy to clipboardprint?
private function onProgress(e:ProgressEvent):void
{
/* Here we use some local variables to make better-reading code */

var loadedBytes:int = Math.round(e.target.bytesLoaded / 1024);
var totalBytes:int = Math.round(e.target.bytesTotal / 1024);
var percent:int = (e.target.bytesTotal / e.target.bytesLoaded) * 100;

/* Sets the loading data to the textfield */

dataTextField.text = String(loadedBytes + " of " + totalBytes + "KB Loaded\n" + percent + "% Complete");
}


private function onProgress(e:ProgressEvent):void
{
/* Here we use some local variables to make better-reading code */

var loadedBytes:int = Math.round(e.target.bytesLoaded / 1024);
var totalBytes:int = Math.round(e.target.bytesTotal / 1024);
var percent:int = (e.target.bytesTotal / e.target.bytesLoaded) * 100;

/* Sets the loading data to the textfield */

dataTextField.text = String(loadedBytes + " of " + totalBytes + "KB Loaded\n" + percent + "% Complete");
}
Step 14 - The Complete Function
view plaincopy to clipboardprint?
private function onComplete(e:Event):void
{
/* Remove listeners */

this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);

//Here you can add a function to do something specific, I just used a trace */

trace("Loaded!");
}

private function onComplete(e:Event):void
{
/* Remove listeners */

this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);

//Here you can add a function to do something specific, I just used a trace */

trace("Loaded!");
}
Step 15 - Document Class
Create a new ActionScript file and start writing:

view plaincopy to clipboardprint?
package classes
{
import flash.display.MovieClip;

public class Main extends MovieClip
{
public function Main():void
{
/* Starts the preloader, "preloader" is the instance name of the clip */

preloader.start(info);
}
}
}

No comments: