0

I have a game engine class that creates my main character instance and game border instance when the game is loaded like so:

public class Engine extends MovieClip
{
     var char:Char = new Char(stage);
     stage.addChildAt(char, 1);

     var border1:Border = new Border();
     stage.addChild(border1);

    }

I would like to access the variable border1 that was created in the Engine class inside of my Char class. Is this possible, and if so, how would I do it?

I've tried changing the code so border1 is static like so

static var border1:Border = new Border();

But then I get

error 1012: The static attribute may be used only on definitions inside a class.

1 Answers1

0

You could do this a few ways.

1.  Create the border1 and char instances in reverse order and pass the border1 to the char instance to maintain a reference.

var border1:Border = new Border();
stage.addChild(border1);

var char:Char = new Char(stage, border1);
stage.addChildAt(char, 1);

// Char consructor

public function Char(target:DisplayObjectContainter, border:Border):void {
    ...
    this._border = border;
}

// Now you have a reference to the border1 instance within your char instance.

You could also do this without passing the border1 instance to the constuctor, and instead just add a method that sets this property.

public function setBorder(border:Border):void {
    this._border = border;
}

// OR

public function set border(border:Border):void {
    this._border = border;
}

Another option would be to make the Engine instance maintain a reference to the border and allow communication through the Engine instance. For example,

public class Engine extends MovieClip {

    public var char:Char;
    public var border1:Border;

    public function Engine():void {
        char = new Char(stage);
        stage.addChildAt(char, 1);

        border1 = new Border();
        stage.addChild(border1);
    }

Now, if the char instance has a reference to the engine instance, then the border instance can be referenced by myEngineInstance.border1.

Or you can make the border instance static. Making it static would allow for code inside of the Char instance like:

Engine.border1.<whatever>
sberry
  • 128,281
  • 18
  • 138
  • 165
  • Thanks for the response! However, when I try to make the border instance static I get the error `1012: The static attribute may be used only on definitions inside a class.` – user1547052 Jul 23 '12 at 22:42
  • Sorry about that. I tried to space out my response with the return key and it added the comment before I was ready. – user1547052 Jul 23 '12 at 22:47
  • So, you are trying to add this to the stage? Read the error. It is telling you the problem. To make the instance static, it needs to be a member of a class. – sberry Jul 23 '12 at 22:50
  • Oh, I see -- I have it working now. I had declared the variable in the Engine() function, when I should have declared it before it. This was causing the problem and confusion as it was inside a class, but it was inside a function that was inside a class. Apparently it needs to be inside a class, but outside a function for it to work. Thanks again for all of the help! – user1547052 Jul 23 '12 at 23:21
  • No problem. If my answer has solved your problem, you should mark it as accepted by clicking the checkmark next to it. – sberry Jul 24 '12 at 01:28