TypeError:错误 #1009:无法访问空对象的属性或方法
Posted
技术标签:
【中文标题】TypeError:错误 #1009:无法访问空对象的属性或方法【英文标题】:TypeError: Error #1009: Cannot access a property or method of a null object 【发布时间】:2015-05-27 09:58:05 【问题描述】:我的 as 代码有一些问题,如下所示,每当我的物品碰到相机时,它就会弹出:
TypeError:错误 #1009:无法访问 null 的属性或方法 对象引用。在 Prototype1/hitCamera1()
TypeError:错误 #1009:无法访问 null 的属性或方法 对象引用。在 Prototype1/hitCamera2()
TypeError:错误 #1009:无法访问 null 的属性或方法 对象引用。在 Prototype1/hitCamera3()
更新 #1:我意识到错误在于“channel3.stop();”每个命中相机上的代码。为什么上面有错误?
stop();
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var Test_hitting:Boolean = false;
var backSound2:VolatileReaction = new VolatileReaction();
var channel3:SoundChannel = backSound2.play();
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keydown);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, movePlayer);
stage.addEventListener(Event.ENTER_FRAME, hitCamera1);
stage.addEventListener(Event.ENTER_FRAME, hitCamera2);
stage.addEventListener(Event.ENTER_FRAME, hitCamera3);
function keyPressedDown(event:KeyboardEvent)
if(event.keyCode == 37)
leftArrow = true;
else if (event.keyCode == 39)
rightArrow = true;
else if(event.keyCode == 38)
upArrow = true;
else if (event.keyCode == 40)
downArrow = true;
function keyPressedUp (event:KeyboardEvent)
if(event.keyCode == 37)
leftArrow = false;
else if (event.keyCode == 39)
rightArrow = false;
else if (event.keyCode == 38)
upArrow = false;
else if(event.keyCode == 40)
downArrow =false;
function hitCamera1(event:Event)
if(camera1.cameralight.hitTestPoint(Player.x - Player.width/2, Player.y - Player.width/2, true))
channel3.stop();
gotoAndStop("gameover");
function hitCamera2(event:Event)
if(camera2.cameralight.hitTestPoint(Player.x - Player.width/2, Player.y - Player.width/2, true))
channel3.stop();
gotoAndStop("gameover");
function hitCamera3(event:Event)
if(camera3.cameralight.hitTestPoint(Player.x - Player.width/2, Player.y - Player.width/2, true))
channel3.stop();
gotoAndStop("gameover");
function movePlayer(event:Event)
var speed:Number = 3;
if(leftArrow)
Player.x -=speed;
if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallLeft)|| Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
Player.x +=speed;
if(Player.hitTestObject(exit))
channel3.stop();
gotoAndStop("secondStage");
if(rightArrow)
Player.x += speed;
if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallRight)|| Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
Player.x -=speed;
if(Player.hitTestObject(exit))
channel3.stop();
gotoAndStop("secondStage");
if(upArrow)
Player.y -= speed;
if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallUp)|| Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
Player.y += speed;
if(Player.hitTestObject(exit))
channel3.stop();
gotoAndStop("secondStage");
if(downArrow)
Player.y +=speed;
if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallDown) || Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
Player.y-=speed;
if(Player.hitTestObject(exit))
channel3.stop();
gotoAndStop("secondStage");
【问题讨论】:
当对象为null
时会触发该错误,因为它不存在或具有其他名称!所以试着验证你的相机和他们的灯......
【参考方案1】:
因为你在EnterFrame上调用了HitCamera函数,可能是“channel3”为空。
在 HitCamera1、2 和 3 上试试这个
// instead of
channel3.stop();
// use
if( channel3 ) channel3.stop();
【讨论】:
当我输入 if(channel3) channel3.stop(); ,它仍然显示此行的 null。 如果 channel3 为空......它将保持为空。只是您避免在空对象上调用方法 stop() 。因此您不会收到任何运行时错误。以上是关于TypeError:错误 #1009:无法访问空对象的属性或方法的主要内容,如果未能解决你的问题,请参考以下文章
TypeError:错误 #1009:无法访问空对象引用的属性或方法。尝试创建按钮时。
TypeError:错误 #1009:无法访问空对象引用的属性或方法 - Event.ENTER_FRAME
TypeError:错误#1009:无法访问null对象引用as3的属性或方法
SyntaxError:错误 #1009:无法访问空对象引用的属性或方法