
Код AS3:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;
import flash.display.Loader;
import fl.text.TLFTextField;
import flash.text.TextFormat;
import flash.text.Font;
import flash.text.TextFieldAutoSize;
import flash.geom.Rectangle;
import com.greensock.*;
import com.greensock.plugins.*;
import com.greensock.easing.*;
import com.greensock.plugins.TintPlugin;
import com.greensock.plugins.TweenPlugin;
public class Prj_Button extends Sprite {
public var prjNum :int;//Номер
public var folder :String;//Папка
private var prjImg :String;
private var loader :Loader;//Изображение
private var loaderError :Boolean;//Ошибка лоадера
private var noPicRus :String;
private var noPicEng :String;
private var filePath :String;//Путь к файлу
public function Prj_Button(lang:Boolean, fldr:String, prjN:int, img:String) {//
// constructor code
currentLang=lang;
folder=fldr;
prjNum=prjN;
prjImg=img;
loaderError=false;
noPicRus="Изображение отсутствует";
noPicEng="No picture";
if (stage)
{
initButton();
}else{
this.addEventListener(Event.ADDED_TO_STAGE, onStageOk);
}
}
private function onStageOk(e:Event):void{
this.removeEventListener(Event.ADDED_TO_STAGE, onStageOk);
initButton();
}
private function initButton():void
{
//Наполнить кнопку
loadImg();
this.mouseEnabled=true;
this.mouseChildren=false;
}
private function loadImg():void
{
//Загрузить картинку
loader=new Loader();
var file:String="";
if (prjNum<10) file=file+"0";
if (prjNum<100) file=file+"0";
file=file+prjNum+".jpg";
filePath="data/"+folder+"/"+file;
var Request:URLRequest = new URLRequest(filePath);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImg);
loader.contentLoaderInfo.addEventListener (IOErrorEvent.IO_ERROR, errorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader.load(Request);
}
private function errorHandler(e:IOErrorEvent):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImg);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
loader=null;
loaderError=true;
if (currentLang)
{
this.errorText.text=noPicRus;
}else{
this.errorText.text=noPicEng;
}
}
private function securityErrorHandler(e:SecurityErrorEvent):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImg);
loader.contentLoaderInfo.removeEventListener (IOErrorEvent.IO_ERROR, errorHandler);
loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader=null;
loaderError=true;
if (currentLang)
{
this.errorText.text=noPicRus;
}else{
this.errorText.text=noPicEng;
}
}
private function onLoadImg(e:Event):void
{
//Подогнать изображение под размер и выровнять
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImg);
loader.contentLoaderInfo.removeEventListener (IOErrorEvent.IO_ERROR, errorHandler);
loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
this.img.addChild(loader);
var maxWidth=276;
var maxHeight=216;
var mc:MovieClip=this.img;
var ratio:Number;
if (mc.height > mc.width)
{
ratio=maxWidth/mc.width;
}else{
ratio=maxHeight/mc.height;
}
mc.width*=ratio;
mc.height*=ratio;
mc.x=-(mc.width-maxWidth)/2;
}
}//
}//