Copy link to clipboard
Copied
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at map_fla::MainTimeline/limit()
at map_fla::MainTimeline/moving()
here is my code
backS1.addEventListener(MouseEvent.MOUSE_DOWN, goS1);
function goS1(Event😞void{
gotoAndStop(1, "Scene 1");
}
F5.addEventListener(MouseEvent.MOUSE_DOWN, gotoF5);
function gotoF5(Event😞void{
gotoAndStop(5);
}
var toR:Boolean=false;
var toL:Boolean=false;
var toU:Boolean=false;
var toD:Boolean=false;
stage.addEventListener(KeyboardEvent.KEY_UP, notClick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onClick);
stage.addEventListener(Event.ENTER_FRAME, moving);
function onClick(event:KeyboardEvent😞void {
switch (event.keyCode) {
case Keyboard.RIGHT :
toR=true;
break;
case Keyboard.LEFT :
toL=true;
break;
case Keyboard.UP :
toU=true;
break;
case Keyboard.DOWN :
toD=true;
break;
}
}
function notClick(event:KeyboardEvent😞void {
switch (event.keyCode) {
case Keyboard.RIGHT :
toR=false;
break;
case Keyboard.LEFT :
toL=false;
break;
case Keyboard.UP :
toU=false;
break;
case Keyboard.DOWN :
toD=false;
break;
}
}
function moving(event:Event😞void {
limit(map);
if (toR) {
map.x-=5;
}
if (toL) {
map.x+=5;
}
if (toU) {
map.y+=5;
}
if (toD) {
map.y-=5;
}
}
navU.addEventListener(MouseEvent.MOUSE_DOWN, goU);
navD.addEventListener(MouseEvent.MOUSE_DOWN, goD);
navR.addEventListener(MouseEvent.MOUSE_DOWN, goR);
navL.addEventListener(MouseEvent.MOUSE_DOWN, goL);
navC.addEventListener(MouseEvent.MOUSE_DOWN, goC);
function goU(Event😞void{
map.y += 25;
}
function goD(Event😞void{
map.y -= 25;
}
function goR(Event😞void{
map.x -= 25;
}
function goL(Event😞void{
map.x += 25;
}
function goC(Event😞void{
map.x = stage.stageWidth/2;
map.y = stage.stageHeight/2;
}
map.addEventListener(MouseEvent.MOUSE_DOWN, Drag);
map.addEventListener(MouseEvent.MOUSE_UP, Drop);
map.addEventListener(MouseEvent.MOUSE_OUT, Drop);
function Drag(Event😞void {
map.startDrag();
}
function Drop(Event😞void {
map.stopDrag();
}
function limit(o:MovieClip) {
if (o.x + 364.5 < 500) {
o.x = 500 - 364.5;
}
else
if (o.x - 364.5 > 50) {
o.x = 50 + 364.5;
}
if (o.y + 309.5 < 375) {
o.y = 375 - 309.5;
}
else
if (o.y - 309.5 > 25) {
o.y = 25 + 309.5;
}
}
when I run my code, it's going well. the problem when I switch to another scene or frame it shows error #1009 and become lagging. but the code can still running. I dont know where the problem is. I'm totally noob. ty in advance.
Copy link to clipboard
Copied
it seems that your map doesn´t exist in any scene/frame that wants to execute
limit(map);
if you for example have limit(map) on frame 24 of your Scene 1, but the MovieClip map only "lives" on frames 1-23
you will get an error
Copy link to clipboard
Copied
any advice how to fix it?
Copy link to clipboard
Copied
One way would be to remove the event listener that calls the moving function whenever you leave the frames where map exists...
stage.removeEventListener(Event.ENTER_FRAME, moving);
Another option would be to test for the existence of map in the moving function before you execute any code that targets it.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now