Copy link to clipboard
Copied
Hi guys:)
I am trying to make a platform game with obstacles and coins. The coins are defined in a class. The code works fine until the player has lost all his health and is gameover. Then I get the error message that I have written above.
Here the code:
stop();
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.display.Sprite;
//vars (define events and functions)
var bulletHolder:Sprite=new Sprite();
addChild(bulletHolder);
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean=false;
var leftKeyIsDown:Boolean=false;
var upKeyIsDown:Boolean=false;
var downKeyIsDown:Boolean=false;
var spaceKeyIsDown:Boolean=false;
var cantShoot:Boolean=false;
var canShoot:Boolean=false;
var pistolCounter:int=0;
var pistolCountFrame:int=12;
var coinCount:int;
var hitObstacle:Boolean=false; // keeps track if obstacle is hit
var health=6;
health_txt.text=health.toString();
//propreties for player
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.RIGHT)
{
rightKeyIsDown=true;
}
if(event.keyCode==Keyboard.LEFT)
{
rightKeyIsDown=true;
}
if(event.keyCode==Keyboard.UP)
{
upKeyIsDown=true;
}
if(event.keyCode==Keyboard.DOWN)
{
downKeyIsDown=true;
}
if(event.keyCode==Keyboard.SPACE)
{
spaceKeyIsDown=true;
}
}
function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.RIGHT)
{
rightKeyIsDown=false;
}
if(event.keyCode==Keyboard.LEFT)
{
rightKeyIsDown=false;
}
if(event.keyCode==Keyboard.UP)
{
upKeyIsDown=false;
}
if(event.keyCode==Keyboard.DOWN)
{
downKeyIsDown=false;
}
if(event.keyCode==Keyboard.SPACE)
{
spaceKeyIsDown=false;
cantShoot=false;
}
}
function moveThePlayer(event:Event):void {
if(rightKeyIsDown)
{
player_mc.gotoAndStop(2);
}
if(leftKeyIsDown)
{
player_mc.gotoAndStop(2);
}
if(downKeyIsDown)
{
player_mc.gotoAndStop(2)
}
if(upKeyIsDown)
{
player_mc.gotoAndStop (3);
}
if(spaceKeyIsDown)
{
player_mc.gotoAndStop(3);
}
if(pistolCounter<pistolCountFrame)
{
pistolCounter++
}
if(pistolCounter>=pistolCountFrame)
{
canShoot=true;
pistolCounter=0;
}
if(spaceKeyIsDown&&!cantShoot&&canShoot)
{
var bullet_mc:MovieClip=new Bullet();
bullet_mc.x=player_mc.x
bullet_mc.y=player_mc.y
bullet_mc.rotation=player_mc.rotation_mc.rotation
bulletHolder.addChild(bullet_mc);
cantShoot=true;
canShoot=false;
}
}
if(rightKeyIsDown)
{
player_mc.rotation_mc.rotation=0
}
if(leftKeyIsDown)
{
player_mc.rotation_mc.rotation=180
}
//defines player
stage.addEventListener(KeyboardEvent.KEY_UP, run);
function run(e:KeyboardEvent):void{
player_mc.gotoAndStop(1);
}
stage.addEventListener(KeyboardEvent.KEY_UP, checkkeysup);
var speed=10;
var moveright=false;
function checkkeysdown(mykey:KeyboardEvent) {
if (mykey.keyCode==Keyboard.RIGHT) {
moveright=true;
}
}
function checkkeysup(mykey:KeyboardEvent) {
if (mykey.keyCode==Keyboard.RIGHT) {
moveright=false;
}
}
//defines obstacles
stage.addEventListener(Event.ENTER_FRAME, gameloop);
function gameloop(e:Event):void{
obstacle_mc.x-=20;
if (obstacle_mc.x<-100){
obstacle_mc.x=650;
hitObstacle=false;
}
if (player_mc.hitTestObject(obstacle_mc)) {
if (hitObstacle==false){ // only subtract health if hitObstacle is false
health--;
}
hitObstacle=true;
health_txt.text=health.toString();
if (health<=0){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, moveThePlayer);
stage.removeEventListener(KeyboardEvent.KEY_UP, run);
stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
stage.removeEventListener(Event.ENTER_FRAME, axtloop);
gotoAndStop(1, "Scene 3");
}
}
}
stage.addEventListener(Event.ENTER_FRAME, axtloop);
function axtloop(e:Event):void{
axt_mc.x-=20;
if (axt_mc.x<-100){
axt_mc.x=650;
hitObstacle=false;
}
if (player_mc.hitTestObject(axt_mc)) {
if (hitObstacle==false){ // only subtract health if hitObstacle is false
health--;
}
hitObstacle=true;
health_txt.text=health.toString();
if (health<=0){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, moveThePlayer);
stage.removeEventListener(KeyboardEvent.KEY_UP, run);
stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
stage.removeEventListener(Event.ENTER_FRAME, axtloop);
gotoAndStop(1, "Scene 3");
}
}
//counts coins
coinCount_txt.text="coins:"+coinCount;
}
The code for the coins:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.net.dns.AAAARecord;
public class Coin extends MovieClip
{
var player_mc:MovieClip;
public var MainTimeLine=MovieClip(root);
public function Coin()
{
// constructor code
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
player_mc=MovieClip(root).player_mc
if(this.hitTestObject(player_mc))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MainTimeLine.coinCount++;
}
}
}
}
I really would appreciate some help because I am trying now since 5 days to solve the problem without success.
Many thanks in advance
Copy link to clipboard
Copied
click file>publish settings>swf and tick 'permit debugging'. retest.
the problematic line number will be in the error message. if that isn't enough info to allow you to quickly correct your problem, copy and paste the error message and indicate the problematic line number mentioned at the top of the message.
Copy link to clipboard
Copied
Hi kglad:)
Many thanks for your reply.
Here is the full error message:
Fonts should be embedded for any text that may be edited at runtime, other than text with the "Use Device Fonts" setting. Use the Text > Font Embedding command to embed fonts.[SWF] Game%20copy%202.swf - 191091 bytes after decompressionTypeError: Error #1009: Cannot access a property or method of a null object reference. at Gamecopy2_fla::MainTimeline/axtloop()[Gamecopy2_fla.MainTimeline::frame1:213]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/update()[/Users/sonjacerny/Desktop/Platform Game/Bullet.as:30]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/update()[/Users/sonjacerny/Desktop/Platform Game/Bullet.as:30]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/update()[/Users/sonjacerny/Desktop/Platform Game/Bullet.as:30]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/update()[/Users/sonjacerny/Desktop/Platform Game/Bullet.as:30]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/update()[/Users/sonjacerny/Desktop/Platform Game/Bullet.as:30]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/update()[/Users/sonjacerny/Desktop/Platform Game/Bullet.as:30]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23]TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:23][UnloadSWF] Game%20copy%202.swfTest Movie terminated.
Do you think you would be able to find a solution?
Many thanks in advance
Date: Tue, 22 Apr 2014 10:36:48 -0700
From: forums_noreply@adobe.com
To: sonjacerny@hotmail.com
Subject: cannot access a property of a null object reference
Re: cannot access a property of a null object reference
created by kglad in ActionScript 3 - View the full discussion
click file>publish settings>swf and tick 'permit debugging'. retest.
the problematic line number will be in the error message. if that isn't enough info to allow you to quickly correct your problem, copy and paste the error message and indicate the problematic line number mentioned at the top of the message.
Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at http://forums.adobe.com/message/6320167#6320167
Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
To unsubscribe from this thread, please visit the message page at . In the Actions box on the right, click the Stop Email Notifications link.
Start a new discussion in ActionScript 3 at Adobe Community
For more information about maintaining your forum email notifications please go to http://forums.adobe.com/thread/416458?tstart=0.
Copy link to clipboard
Copied
Hi Sonja,
In case you don't realize it, if you are answering by email instead of using the forum, by copying the same answer to different people you end up repeating it in the forum. It is much better if you participate in the forum, especially with all the default response stuff that your messages include, like repeating the response you are answering as well as a slew of statments regarding using email to respond... you oughta visit the forum to see what I mean.
Copy link to clipboard
Copied
Hi Ned, yeah I realize it now:)
I will removed the bullets. Too difficult. The main problem is the coins because they have to stay. I set a motion tween for them and pasted a good bit of them on the stage. How can I remove them when I go to the gameover screen? Well I guess that is what I have to do because that's were the error message comes.
Copy link to clipboard
Copied
If you show the code at and around line 23 of the Coin.as file one of us should be able to help you out.
The bullets might not be that bad once you get thru the coins, so just comment them out instead of obliterating them.
Copy link to clipboard
Copied
That would be great.
Here the code:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.net.dns.AAAARecord;
public class Coin extends MovieClip
{
var player_mc:MovieClip;
public var MainTimeLine=MovieClip(root);
public function Coin()
{
// constructor code
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
player_mc=MovieClip(root).player_mc
if(this.hitTestObject(player_mc))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MainTimeLine.coinCount++;
}
}
}
}
Copy link to clipboard
Copied
PS:
Line 23 is player_mc=MovieClip(root).player_mc
Copy link to clipboard
Copied
The problem might lie in you not waiting long enough before trying to determine what the root is relative to the Coin. It might be coming up null from the start. Try changing the code to wait until it gets added to the stage before trying to target the root...
public function Coin()
{
this.addEventListener(Event.ADDED, initialize);
}
private function initialize(evt:Event):void
{
MainTimeLine=MovieClip(root);
this.addEventListener(Event.ENTER_FRAME, update);
}
and use MainTimeLine to target player_mc or anything else relative to the root from that point forward. You mix up the way you target the root thereafter, so whichever way you go, try to keep it consistent
Copy link to clipboard
Copied
The error message still comes up:
at Gamecopy_fla::MainTimeline/axtloop()[Gamecopy_fla.MainTimeline::frame1:213]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:30]
How could I change this line: "player_mc=MovieClip(root).player_mc", so that it is consistent as you said?
Copy link to clipboard
Copied
It is no longer pointing at line 23, it is indicating line 30.
If you wanted to be consistent you could use MainTimeLine instead of MovieClip(root), though if the only place it gets used is within that function, you might be better off just using MovieClip(root) and forgetting about creating/using the MainTimeLine variable.
There is a portion of another error you start with that points to line 213 of the main timeline, though there might be more to that error that identifies the line better (the first line indicated is where the error is).
Copy link to clipboard
Copied
Hi I tried to what you said. But I just don't manage it. When I take the MainTimeLine out, nothing works anymore. Could you may give me a code example how to change it? Strart to get desperate. I put a lot of work in that game and hope somebody can give me a solution:)
Copy link to clipboard
Copied
Here the code again:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.net.dns.AAAARecord;
public class Coin extends MovieClip
{
var player_mc:MovieClip;
public var MainTimeLine=MovieClip(root);
public function Coin()
{
this.addEventListener(Event.ADDED, initialize);
}
private function initialize(evt:Event):void
{
MainTimeLine=MovieClip(root);
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
player_mc=MovieClip(root).player_mc
if(this.hitTestObject(player_mc))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MainTimeLine.coinCount++;
}
}
}
}
Line 30: player_mc=MovieClip(root).player_mc
When I take the MainTImeLIne out it says that player_mc is undefined.
Copy link to clipboard
Copied
You didn't adjust the code for the changes - there is no need to assign anything to the variable until it is ready to be assigned. That's the whole purpose of using the ADDED listener... it is waiting until the Coin has a root before trying to assign it.
The following is what I meant. Note that I am changing the variable name to be consistent with standard practices. Class names start with capital letters, but variables and function do not...
package {
import flash.display.MovieClip;
import flash.events.*;
public class Coin extends MovieClip {
private var mainTimeLine:MovieClip;
public function Coin() {
this.addEventListener(Event.ADDED, initialize);
}
private function initialize(evt:Event):void {
mainTimeLine = MovieClip(root);
this.addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void {
if(this.hitTestObject(mainTimeLine.player_mc)){
this.removeEventListener(Event.ENTER_FRAME, update);
MovieClip(parent).removeChild(this);
mainTimeLine.coinCount++;
}
}
}
}
Or you could eliminate using the mainTimeLine variable altogether and just use MovieClip(root) instead, though then you have to be careful that you don't try to target the root after you remove the child.
Copy link to clipboard
Copied
I took your code and pasted it in.
Now I still get an error message.
Looks like this:
Fonts should be embedded for any text that may be edited at runtime, other than text with the "Use Device Fonts" setting. Use the Text > Font Embedding command to embed fonts.
[SWF] Game%20copy.swf - 190939 bytes after decompression
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Gamecopy_fla::MainTimeline/axtloop()[Gamecopy_fla.MainTimeline::frame1:213]
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:20]
TypeError: Error #2007: Parameter hitTestObject must be non-null.
Is it possibel to fix that or do I have to rewrite the code? Would it be easier to create the funtion for the coins in that way in the AS3 code instead of the Class code?
Copy link to clipboard
Copied
Any error can be fixed. Usually fixing means rewriting to some extent or another, but it is doubtful you have to start from sctratch if that is what you are wondering.
You should learn how to read/interpret the error messages. The 1009 error is indicating that you have a problem with line 213 in frame 1 of the main timeline of the Gamecopy file. Knowing that, you should investigate why some object being targeted on that line is being seen as null. My first response provided a list of the most common possibile reasons.
The 2007 error appears to be indicating that whatever object you are hitTesting for is coming up null as well. That means the mainTimeLine.player_mc object is not being found. To troubleshoot that you could put traces for ...
trace("timeline: "+mainTimeLine);
trace("player: "+mainTimeLine.player_mc);
just before that line (20) to see which part of it is failing to be found. It might be as simple as you missing assigning a name to the object you refer to as player_mc
Copy link to clipboard
Copied
HI Ned.
The Line 213 is: coinCount_txt.text="coins:"+coinCount;
It is part of following code:
stage.addEventListener(Event.ENTER_FRAME, axtloop);
function axtloop(e:Event):void{
axt_mc.x-=20;
if (axt_mc.x<-100){
axt_mc.x=650;
hitObstacle=false;
}
if (player_mc.hitTestObject(axt_mc)) {
if (hitObstacle==false){ // only subtract health if hitObstacle is false
health--;
}
hitObstacle=true;
health_txt.text=health.toString();
if (health<=0){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, moveThePlayer);
stage.removeEventListener(KeyboardEvent.KEY_UP, run);
stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
stage.removeEventListener(Event.ENTER_FRAME, axtloop);
gotoAndStop(1, "Scene 3");
}
}
coinCount_txt.text="coins:"+coinCount;
}
The player is just called player_mc. Could I write the coin.as code without the mainTimeLine? And when yes how?
Because that seems to be the part that is null. I took the inspiration for the code from a tutorial.
I think what you said may be right, that the problem can be solved but I really don't know how. This is my first game in Actionscript3.
Here your overworked code again.
package {
import flash.display.MovieClip;
import flash.events.*;
public class Coin extends MovieClip {
private var mainTimeLine:MovieClip;
public function Coin() {
this.addEventListener(Event.ADDED, initialize);
}
private function initialize(evt:Event):void {
mainTimeLine = MovieClip(root);
this.addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void {
trace("timeline: "+mainTimeLine);
trace("player: "+mainTimeLine.player_mc);
if(this.hitTestObject(mainTimeLine.player_mc)){
this.removeEventListener(Event.ENTER_FRAME, update);
MovieClip(parent).removeChild(this);
mainTimeLine.coinCount++;
}
}
}
}
Copy link to clipboard
Copied
The trace function is used to help troubleshoot code. You can use it to check the status of things while code processes to help see why some processing might not be working as expected.
If the line....
coinCount_txt.text="coins:"+coinCount;
is causing the 1009 error, then you should say to yourself... 'some object being targeted in that line is coming up null'. So the next step is to see which it might be. If there is only one, then it is somewhat obvious. But if there are more, you can use the trace function. You can use it anyways just to confirm that the object is coming up null.
There are only two objects involved in that line, one of which is a variable, which I would normally be less suspiscious of, but for the sake of seeing, try tracing both before that line...
trace("textfield: "+coinCount_txt);
trace("variable: "+coinCount);
Test the file and see what those traces show in the output. One of them should be coming up null.
The same goes for the other traces I told you to add... see what they end up displaying in the output panel to determine where the problem lies... see which object is coming up null.
Copy link to clipboard
Copied
Yeah the variable is null.
I have the variable like this: var coinCount:int;
In the output it says:
textfield: [object TextField]
variable: 0
Copy link to clipboard
Copied
Although an int defaults to a value of 0, (I think), just assign it a solid 0 to start with and see if that helps alleviate that error (assuming that would be the value you start with)...
var coinCount:int = 0;
Copy link to clipboard
Copied
Hi I solved the problem with the coinCount:)
The only problem is the line20: player_mc = MovieClip(root).player_mc;
It says:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Coin/update()[/Users/sonjacerny/Desktop/Platform Game/Coin.as:20]
why is that null? I made a variable for it?
Copy link to clipboard
Copied
I get a feeling like you are turning back to where you started. I didn't use that line in my version in case you still have it, and the use of MovieClip(root) in that line went away as well. Did you check the results of the traces I told you to put for that line?
Copy link to clipboard
Copied
No I don't like to go back were I started at all. I tried your version but it didn't make any difference. On the opposite. I got more error messages. Yes I made a trace for that line. I did read a bit on that problem and one thing that I found it that you have remove the listener when it gets to gameover. I tried to do so but without success.
The full code of Coin.as looks now like this:
package
{
import flash.display.MovieClip;
import flash.events.*;
public class Coin extends MovieClip
{
var health=6;
var player_mc:MovieClip;
private var MainTimeLine=MovieClip(root);
public function Coin()
{
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
trace("variable:"+player_mc);
player_mc = MovieClip(root).player_mc;
if(this.hitTestObject(player_mc))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MainTimeLine.coinCount++;
}
if (health<=0){
this.removeEventListener(Event.ENTER_FRAME, update);
gotoAndStop(1, "Scene 3");
}
}
}
}
The gameover part of ACS:
stage.addEventListener(Event.ENTER_FRAME, gameloop);
function gameloop(e:Event):void{
obstacle_mc.x-=20;
if (obstacle_mc.x<-100){
obstacle_mc.x=650;
hitObstacle=false;
}
if (player_mc.hitTestObject(obstacle_mc)) {
if (hitObstacle==false){ // only subtract health if hitObstacle is false
health--;
}
hitObstacle=true;
health_txt.text=health.toString();
if (health<=0){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, moveThePlayer);
stage.removeEventListener(KeyboardEvent.KEY_UP, run);
stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
gotoAndStop(1, "Scene 3");
}
}
}
When the player has lost all his health the Scene changes from Scene1 to Scene3. There is were the error message comes. So I guess that the problem must be there that something is still on the stage that shouldn't be there anymore. I really try very hard. I don't think it is the player that is null either. I think it is probably the MainTimeLine because the default of that is null. Am I right? The only question then would be how to get that problem finally out of the world?
Copy link to clipboard
Copied
I'm not going to be able to try to help you further. You did step back, all the way back, ignoring the problems I already explained you had earlier, adding them back in.
Copy link to clipboard
Copied
The 1009 error indicates that one of the objects being targeted by your code is out of scope. This could mean that the object....
- is declared but not instantiated
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s)
- is an object that was removed but code is still trying to target it
If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.