AS3 PV3D Preloader
Preloading is crucial when you have to load anything that takes more than a couple seconds. I’ve never liked the popular export to frame two, movie clip on the stage containing every single asset technique either. In my opinion it always felt like a hack, and somewhat of a hassle when you have a lot to preload. I also prefer to have a single empty key frame on the main time line, so I created this preloader. It’s essentially a wrapper swf that loads your main swf which contain all the assets. I also thought this would be the perfect opportunity to represent Papervision3D by rocking the logo. The preloader is only 10k total, and uses TweenLite. You’ll also need the latest PV3D 2.0 classes which can be found HERE.
Preloader.as
/** * AS3 PV3D Preloader v1.5 * Copyright (c) 2008 Marco Di Giuseppe - http://designmarco.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package { import flash.display.Sprite; import flash.display.Loader; import flash.display.Stage; import flash.display.StageAlign; import flash.display.StageQuality; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.IEventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.URLRequest; import fl.motion.easing.Exponential; import gs.TweenLite; public class Preloader extends Sprite { private static const MAIN_SWF:String = "main.swf"; //name of swf we are preloading private static const POST_DELAY:Number = 2; //time in seconds before loading begins private static const BAR_INERTIA:Number = 5; //amount of easing applied to progress bar private var preloader:PreloadClip; // library movieclip of logo private var widthRatio:Number; //total length of progress bar divided by 100 public function Preloader() { init(); } private function init():void { stage.quality = StageQuality.HIGH; stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; preloader = new PreloadClip(); widthRatio = preloader.progress_mc.bar.width / 100; preloader.x = stage.stageWidth * 0.5 - preloader.width * 0.5; preloader.y = stage.stageHeight * 0.3; preloader.progress_mc.bar.width = 0; preloader.progress_mc.visible = false; addChild(preloader); initSequence(); } // logo animation sequence private function initSequence():void { var i:int = preloader.numChildren; var logo:Sprite; while (i--) { logo = preloader.getChildByName("m" + i) as Sprite; TweenLite.from(logo, 0.4, {scaleX:10, scaleY:10, alpha:0, delay:i * 0.1, ease:Exponential.easeOut}); } // allows animation to complete smoothly TweenLite.delayedCall(POST_DELAY, loadContent); } // begin loading process private function loadContent():void { var loader:Loader = new Loader(); var request:URLRequest = new URLRequest(MAIN_SWF); initListeners(loader.contentLoaderInfo); try { loader.load(request); preloader.progress_mc.visible = true; } catch (error:Error) { trace("error loading"); } } //display loading progress private function progressHandler(event:ProgressEvent):void { var percentLoaded:Number = int(((event.bytesLoaded / 1024) / (event.bytesTotal / 1024)) * 100); var barWidth:Number = widthRatio * percentLoaded; preloader.progress_mc.loading_txt.text = String(int(percentLoaded) + "%"); preloader.progress_mc.bar.width += (barWidth - preloader.progress_mc.bar.width) / BAR_INERTIA; if (percentLoaded == 100) preloader.progress_mc.visible = false; } // handle possible error private function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); killListeners(event.target.content); destroy(); } // fade out preloader and remove listeners private function completeHandler(event:Event):void { var main:Sprite = event.target.content as Sprite; TweenLite.to(preloader, 1, {alpha:0, ease:Exponential.easeOut, onComplete:showContent, onCompleteParams:[main]}); killListeners(main); } private function showContent(swf:Sprite):void { addChild(swf); destroy(); } //clean-up private function destroy():void { if (preloader && preloader.parent) { preloader.parent.removeChild(preloader); preloader = null; } } //listen for loading events private function initListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.COMPLETE, completeHandler); dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true); dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true); } private function killListeners(dispatcher:IEventDispatcher):void { dispatcher.removeEventListener(Event.COMPLETE, completeHandler); dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler); } } }
Main.as
/** * PV3D 2.0 Basic Example * @author Marco Di Giuseppe * marco (at) designmarco (dot) com * http://designmarco.com */ package { import flash.display.Sprite; import flash.events.Event; import org.papervision3d.scenes.Scene3D; import org.papervision3d.view.Viewport3D; import org.papervision3d.cameras.Camera3D; import org.papervision3d.render.BasicRenderEngine; import org.papervision3d.objects.primitives.Plane; import org.papervision3d.materials.BitmapAssetMaterial; public class Main extends Sprite { private var scene:Scene3D; private var renderer:BasicRenderEngine; private var viewport:Viewport3D; private var camera:Camera3D; private var plane:Plane; public function Main() { addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true); } // initialize pv3d scene private function init(event:Event):void { removeEventListener(Event.ADDED_TO_STAGE, init); viewport = new Viewport3D(stage.stageWidth, stage.stageHeight); scene = new Scene3D(); renderer = new BasicRenderEngine(); camera = new Camera3D(); camera.zoom = 11; camera.focus = 100; addChild(viewport); initObjects(); addEventListener(Event.ENTER_FRAME, render, false, 0, true); } // create material and plane private function initObjects():void { var material:BitmapAssetMaterial = new BitmapAssetMaterial("image"); material.smooth = true; material.doubleSided = true; plane = new Plane(material, 500, 500, 2, 2); plane.pitch(20); scene.addChild(plane); } private function render(event:Event):void { plane.yaw(0.75); renderer.renderScene(scene, camera, viewport); } } }
comments
Leave a Reply