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 ?
I think you must do something like this:
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.