I'm making a simple game in Adobe Flash CS4 using ActionScript 3. I recently implemented a new, very small preloader to replace the placeholder that I was using. However, once I did this, I seemed to break everything.

The first frame has only a stop(); in "Actions" and then a DocumentClass external class file (all of my classes are external) that sets up the preloader, which is in its own class file. Once the preloader is done, it fires off an Event to the DocumentClass, which runs a gotoAndStop(3);. On slide three is a movie clip titled Main, with its own class file.

Main imports a bunch of stuff, then loads the opening screen of the game. At this point everything basically breaks down. Any attempts to modify things that are on this opening screen (which has both a class file and MovieClip object) results in "TypeError: Error #1009: Cannot access a property or method of a null object reference." I thought this code might be running before the MovieClip is on the stage, so I added addEventListener( Event.ADDED_TO_STAGE, onAddtoStage ); and moved the code to an onAddtoStage function; while doing trace("this is the stage: "+stage); and trace("this is my parent: "+this.parent); will return the proper, non-null values, I still can't modify anything (for example, making a MovieClip object I'd like to hide invisible by setting invisibleBG.alpha = 0; still returns Error #1009).

Furthermore, while the Main MovieClip is visible on the stage, the opening screen MovieClip doesn't show up. I'm not sure if it's behind the Main object somehow, but it seems like it's simply not on the stage at all.

There's some of my code below. I can add more if this doesn't show what the problem is. I've been really scratching my head over this; of course, it took me two days to figure out I couldn't modify a TextField because it was stretched out over two frames, so there might be a very simple solution. Thanks in advance for your help!

Here's most of the class that handles the initial loading:

public class DocumentClass extends MovieClip  
{
    public var loadingScreen:LoadingScreen;

    public function DocumentClass()
    {
        loadingScreen = new LoadingScreen();
        addChild( loadingScreen );

        stage.stageFocusRect = false;

        loadingScreen.addEventListener( Event.COMPLETE, onPreloaderComplete );
        loadingScreen.setLoaderInfo( loaderInfo );
    }

    public function onPreloaderComplete( e:Event ):void
    {
        loadingScreen.removeEventListener( Event.COMPLETE, onPreloaderComplete );
        removeChild( loadingScreen );
        loadingScreen = null;

        gotoAndStop( 3 );
    }
}

Here's the relevant portion of the Main class:

public class Main extends MovieClip
{
    public function Main()
    {
        showDesktop();
    }
    public function showDesktop():void
    {
        desktopScreen = new DesktopScreen();
        addChild( desktopScreen );

        stage.focus = desktopScreen;
    }
}

And the relevant portion of DesktopScreen, the game's opening screen:

public class DesktopScreen extends MovieClip
{
    public function DesktopScreen()
    {
        addEventListener( Event.ADDED_TO_STAGE, onAddtoStage );
    }

    public function onAddtoStage( e:Event ):void
    {
        Mouse.show();

        invisibleBG.alpha = 0;
        fakeButton.stop();

        time = new Time;
        time.x = 504;
        time.y = 3;
        addChild( time );
    }
}

TL;DR I'm adding a MovieClip to the stage, and even though I can tell via trace functions that it thinks it's on the stage, I can't see it or modify any of it's contents in a way that would require it to actually be on the stage.

有帮助吗?

解决方案

Okay, I managed to fix this today. Here's what happened.

When I implemented my new preloader, I attempted to use the three-frame method detailed here. By the way, that's a great general tutorial on preloaders, if you're looking for one.

However, I tried to use the four-frame method later on in the tutorial, but I missed a crucial fact: Flash can't access assets until it reaches the frame that they're on. So, frame 1 had only my preloader (and the very small DocumentClass), frame 2 was my class frame, frame 3 had the Main object and its associated code, and frame 4 had my AssetHolder object with all of my library objects.

The result was that when I ran gotoAndStop(3);, the Main class and object were available, which called the DesktopScreen class and object. But only the DesktopScreen class was available (as it was in frame 2) and the object wasn't (it wouldn't be available until frame 4). The result was that the code was run, but since nothing was actually on the stage (even though Flash was telling me it was) it threw a bunch of errors.

I finally figured this out when I reviewed the extremely informative tutorial by Michael James Williams here. That's another good tutorial to review if you're looking to get started with Flash!

So, this was easy enough to fix; stop using (the unnecessary) frame 4 for my assets, and move them instead to frame 2. Frame 1 still has stop(); in the actions panel, so the game stops as soon as it starts to load, looks to DocumentClass, starts preloading, then goes to frame 3 (after all of the assets and classes are available) once loaded.

I hope this helps anyone who has a similar, incredibly obscure, problem. If you have any questions, feel free to ask!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top