Skip to main content
Inspiring
September 19, 2015
Answered

Accessing Document Class Function From Other Class

  • September 19, 2015
  • 1 reply
  • 727 views

I have this code in Enemy.as which calls a function playerTurn() in Main.as, which is also the document class.

var rootRef:Main;

rootRef.playerTurn();

Here is playerTurn()

public function playerTurn(): void {

  player_turn = true

  menu = battle_men

  menu.attackBtn.addEventListener(MouseEvent.CLICK, atkClicked);

  }

Once this code runs, I get

TypeError: Error #1009: Cannot access a property or method of a null object reference.

  at CharacterClasses::Enemy/initEnemyTurn()

The 23rd line in Enemy.as is rootRef.playerTurn();

Why am I getting this error?

This topic has been closed for replies.
Correct answer mcdermitt

Should rootRef:Main be rootRef:somethingelse?


I got it.

Object(parent).playerTurn() worked fine, but the function in which that line was run was based on an event listener, like so

public function initEnemyTurn(evt:Event):void{

  removeEventListener(Event.ENTER_FRAME, initEnemyTurn);

  player_turn = Main.player_turn;

  if(player_turn){

  addEventListener(Event.ENTER_FRAME, initEnemyTurn);

  }

  else{

  Object(parent).playerTurn();

  }

  }

initEnemyTurn is attached to an ENTER_FRAME listener, which was not readded in the else statement, thus breaking the code.

The reason it was remove in the first line of initEnemyTurn is so it doesn't run twice while the first is trying to determine player_turn.

So it turns out none of this had to do with accessing the function...

Thanks for the help!

1 reply

kglad
Community Expert
Community Expert
September 20, 2015

rootRef is null

my guess is your setup isn't good.  your enemy class shouldn't be doing anything to your player class.  and i'm doubtful your player class should be the document class.

that said, you can get a reference to the document class using:

this.addEventListener(Event.ADDED_TO_STAGE,addedF);  // in your enemy constructor

// in your enemy class

private function addedF(e:Event):void{

rootRef=this.stage;

}

mcdermittAuthor
Inspiring
September 20, 2015

The Main.as doesn't attach to the player. It only handles the 'emcee' side of things (deactivating the menu, triggering the animations in Player.as, determining whose turn it is, etc.)

Should rootRef still be defined as Main when addedF runs? Otherwise I get a 1067: Implicit Coercion of a value of type flash.display.stage to an unrelated type etc.

kglad
Community Expert
Community Expert
September 21, 2015

your choice of variable name (and class name) are misleading then.

root always refers to the main timeline.  and Main reinforces that and typically references the document class.

but names are just names so, while they can cause confusion, they don't cause errors.  but poorly chosen names make errors more likely.

in any case, i have no idea of the relationship (class-wise) between playerTurn and Main.

p.s. playerTurn looks like an instance name from a PlayerTurn class.