错误#1009:无法访问空对象引用的属性或方法。 在MethodInfo-10()。运行此代码时出现此错误。谁能告诉我我做错了什么。它没有显示错误的位置和错误的行。感谢
package
{
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends MovieClip
{
public function Main()
{
stop();
start_btn.addEventListener(MouseEvent.CLICK, gothere);
function gothere(clickInfo:MouseEvent)
{
start_btn.removeEventListener(MouseEvent.CLICK, gothere);
nextFrame();
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
function key_pressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
player.y += -5;
if ((wall1.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)))
{
player.y += 5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback);
function goback(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback);
prevFrame();
}
}
else if ((wall1.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)))
{
player.y += 5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback1);
function goback1(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback1);
prevFrame();
}
}
else if (player.hitTestObject(finish))
{
gotoAndStop(4);
}
break;
};
case Keyboard.DOWN :
{
player.y += 5;
if ((wall1.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)))
{
player.y += -5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback2);
function goback2(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback2);
prevFrame();
}
}
else if ((wall1.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)))
{
player.y += -5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback3);
function goback3(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback3);
prevFrame();
}
}
else if (player.hitTestObject(finish))
{
gotoAndStop(4);
}
break;
};
case Keyboard.LEFT :
{
player.x += -5;
if ((wall1.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true)))
{
player.x += 5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback4);
function goback4(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback4);
prevFrame();
}
}
else if ((wall1.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x - player.width/2, player.y + player.height/2,true)))
{
player.x += 5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback5);
function goback5(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback5);
prevFrame();
}
}
else if (player.hitTestObject(finish))
{
gotoAndStop(4);
}
break;
};
case Keyboard.RIGHT :
{
player.x += 5;
if ((wall1.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true)))
{
player.x += -5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback6);
function goback6(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback6);
prevFrame();
}
}
else if ((wall1.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)) || (wall2.hitTestPoint(player.x + player.width/2, player.y + player.height/2,true)))
{
player.x += -5;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goback7);
function goback7(event:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goback7);
prevFrame();
}
}
else if (player.hitTestObject(finish))
{
gotoAndStop(4);
}
break;
}
}
}
}
}
};
答案 0 :(得分:2)
首先让你的代码不是那么可怕 - 超重 - 重复。还有,在函数内部定义的函数内定义的函数?在如此多的层面上都错了。
package
{
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
public class Main extends MovieClip
{
public function Main()
{
stop();
start_btn.addEventListener(MouseEvent.CLICK, goThere);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
}
private function goBack(e:MouseEvent):void
{
playagain_btn.removeEventListener(MouseEvent.CLICK, goBack);
prevFrame();
}
private function goThere(clickInfo:MouseEvent):void
{
start_btn.removeEventListener(MouseEvent.CLICK, gothere);
nextFrame();
stage.focus = stage;
}
private function testPlayer():Boolean
{
if (wall1.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true) return true;
if (wall2.hitTestPoint(player.x - player.width/2, player.y - player.height/2,true) return true;
if (wall1.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true) return true;
if (wall2.hitTestPoint(player.x + player.width/2, player.y - player.height/2,true) return true;
return false;
}
private function movePlayer(dx:Number, dy:Number):void
{
player.x += dx;
player.y += dy;
if (player.hitTestObject(finish))
{
gotoAndStop(4);
}
else if (testPlayer())
{
player.x -= dx;
player.y -= dy;
nextFrame();
playagain_btn.addEventListener(MouseEvent.CLICK, goBack);
}
}
private function onKey(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
movePlayer(0,-5);
break;
case Keyboard.DOWN:
movePlayer(0,5);
break;
case Keyboard.LEFT:
movePlayer(-5,0);
break;
case Keyboard.RIGHT:
movePlayer(5,0);
break;
}
}
}
}