A class for adding images to the stage, resizing them and turning smoothing on.
// load the Image class import com.drpunchlogic.IMGload; // -- Image // set up the var for the image // - ( the container for the image , the location of the xml ) var I:IMGload = new IMGload( holder_mc, "sample.jpg" );
package com.drpunchlogic
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.Event;
// tween class
import gs.TweenMax;
import gs.easing.*;
public class IMGload
{
private var _Width:Number;
private var _Height:Number;
//
private var _container:Object;
private var _img:String;
//
private var _percent:Number;
private var _loader:Loader;
// grab preload from library
public var preloader:PreLoader;
// set center of container
private var _conXcent:Number;
private var _conYcent:Number;
public function IMGload( container:Object, img:String ) : void
{
// set the container for the object
_container = container;
// set image path
_img = img;
// set center
_conXcent = Math.floor( _container.width / 2 );
_conYcent = Math.floor( _container.height / 2 );
// startloading Image
LoadStart();
}
private function LoadStart() : void
{
// set a new var each time
preloader = new PreLoader();
LoadAttach( preloader );
_loader = new Loader();
_loader.load( new URLRequest( _img ) );
_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, LoadProgress );
_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, LoadFin );
}
// generic attach to center of the container
private function LoadAttach( obj:Object ) : void
{
// place preloader in the clip
obj.name = "imgPreload_mc";
_container.addChild( obj );
// position the preloader within the clip
obj.x = _conXcent;
obj.y = _conYcent;
trace( _container.width + ":" + _conXcent + "\t\t" + _container.height + ":" + _conYcent);
// prep the preloader to ani in
obj.alpha = 0;
obj.scaleX = obj.scaleY = 0.3;
// object animate in
LoadAniIn( obj );
trace( "\t{ " + _img + ": ADDED:\t" + obj.name + " }" );
}
private function LoadRemove( obj:Object ) : void
{
_container.removeChild( obj );
trace( "\t{ " + _img + ": KILLED:\t" + obj.name + " }" );
}
private function LoadAniIn( obj:Object ) : void
{
trace( "\t{ " + _img + ": ANI IN:\t" + obj.name + " }" );
// animate
TweenMax.to(obj, 0.5, {
scaleX:1,
scaleY:1,
alpha:1,
ease:Circ.easeOut
});
}
private function LoadAniOut( obj:Object ) : void
{
trace( "\t{ " + _img + ": ANI OUT:\t" + obj.name + " }" );
// animate
TweenMax.to(obj, 0.5, {
scaleX:0.3,
scaleY:0.3,
alpha:0,
ease:Circ.easeOut,
onComplete:LoadRemove( obj )
});
}
// tracking the porgess of the load
private function LoadProgress( e:ProgressEvent ) : void
{
_percent = Math.floor( e.bytesLoaded / e.bytesTotal * 100 );
var obj = _container.getChildByName( "imgPreload_mc" );
obj.txt.htmlText = String( "LOADING:\t" + _percent + "%" );
trace( "\t{ " + _img + ": LOADING:\t" + _percent + "% } " );
}
// now that the loading is over
private function LoadFin(e:Event) : void
{
// turn on smoothing
e.target.content.smoothing = true;
// get width and height
_Width = e.target.width;
_Height = e.target.height;
trace( "\t{ W:\t" + _Width + " H:\t" + _Height + " }" );
// define the vars
var desiredSize:Number ;
var currentSize:Number ;
// get the image object
//var _imgObj = e.target.content;
var _imgObj = _loader;
_container.addChild( _imgObj );
// check to see which side is longer
if( _Width > _Height )
{
desiredSize = _conXcent * 2;
currentSize = _Width;
} else {
desiredSize = _conYcent * 2;
currentSize = _Height;
}
//Get the %
var scale:Number = (desiredSize / currentSize);
//Set the _xscale and _yscale of the loaded image
_imgObj.scaleX = _imgObj.scaleY = scale;
// Set the placement of the image object
_imgObj.x = -( _conXcent );
_imgObj.y = -( _conYcent );
_imgObj.alpha = 0;
TweenMax.to(_imgObj, 0.5, {
alpha:1,
ease:Circ.easeIn
});
trace( "\t{ X:\t" + _imgObj.x + " Y:\t" + _imgObj.y + " S:\t" + _imgObj.scaleX +" }" );
// set the loader text to 100%
var obj = _container.getChildByName( "imgPreload_mc" );
obj.txt.htmlText = String( "LOADING: 100%" );
trace( "\t{ " + _img + ": LOADING: COMPLETE } " );
// animate out
LoadAniOut( obj );
}
}
}
