A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Loading bar problems

  1. #1
    Junior Member
    Join Date
    Jan 2009
    Posts
    15

    Loading bar problems

    I can't get this loading bar to work with my movie. It worked pretty well when I had a music file actually on the timeline of the movie, starting on the second frame, but I have that set up through a custom class now. Currently, when I test the movie and simulate the download, the screen is blank until the loading is complete, rather than showing the progress of the load.

    Here's the code I have on frame one of my movie. Pretty much everything else is in .as files.

    Code:
    stop();
    
    addEventListener(Event.ENTER_FRAME, loaderF);
    
    function loaderF(e:Event):void {
    	var toLoad:Number = loaderInfo.bytesTotal;
    	var loaded:Number = loaderInfo.bytesLoaded;
    	var total:Number = loaded/toLoad;
    	
    	if(loaded == toLoad) {
    		removeEventListener(Event.ENTER_FRAME, loaderF);
    		gotoAndStop(2);
    	} else {
    		preloader_mc.preloaderFill_mc.scaleX = total;
    		preloader_mc.percent_txt.text = Math.floor(total*100) + "%";
    		preloader_mc.ofBytes_txt.text = loaded + "bytes";
    		preloader_mc.totalBytes_txt.text = toLoad + "bytes";
    	}
    }
    I also tried setting up a second swf that would load the first swf into it, thinking that might solve the problem, but that's a no go. Here's the code I used for that:

    Code:
    stop();
    
    var ldr:Loader = new Loader();
    
    addEventListener(Event.ENTER_FRAME, startLoading);
    
    function startLoading(e:Event):void {
    	ldr.load(new URLRequest("MainGame.swf"));
    	var toLoad:Number = ldr.contentLoaderInfo.bytesTotal;
    	var loaded:Number = ldr.contentLoaderInfo.bytesLoaded;
    	var total:Number = loaded/toLoad;
    	
    	if(loaded == toLoad) {
    		removeEventListener(Event.ENTER_FRAME, startLoading);
    		removeChild(preloader_mc);
    		loader_mc.addChild(ldr);
    	} else {
    		preloader_mc.preloaderFill_mc.scaleX = total;
    		preloader_mc.percent_txt.text = Math.floor(total*100) + "%";
    		preloader_mc.ofBytes_txt.text = loaded + "bytes";
    		preloader_mc.totalBytes_txt.text = toLoad + "bytes";
    	}
    }
    Any ideas/suggestions? Maybe I'm going about this all wrong.

  2. #2
    Junior Member
    Join Date
    Jan 2009
    Posts
    15
    This code works correctly with the loading bar, however, my game isn't working correctly once loaded. The standalone swf for the game itself plays fine, just not when it is played through the loader's swf. I get this:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at MainGame/startGameLevel()
    at MainGame/frame2()

    The game screen loads, objects animate, but character controls are unresponsive. Anyone know how to fix this?

    Code:
    var myLoader:Loader = new Loader();
    var myRequest:URLRequest = new URLRequest("MainGame.swf");
    myLoader.load(myRequest);
    
    myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult);
    
    function showProgress(evt:ProgressEvent):void {
    	var toLoad:Number = evt.bytesTotal;
    	var loaded:Number = evt.bytesLoaded;
    	var total:Number = loaded/toLoad;
    	
    	preloader_mc.preloaderFill_mc.scaleX = total;
    	preloader_mc.percent_txt.text = Math.floor(total*100) + "%";
    	preloader_mc.ofBytes_txt.text = loaded + "bytes";
    	preloader_mc.totalBytes_txt.text = toLoad + "bytes";
    }
    
    function showLoadResult(evt:Event):void {
    	removeChild(preloader_mc);
    	addChild(myLoader);
    }

  3. #3
    Junior Member
    Join Date
    Jan 2009
    Posts
    15
    After a bit of research and testing, I think I've isolated the problem to the stage returning null when the main swf is played through the loader swf. When the main swf is played alone, tracing stage returns object Stage.

    I still have no idea what to do about it.

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    The "stage" property isn't set until after the constructor finishes running for your game - so if you have anything in the constructor that points to stage (like stage.stageWidth) you need to move that outside to a function that's called after the constructor finishes. Here's the setup I use to get around the problem. Also for a really good rundown on preloading, you should check out Lee's tutorial over at gotoAndLearn.com.

    PHP Code:
    public function Constructor(){
        
    super();
        
    //  stage doesn't exist here yet
        
    addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void{
            
    e.target.removeEventListener(e.typearguments.callee);                    
            
    init();
        });        
            }

    private function 
    init():void{
        
    //  stage exists here


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center