`
sakakokiya
  • 浏览: 490884 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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]);
				
		}
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics