Tieme
Tieme

Reputation: 65379

AS3.0: Acces properties of created instance from 2nd class

I'm building a small game.

On my document class i create a instances of the class Character and Level with the following code:

//add the Level
level = new TileGrid();
level.y = 100;
level.x = 400;
addChild(player);

//add our player
player = new Character();
player.y = 150;
player.x = 400;
addChild(player);

I also create a controller class which handles the user input. (for example checks if the player is able to move to the right.)

I also create eventlisteners for keyboardevents and stuff.

When a key is pressed i want to check if the movement is possible by calling the checkTile(tileNumber) function of the TileGrid class from within the controller class.

The controller class looks like this:

package  {
    import flash.events.KeyboardEvent;
    import flash.events.Event;

    public class Controller{

    //Constructor code
    public function Controller(){}

    //Keyboard pressed -> move character
    public function keyPressed(evt:KeyboardEvent):void
    {
        trace(level.checkTile(30));
    }
}

And the TileGrid class looks something like this:

package  {
    import flash.events.KeyboardEvent;
    import flash.events.Event;

    public class TileGrid{

    //Constructor code
    public function TileGrid(){
        //Creating all the tiles and adding them to the stage.
    }

    //Check if a certain tile is walkable
    public function checkTile(tileNumberType){
        if(tileNumberType > 15){
            return false;
        }else{
           return true;
        }
    }
}

But when i test this i get the following error: Line 81 1120: Access of undefined property level.

When i try: trace(Object(parent).level.checkTile(30)); i get: 1120: Access of undefined property parent.

How can i access methods from one class with an instance from a second class ?

Upvotes: 0

Views: 575

Answers (2)

I think you must do something like this:

...
// somewhere in your document class (or somewhere else)
var player:Character = new Character();
var level:TileGrid = new TileGrid();
var controller:Controller = new Controller(player, level);
...

// in your Controller class

private var level:TileGrid;
private var player:Character;

public Controller(player:Character, level:TileGrid) {
    this.player = player;
    this.level = level;
}

public function keyPressed(event:KeyboardEvent):void {
    level.checkTile(30); // in this line "level" means "this.level"
}

In that case you must say which player and which level the controller must control. The controller is a class that has no knowledge of any other class. The variables are not global (in your example, and they shouldn't be) so you can't acces them from everywhere.

Upvotes: 2

cwallenpoole
cwallenpoole

Reputation: 81998

Classes in AS can't access their context. This means that if I have:

function foo():void
{
    var k:MyCustomClass = new MyCustomClass();
    var j:MyOtherClass  = new MyOtherClass();
}

the two instances j and k don't know about each other.

In this particular case, however, because you're clearly dealing with a parent structure, it is possible to get the parent's properties. This is possible because you have a path to the variable in a public scope.

Try this instead:

trace(Object(parent).level.checkTile(30));

Also note the Object(parent). A DisplayObject's parent is a DisplayObjectContainer, which does not have a level property. By wrapping parent in Object, however, you're telling Flash, "It's OK, you should look for this property at runtime not when compiling."

Upvotes: 0

Related Questions