Preloader


package
{
import evt.GameEvent;

import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.geom.Point;
import flash.text.TextField;

import mx.effects.SoundEffect;
import mx.events.FlexEvent;
import mx.preloaders.DownloadProgressBar;

/**
* This class extends the lightweight DownloadProgressBar class. This class
* uses an embedded Flash 8 MovieClip symbol to show preloading. *
* @author jessewarden * */
public class Preloader extends DownloadProgressBar
{
/**
* The Flash 8 MovieClip embedded as a Class. */
[Embed(source="/assets/flash/lisenwell.swf", symbol="loader")]
private var FlashPreloaderSymbol:Class;
[Embed(source="/assets/flash/lisenwell.swf", symbol="startbtn")]
private var FlashBtnSymbol:Class;
[Embed(source="/assets/flash/lisenwell.swf", symbol="mouseOver.mp3")]
[Bindable] public var mouseOverSound:Class;

private var clip:MovieClip;
private var _preloader:Sprite;
public var msg:TextField;
public var btn:SimpleButton;
public var point1:Point;
private var btnSoundEffect:SoundEffect;
public function Preloader()
{
super();
btnSoundEffect=new SoundEffect();
btnSoundEffect.source=mouseOverSound;
// instantiate the Flash MovieClip, show it, and stop it.
// Remember, AS2 is removed when you embed SWF's,
// even "stop();", so you have to call it manually if you embed.
this.clip=new FlashPreloaderSymbol();
// point1=new Point();
// clip.scaleX=1.5;
// clip.scaleY=1.5;
// point1.x=this.x-300;
// point1.y=this.y-300;
// point1=this.localToGlobal(point1);
// clip.x=point1.x;
// clip.y=point1.y;
clip.x=this.x
// clip.y=point1.y;
this.addChild(clip);
this.clip.gotoAndStop("start");
// msg=new TextField();
// msg.text="完成了!!!!";
// msg.x=200;
// msg.y=200;
// addChild(msg);

}
private function centerPreloader():void
{
x = (stageWidth / 2) - (clip.width / 2);
y = (stageHeight / 2) - (clip.height / 2);
trace(x+" y"+y+"myPreloader.width"+clip.width)
}
public override function set preloader(preloader:Sprite):void
{
preloader.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress);
preloader.addEventListener(Event.COMPLETE, onSWFDownloadComplete);
preloader.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress);
preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
stage.addEventListener(Event.RESIZE, centerPreloader);
_preloader=preloader;
centerPreloader();
}

/**
* Makes sure that the preloader is centered in the center of the app.
* */
// private function centerPreloader(event:Event=null):void
// {
// var adjust_X:Number=300; // X coodinate adjustion according to Object layout.
// var adjust_Y:Number=500; // Y coodinate adjustion according to Object layout.
// x=((stage.stageWidth + adjust_X) / 2) - (clip.width / 2);
// y=((stage.stageHeight + adjust_Y) / 2) - (clip.height / 2);
// }

/**
* As the SWF (frame 2 usually) downloads, this event gets called.
* You can use the values from this event to update your preloader.
* @param event * */
private function onSWFDownloadProgress(event:ProgressEvent):void
{
var t:Number=event.bytesTotal;
var l:Number=event.bytesLoaded;
var p:Number=Math.round((l / t) * 100);
clip.loadA.gotoAndStop(p);
//clip.preloader.amount_txt.text=String(p);
//clip.preloader.amount_txt_img.text=String(p);
}

/** * When the download of frame 2
* is complete, this event is called.
* This is called before the initializing is done.
* @param event * */
private function onSWFDownloadComplete(event:Event):void
{
clip.loadA.gotoAndStop(100);
//clip.preloader.amount_txt.text="100";
//clip.preloader.amount_txt_img.text="100";
}

/**
* When Flex starts initilizating your application.
* @param event * */
private function onFlexInitProgress(event:FlexEvent):void
{
clip.loadA.gotoAndStop(100);
//clip.preloader.amount_txt.text="Init...";
}

/**
* When Flex is done initializing, and ready to run your app,
* this function is called. *
* You're supposed to dispatch a complete event when you are done.
* I chose not to do this immediately, and instead fade out the
* preloader in the MovieClip. As soon as that is done,
* I then dispatch the event. This gives time for the preloader
* to finish it's animation. * @param event *
*/
private function onFlexInitComplete(event:FlexEvent):void
{
_preloader.removeEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress);
_preloader.removeEventListener(Event.COMPLETE, onSWFDownloadComplete);
_preloader.removeEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress);
_preloader.removeEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
stage.removeEventListener(Event.RESIZE, centerPreloader);
// clip.addFrameScript(121, onDoneAnimating);
clip.gotoAndStop("fade out");
btn=new FlashBtnSymbol();
btn.x=280; btn.y=180;
clip.addChild(btn);
btn.addEventListener(MouseEvent.CLICK,onDoneAnimating);
btn.addEventListener(MouseEvent.MOUSE_OVER,onOver);
//clip.loadA.stop();
trace("fade out ");
clip.stop();

}

/**
* If the Flash MovieClip is done playing it's animation,
* I stop it and dispatch my event letting Flex know I'm done.
* @param event * */
private function onDoneAnimating(evt:MouseEvent):void
{
this.removeChild(clip);
dispatchEvent(new Event(Event.COMPLETE));
}
private function onOver(evt:MouseEvent):void{

btnSoundEffect.play([btnSoundEffect]);

}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值