Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Reference stage OOP

Guest
Jul 04, 2009 Jul 04, 2009

from my documentclass  i got a isntance of the my view class in there i got a instance of navigation class extends Sprite

I got a utilz.as file with all static public functions like this one

alignToCenter(obj:DisplayObject):vid

{

     obj.x = obj.stage.stageWidth/2;

}

Utilz.alignToCenter(instance of navigation class)

I get a error stage is null reference..

What is a easy way to fix that?

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 04, 2009 Jul 04, 2009

Main is your document class?

(and don't nest named functions.)

Translate
Guest
Jul 04, 2009 Jul 04, 2009

If i want to create a ViewModel class

and it should have a stage reference

like

package mvc.view
{
    import flash.display.Stage;

    public class ViewModel extends Sprite
    {
        private var s:Stage = new Stage() <--- btu this is really the current stage
       
        public function ViewModel()
        {
           
        }
       
        public function update():void
        {
           
        }       
    }
}

This would be one idea right?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 04, 2009 Jul 04, 2009

A DisplayObject's stage property becomes available when the instance is added to the display list.

When trying to access the stage property too soon, it'll throw an error.

Try listening for the ADDED_TO_STAGE event in you custom class, at which time the stage property will be available for use.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 04, 2009 Jul 04, 2009

Yes, null reference error almost always means the thing's not loaded yet. Adding an event listener is the most direct solution. I usually find it easiest just to eliminate any stage-positioning reference that isn't in the main class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 04, 2009 Jul 04, 2009

in my main class before i do anything i do :

  public function Main()
        {

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
           
            function onAddedToStage(e:Event):void
            {
                removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.align = "TL";
                trace(stage.stageWidth);
                init();    

            }

}

why i get 0 on my trace? stage is there

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 04, 2009 Jul 04, 2009

Main is your document class?

(and don't nest named functions.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 04, 2009 Jul 04, 2009

Yeah!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 04, 2009 Jul 04, 2009
LATEST

I am pretty sure this is what is going on:

1) Your main/document class brings in another class--called either Navigation.as or Utilz.as (I can't tell), and inside this other class you are centering an object within that class's own stage.

2) But your Utilz.as (or Navigation) class, including all the subclasses and objects inside it, is not fully loaded yet when the Flash engine tries to center an object...

3) So Flash gives a null reference error!

4) Your event listener appears to be in your main class, but your problem is in your utilz or navigation class, and that's where you have to put your listener. If you are going to center an object within a subclass, you have to make sure it is loaded first.

Finally: Try commenting out // that stage-centering property and see if you get a different result.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines