Copy link to clipboard
Copied
When i play normally, i didnt encounter any problem...but when i pause and replay the animation, it show Cannot access a property or method of a null object reference.
at line as:80. What is the reason ?how can i solve it?
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Assignment extends MovieClip
{
var vx:int;
var vy:int;
var collisionHasOccurred:Boolean;
var score:uint;
public function Assignment()
{
init();
}
function init():void
{
//Initialize variable
vx=0;
vy=0;
collisionHasOccurred=false;
stone.stop();
score=0;
optionPage.visible=false;
//Add event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.LEFT)
{
vx=-5;
}
else if (event.keyCode==Keyboard.RIGHT)
{
vx=5;
}
else if (event.keyCode==Keyboard.UP)
{
vy=-5;
}
else if (event.keyCode==Keyboard.DOWN)
{
vy=5;
}
}
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode==Keyboard.LEFT || event.keyCode==Keyboard.RIGHT)
{
vx=0;
}
else if (event.keyCode==Keyboard.DOWN || event.keyCode==Keyboard.UP)
{
vy=0;
}
}
function onEnterFrame(event:Event):void
{
//Move the player
player.x+=vx;
player.y+=vy;
//collision detection
if (stone.hitTestObject(player))<-----here is line 80
{
player.gotoAndStop(3);
health.meter.width-=2;
if (! collisionHasOccurred)
{
score++;
messageDisplay.text=String(score);
collisionHasOccurred=true;
}
}
else
{
player.gotoAndStop(1);
collisionHasOccurred=false;
}
if (health.meter.width <=1)
{
New.text="Game Over!";
stop();
fl_CountDownTimerInstance_3.stop();
}
if (player.hitTestObject(wall))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallA))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallB))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallC))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallD))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallE))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallF))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallG))
{
player.x-=vx;
player.y-=vy;
}
if (player.hitTestObject(wallH))
{
player.x-=vx;
player.y-=vy;
}
}
}
}
What are you doing when you pause? Likewise, what are you doing when you replay?
Before line 80 insert the line: trace(stone); and see if it comes up null after you pause/replay.
Copy link to clipboard
Copied
Which line is line 80? What is the complete error message?
Copy link to clipboard
Copied
if (stone.hitTestObject(player))<-----here is line 80
{
player.gotoAndStop(3);
health.meter.width-=2;
if (! collisionHasOccurred)
{
score++;
messageDisplay.text=String(score);
collisionHasOccurred=true;
}
}
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Assignment/onEnterFrame()
Copy link to clipboard
Copied
What are you doing when you pause? Likewise, what are you doing when you replay?
Before line 80 insert the line: trace(stone); and see if it comes up null after you pause/replay.
Copy link to clipboard
Copied
hmm,ya..i successfully correct it..thanks for ur great help,friend.
Copy link to clipboard
Copied
You're welcome... sounds like you did all the work to fix it... good going.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now