I've searched everywhere and can't find an answer to this question and I'll do my best to explain it.
I have 2 movieclips in my library, 'Bluknife' and 'cat' (It's an rpg game.). Both those movieclips are linked to as3 classes of the same names.
I know I can can add both of these items to the stage by having the following code in the document class:
var knife:BluKnife = new BluKnife();
stage.addChild(knife);
knife.x = stage.stageWidth / 2;
knife.y = stage.stageHeight / 2;
var ct:cat = new cat();
stage.addChild(ct);
ct.x = stage.stageWidth / 2;
ct.y = stage.stageHeight / 2;
However I don't want all this stuff to be in the document class, so I made another class that I want to use to call all of this stuff to the stage and named it callitems
:
package
{
import flash.display.MovieClip;
import flash.display.MovieClip;
import flash.display.Stage;
public class callitems extends MovieClip
{
public function callitems()
{
var knife:BluKnife = new BluKnife();
stage.addChild(knife);
knife.x = stage.stageWidth / 2;
knife.y = stage.stageHeight / 2;
var ct:cat = new cat();
stage.addChild(ct);
ct.x = stage.stageWidth / 2;
ct.y = stage.stageHeight / 2;
}
}
}
I'm just wondering how I would go about calling the callitems
class from my document class? I've tried a few things, like creating an instance of callitems
and adding it to the stage from the document class (which i wasn't surprised didn't work). Here's the function:
public function Engine()
{
var calling:callitems = new callitems();
stage.addChild(calling);
}
I got the following error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at callitems() at Engine()
The name of my document class is Engine
First, you have a misconception, that when you create a CallItems
(note in ActionScript Class names should be in Camel Case with an initial capital leter), that somehow the Classes that CallItems
uses will not be compiled into the Class that creates the CallItems
instance. Everything that every Class that is directly referenced by your Class uses gets compiled in.
To resolve that, you need to program to Interfaces, and have your Document Class only know about the Interface. There are two ways I know of to populate variables of an Interface type without having the Document Class have a reference to the implementation.
Now, to your actual problem. Your CallItems
instance is adding to the stage, rather than to itself. Since it has not yet been added to the stage (and I'd recommend that your Engine
add it to itself if you're going to go through the tiresome and tedious process of creating and adding everything through code) rather than adding to the stage.
Display Objects do not have a reference to the stage until they have been added to something that is on the display list.
You may also want to consider why you're adding calling
in Engine
, since you presumably think that calling
is already adding the things it makes to the stage (except it doesn't have a reference to the stage).