hi, how are guys
first thing i wanna say iam so sorry because iam so useless in this forum , am just asking without helping
here the problem i designed some games and i always get this error 1009 error i.ve read it in this book (foundation game programming ) and i still cant understand what does he mean
here (iam asking in the blue lines and the problem red lines
help me please it is realy confusing me in game programming)
Adding and removing objects from the stage
Before you look at the code of the game, I need to explain the new way in which classes are being initialized.
Dungeon Maze Adventure has been planned as a multilevel game. If you want to add another
level, it means that you have to remove dungeon one from the stage and add dungeon tow . This gets you
into a tricky technical situation regarding how AS3.0 initializes objects in the Flash Player when the
game runs. It’s an extremely important issue to be aware of because it will plague the development of
your Flash games and give you countless sleepless nights if you don’t understand why it happens and
how to solve it.
Let’s take a look a look at this problem. When the published SWF of the game runs, it does two things
(in this order):
1. It initializes all the objects by running the constructor methods in their classes.
2. It displays the objects on the stage. (It displays them one by one in the order in which they
were dragged to the stage from the Library when you designed the game in the FLA.)what does he mean by saying display them on the stage?? because i cant see them without addchild method and he said the swf file display them??!!!!!!!!
Here’s the issue: if the class’s constructor method includes a reference to the stage or an object that
is on the stage that hasn’t yet been displayed, you’ll get an error message saying this: here i cant understand any single word!!
Error #1009: Cannot access a property or method of a null object reference.
If you’re adding objects to the game, there’s a good chance that many of those objects have to access
the stage as soon as they’re created. They might need to do this to set stage boundaries or access the
properties of other objects that are also on the stage. If they can’t find the stage object because they
haven’t been added to the stage yet, AS3.0’s compiler will “throw its hands in the air” and you’ll see
the preceding error message.
To solve this problem, you need to initialize objects only when they they’ve been added to the stage,
not before.
done
i hope you guys can help me
thank you
2. It displays the objects on the stage. (It displays them one by one in the order in which they
were dragged to the stage from the Library when you designed the game in the FLA.)what does he mean by saying display them on the stage?? because i cant see them without addchild method and he said the swf file display them??!!!!!!!!
are you using pure AS3 or are you using the design tool in CS4/5
when using the design tool in CS4/5 you can have objects in the library and drag them to the stage. this will automatically at the objects to the stage without having to call addChild()
if you are using pure AS3 you don't have access to this and you have to explicity add all you objects using addChild
Here’s the issue: if the class’s constructor method includes a reference to the stage or an object that
is on the stage that hasn’t yet been displayed, you’ll get an error message saying this: here i cant understand any single word!!
Error #1009: Cannot access a property or method of a null object reference.
you would only need to add a reference to the stage for non DisplayObjects (e.g. generally things that aren't sprites or MovieClips)
All DisplayObjects have access to the stage via the stage property. The stage property for an object is always null until you call addChild, not when they are constructed as you mention.
if you are trying to use the stageWidth and stageHeight property you shouldn't do this in the constructor of the object instead you would need to add an Event.ADDED_TO_STAGE listener and then do any configuration based ont he stage within this listener.
hope that clears it up a little
so why it initializes their objects before displaying on the stage ??!! it still confusing me
i also did this
i created small project with 2 classes
the first (class) has an instance in the stage and there is conditional
this
public function g(event:MouseEvent):void
{
var class2instance:class2 = new class2instance ;
addChild(class2instanceb);
removeChild(this);// the first class instance
}
and yeah when i did this thing its adding child but its not removing
i still cant totally get it
you can ignore this msg your answer was the correct anyway but i couldn.t get it all thank you
Be careful what you name things. Don't use a variable name that is a class name.
e.g.: rewrite that function:
public function g(event:MouseEvent):void
{
var c2instance:class2 = new class2();
MovieClip(parent).addChild(c2instance);
removeChild(this);
}
In your previous method you were adding an instance (of what I assume) is class2 but you called it class2instance (I assumed this was a typo). You then ran a simple addChild() which would add it inside the current object the function is running from. Then you removeChild(this) which removes not only the current object but what you just tried to add to the displaylist.
So I changed it up so you'd be adding class2 to the parent so it actually shows when you removeChild(this).
North America
Europe, Middle East and Africa
Asia Pacific