Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Accessing Document Class Function From Other Class

Participant ,
Sep 19, 2015 Sep 19, 2015

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?

TOPICS
ActionScript
615
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Sep 21, 2015 Sep 21, 2015

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

...
Translate
Community Expert ,
Sep 19, 2015 Sep 19, 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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 20, 2015 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2015 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2015 Sep 21, 2015

playerTurn is in Main.as, which is the document class. I am able to call playerTurn from the main timeline, but not from Enemy.as, which I need to do.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2015 Sep 21, 2015

then add this to your Enemy class:

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

// in your enemy class

private function addedF(e:Event):void{

rootRef=this.stage;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2015 Sep 21, 2015

I already tried that and it didn't work.

However, I tried Object(parent).playerTurn(); and it worked.

But when that code tried to run again (the player's second turn) it's telling me #1006 playerTurn is not a function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2015 Sep 21, 2015

what do you mean by, '..it didn't work'?

was there an error?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2015 Sep 21, 2015

1067: Implicit coercion of a value of type flash.display:Stage to an unrelated type CharacterClasses:Main.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2015 Sep 21, 2015

Should rootRef:Main be rootRef:somethingelse?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2015 Sep 21, 2015
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines