A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Variables on stage null at run-time

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    Variables on stage null at run-time

    Hello, I am writing a game in AS3.

    I have run into a problem. All the instances on the stage become null at runtime. As soon as it gets to the first function, they become null.

    This is the basics of the code: (MainTextDisplay is a textfield on the MainTimeline of the stage)

    Code:
    stage.addEventListener(Event.ENTER_FRAME, Loading);
    
    // this will not be null, it will be the MainTextDisplay variable
    var test:TextField = MainTextDisplay;
    
    function Loading(e:Event)
    {
    
    if(stage.loaderInfo.bytesLoaded == stage.loaderInfo.bytesTotal) {
    stage.removeEventListener(Event.ENTER_FRAME, Loading); stage.addEventListener(Event.ENTER_FRAME, init);
    }
    } function init(e:Event) {
    // this will be null var anotherTest:TextField = MainTextDisplay;
    }
    This just happened out of nowhere, and it is consistent with all variable instances on the MainTimeline (and only those).

    I was wondering what might be causing this.

    Thanks in advance.

  2. #2
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    Do you have this code in a class or on the timeline? From you post, it's not clear what part of the code is coming up null, I'm assuming it's the references to the stage in the if statement within Loading.

    Typically this problem will happen in a class where you make a reference to the stage before the class is actually added to the stage. You would get around that by waiting until receiving an added to stage event to make the stage reference.

    Actionscript Code:
    function MyClass() : void {
        addEventListener(Event.ADDED_TO_STAGE, onAdded);
    }

    function onAdded(e:Event) : void {
        trace("stage.loaderInfo.bytesLoaded: " + stage.loaderInfo.bytesLoaded );
    }

    From the timeline though, you should usually be able to access the stage. But when you're instantiating an MC and using addChild to add it to the stage, the same problem may occur. You can try the same kind of waiting for the added to stage event, or you can do a cheap hack by executing your code like on frame 2 instead of frame 1, to give it a bit of a delay to make sure it's added to the stage.

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