Copy link to clipboard
Copied
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?
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
...Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
what do you mean by, '..it didn't work'?
was there an error?
Copy link to clipboard
Copied
1067: Implicit coercion of a value of type flash.display:Stage to an unrelated type CharacterClasses:Main.
Copy link to clipboard
Copied
Should rootRef:Main be rootRef:somethingelse?
Copy link to clipboard
Copied
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!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now