Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411517 Posts in 69377 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 28, 2024, 12:25:52 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[SOLVED]Using the "stage" in a Nested Swf? null object reference :[
Pages: [1]
Print
Author Topic: [SOLVED]Using the "stage" in a Nested Swf? null object reference :[  (Read 3251 times)
YagerX
Level 2
**



View Profile WWW
« on: April 18, 2009, 03:03:22 AM »

Uh bit fustrated here, I'm trying to make a basic preloader for my main swf.
All it does is loads the swf whilst tracking its progress, then adding childen and what not.
The code for the preloader is:
Code:
var Request:URLRequest = new URLRequest("Space Defender.swf");
var L:Loader = new Loader();
L.load(Request);
 
L.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
L.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
L.contentLoaderInfo.addEventListener(Event.COMPLETE,showContent);

var bar:loadingBar = new loadingBar();
 
function showPreloader(event:Event):void {
        addChild(bar);
        bar.x = stage.stageWidth/2;
        bar.y = stage.stageHeight/2;
}
 
function showProgress(event:ProgressEvent):void {
        var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
      bar.width = 200 * percentLoaded;
}
 
function showContent(event:Event):void {
        removeChild(bar);
L.x = 0;
L.y = 0;
        addChild(L);
}

When I compile however I get a runtimer error after loading:
Code:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main()[C:\Flash Project\Space Defender\Main.as:32]

Line 31-33:
Main.stage=root.stage;
stage.addEventListener(KeyboardEvent.KEY_DOWN, menu.keyIn);
stage.addChild(menu);

I commented lines 32-33 out and it worked fine. Unfortunately with out the menu I can't actually physically start the game...
The stage is referenced at line 31 in main so that I can use it in my other classes, but I believe that's where the problem lies.
Does anyone know how to solve this?
« Last Edit: April 20, 2009, 05:05:09 PM by Derek » Logged

Drew
Level 0
***


Hooray


View Profile
« Reply #1 on: April 18, 2009, 09:23:30 AM »

I'm new at this, but I might as well try to help.  I just tried to make a loader with that code, and it loaded my current project fine, so it might be something fancy you're doing in your space defender file.

Interestingly, the stage background color doesn't come in from the loaded swf, so I'm assuming that it essentially throws out the stage of the loaded movie. Maybe line 31 is causing the issue?

I guess it might be useful to know whether stage is null or menu is null.  Try putting in a trace(stage); and trace(menu); after line 31 and see what it prints out.

Edit: I need to read more carefully.  I don't think I told you anything you didn't already know Smiley

Edit2: I think I got it.  Are lines 31-33 in the Constructor?  That's too early.  They need to happen after addChild is called, otherwise the stage is set to null.
« Last Edit: April 18, 2009, 09:52:40 AM by drew » Logged
Sam
Level 3
***



View Profile WWW
« Reply #2 on: April 18, 2009, 10:00:52 AM »

I'm not 100% on this, but I've encountered an issue that I think is the same as yours.  Assuming your Main.as is something of the form:
Code:
package
{
   import flash.display.Bitmap;
   import flash.display.BitmapData;

   public class Main extends Movieclip
   {
      public function Main()
      {
         var myPrettyBitmap:Bitmap = new Bitmap(new BitmapData(20,20,true,0xdd5522));
         stage.addChild(myPrettyBitmap);
      }
   }
}

Compile that into a .SWF and it works fine.  But try loading it through a preloader, and it gives a null object reference error.  This is something to do with not being able to access the stage at the time that the constructor of Main is called.  Main will be able to access the stage once it is actually added to the stage, which happens just after it's constructed.  You can detect when it is added by using an event listener:
Code:
package
{
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.events.Event;

   public class Main extends Movieclip
   {
      public function Main()
      {
         this.addEventListener(Event.ADDED_TO_STAGE, init);
      }
      private function init(e:event):void
      {
         var myPrettyBitmap:Bitmap = new Bitmap(new BitmapData(20,20,true,0xdd5522));
         stage.addChild(myPrettyBitmap);
      }
   }
}

I hope that helps, and that it actually works.

edit:  Drew got there before me with his second edit!
Logged
Business Bear
Level 0
***



View Profile
« Reply #3 on: April 18, 2009, 12:25:20 PM »

Salt is right on with his solution.  I had this same issue the other day!
Logged
YagerX
Level 2
**



View Profile WWW
« Reply #4 on: April 18, 2009, 02:55:53 PM »

Hokey doke, Will fix tonight, thanks guys!
Logged

YagerX
Level 2
**



View Profile WWW
« Reply #5 on: April 18, 2009, 04:08:03 PM »

Thanks, works perfectly now
Hm...Anyone know what url kongregate holds your swfs?
Nevermind, the swfs are held "next to each other".
Consider this solved!
« Last Edit: April 18, 2009, 04:16:27 PM by YagerX » Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic