Pregunta

I'm trying to make my first preloader come to fruition on my game. I've done quite a bit of experimenting and here's what I've found.

First off, when I simulate my file, I am simply getting a blank white screen until the file...or actually just a large image I'm using att the moment...is fully loaded. I used the Bandwidth Profiler to check what was going on. It is loading at a proper rate and I can see the loading percentage increasing. Unfortunately my loader just doesn't appear, and the weird thing is it says it's in frame 0. I have no idea why it is telling me this.

What I've done to try and fix it. I've made sure that EVERY movieclip in my game has the box for exporting on first frame is unchecked, EXCEPT my preloader, so that shouldn't be the problem. I also created a blank .fla file and simply imported my preloader into it and ran it. Surprisingly, it worked! (once again, just loaded a large image). I didn't change anything....all the same names and stuff. I have no idea why that one would work and this one wouldn't.

My timeline basically is the following: Two layers: First layer first frame actions says stop();. I would assume my preloader should load on the first frame like I've told it too. Once finished loading, it runs through the timeline to frame 3, where my main game should start running since I also put in the actions of frame 3. Second layer second frame I have a MovieClip that contains the image that I want it to load.

Here is my document class

package com.classes 
{
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.events.Event;

    public class DocumentClass extends MovieClip
    {

        private var preloader:ThePreloader;
        public static var enemyList1:Array = new Array();
        // moved stickobject1 to a class variable.
        private var stickobject1:Stickman2;

        private var scoreHud:ScoreHud;

        public function DocumentClass() : void
        {
            preloader = new ThePreloader(390, this.loaderInfo);
            stage.addChild(preloader);
            preloader.addEventListener("loadComplete", loadAssets);
            preloader.addEventListener("preloaderFinished", showSponsors);

            var bg1:background1 = new background1();
            stage.addChild(bg1);

            stickobject1 = new Stickman2(stage);
            stage.addChild(stickobject1);

            stickobject1.x=50;
            stickobject1.y=300;

            //running a loop now.... so we can keep creating enemies randomly.
            addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
            stickobject1.addEventListener("hit", stickobject1Hit, false, 0, true);
            scoreHud = new ScoreHud(stage); //create our HUD
            stage.addChild(scoreHud); //and display it.

        }

        private function loadAssets(e:Event) : void
        {
            this.play();
        }

        private function showSponsors(e:Event) : void
        {
            stage.removeChild(preloader);
            trace("show sponsors");
        }

        //our loop function
        private function loop(e:Event) : void
        {
            //run if condition is met.
            if (Math.floor(Math.random() * 40) == 5)
            {
                //create our enemyObj1 
                var enemyObj1:Enemy1 = new Enemy1(stage, stickobject1);

                //listen for enemyObj1 being removed from stage
                enemyObj1.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemyObj1, false, 0, true);
                enemyObj1.addEventListener("killed", enemy1Killed, false, 0, true);
                enemyList1.push(enemyObj1);

                stage.addChild(enemyObj1);
            }   
        }

        private function enemy1Killed(e:Event) : void
        {
            scoreHud.updateKills(1); //add 1 to enemy kills
            scoreHud.updateScore(e.currentTarget.points); //add that points variable we created earlier
        }

        private function stickobject1Hit(e:Event) : void
        {
            scoreHud.updateHits(1); //add 1 to number of hits
        }

        private function removeEnemyObj1(e:Event)
        {
            enemyList1.splice(enemyList1.indexOf(e.currentTarget), 1);
        }


    }

}

and here is my "ThePreloader" class

package com.classes
{
    import flash.display.LoaderInfo;
    import flash.display.MovieClip;
    import flash.events.*;

    public class ThePreloader extends MovieClip
    {

        private var fullWidth:Number; //the width of our mcPreloaderBar at 100%
        public var ldrInfo:LoaderInfo;

        public function ThePreloader(fullWidth:Number = 0, ldrInfo:LoaderInfo = null) 
        {
            this.fullWidth = fullWidth;
            this.ldrInfo = ldrInfo;

            addEventListener(Event.ENTER_FRAME, checkLoad);
        }

        private function checkLoad (e:Event) : void
        {
            if (ldrInfo.bytesLoaded == ldrInfo.bytesTotal && ldrInfo.bytesTotal != 0)
            {
                //loading complete
                dispatchEvent(new Event("loadComplete"));
                phaseOut();
            }

            updateLoader(ldrInfo.bytesLoaded / ldrInfo.bytesTotal);
        }

        private function updateLoader(num:Number) : void
        {
            //num is a number between 0 and 1
            mcPreloaderBar.width = num * fullWidth;
        }

        private function phaseOut() : void
        {   
            removeEventListener(Event.ENTER_FRAME, checkLoad);
            phaseComplete();
        }

        private function phaseComplete() : void
        {   
            //go on to the next phase
            dispatchEvent(new Event("preloaderFinished"));  
        }


    }

}

My main confusion is why the Bandwidth Profiler is telling me I'm stuck in frame 0 and until it's pretty much 100% loaded. Thus, not ever displaying the preloader....thank you!

¿Fue útil?

Solución

I think your preloader should be listening to LoaderInfo's progress event. Since, you have stopped execution, I don't think ENTER_FRAME would get called for you. Also, AFAIR, single frame movie are treated specially by the player and player sends ENTER_FRAME periodically for them. Probably, that is the reason you test application worked out.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top