Copy link to clipboard
Copied
Hi, I'm creating an android game and everything goes wrong when you lose and the "lose screen" is displayed...
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at rock/destroyEnemy()
at Function/main/$construct/endGame()
at Function/main/$construct/Loop()
(this is in the main class file)
for each (var enemy:rock in aEnemyArray)
{
//destroy enemy in loop
enemy.destroyEnemyAgain();
//remove enemy from array
aEnemyArray.splice(0, 1);
}
(this is in the enemy class file)
public function destroyEnemy():void
{
parent.removeChild(this)
removeEventListener(Event.ENTER_FRAME, enemyLoop);
}
The thing is that if you don't shot at he rocks there´s no problem, but when you hit one, at the time you lose, the error comes.
Thanks.
Use the trace function to track things down. If something exists one memoent and not the next, you need to intercept that processing between that existence change and make sure you are not still looking for the object. If you are retaining an array that references the object then make sure you remove it from that array before the next loop cyce starts.
Copy link to clipboard
Copied
Which is line 108 of the rock.as file?
Copy link to clipboard
Copied
enemy.destroyEnemyAgain();
Copy link to clipboard
Copied
in the code its enemy.destroyEnemy(); not enemy.destroyEnemyAgain(); ... just forget the "Again"
Copy link to clipboard
Copied
You indicated that line is in the main file in your first posting, not the rock.as file.
chances are that for loop is trying to target an enemy after it has been removed already. Try tracing the enemy on a line before that to confirm that is the case.
Copy link to clipboard
Copied
sorry, I´m a mess right now.
the line is parent.removeChild(this).
I'll try that and let you know what happen
Copy link to clipboard
Copied
yeah, that is. Any idea how can I fix it?
Copy link to clipboard
Copied
Use the trace function to track things down. If something exists one memoent and not the next, you need to intercept that processing between that existence change and make sure you are not still looking for the object. If you are retaining an array that references the object then make sure you remove it from that array before the next loop cyce starts.
Copy link to clipboard
Copied
Solved! thank you so much!
Copy link to clipboard
Copied
You're welcome