Copy link to clipboard
Copied
i'm making a simple game and i'm still learning as3 I want to go at the next frame but get the null error. it is a maze game.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Senzanome_9_Scena1_fla::MainTimeline/stage_onEnterFrame()
here is the code
stop ();
next.visible=false;
var rightArrow:Boolean = false;
var leftArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var speed:int = 5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, stage_onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, stage_onKeyUp);
stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
function stage_onKeyDown(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = true;
if(event.keyCode == Keyboard.LEFT) leftArrow = true;
if(event.keyCode == Keyboard.UP) upArrow = true;
if(event.keyCode == Keyboard.DOWN) downArrow = true;
}
function stage_onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = false;
if(event.keyCode == Keyboard.LEFT) leftArrow = false;
if(event.keyCode == Keyboard.UP) upArrow = false;
if(event.keyCode == Keyboard.DOWN) downArrow = false;
}
function stage_onEnterFrame(event:Event):void {
var rect:Rectangle = player.getBounds(this);
var i:int = 0;
var xBump:int = 0;
var yBump:int = 0;
if(rightArrow) {
xBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.right + i, player.y, true)) {
xBump = i - 1;
break;
}
}
}
if(leftArrow) {
xBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.left - i, player.y, true)) {
xBump = -i + 1;
break;
}
}
}
if(upArrow) {
yBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(player.x, rect.top - i, true)) {
yBump = -i + 1;
break;
}
}
}
if(downArrow) {
yBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(player.x, rect.bottom + i, true)) {
yBump = i - 1;
break;
}
}
}
player.x += xBump;
player.y += yBump;
pickUp();
function pickUp (){
if (player.hitTestObject(coin1)) {
coin1.x=1000
}
if (player.hitTestObject(coin2)) {
coin2.x=1000
}
if (player.hitTestObject(coin3)) {
coin3.x=1000
}
if (player.hitTestObject(coin4)) {
coin4.x=1000
}
if (player.hitTestObject(coin5)) {
coin5.x=1000
}
if (player.hitTestObject(coin6)) {
coin6.x=1000
{next.visible = true;}
}
}
}
next.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_10);
function fl_ClickToGoToAndStopAtFrame_10(event:MouseEvent):void
{
gotoAndStop(3);
}
Or remove it at this point:
if (player.hitTestObject(coin6)) {
stage.removeEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
coin6.x=1000
next.visible = true;
}
That will be better.
Copy link to clipboard
Copied
I run the movie in debug mode and found the following point that causes that error :
if(maze.hitTestPoint(rect.left - i, player.y, true)) {
xBump = -i + 1;
break;
}
Could you please help me to solve this problem?
Thanks for all help
Copy link to clipboard
Copied
I think that "maze" movie clip is not visible to the main timeline, did you add it in another movie clip?
Copy link to clipboard
Copied
no.. I create a maze at frame 1 and now i want to go at the next frame, but i can't.
what can i do?
Copy link to clipboard
Copied
Remove the enter frame event listener:
function fl_ClickToGoToAndStopAtFrame_10(event:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
gotoAndStop(3);
}
Copy link to clipboard
Copied
Or remove it at this point:
if (player.hitTestObject(coin6)) {
stage.removeEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
coin6.x=1000
next.visible = true;
}
That will be better.
Copy link to clipboard
Copied
thaanks now it's works
Grazie mille
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now