Actionscript 3 错误 1009:无法访问空对象引用的属性或方法
Posted
技术标签:
【中文标题】Actionscript 3 错误 1009:无法访问空对象引用的属性或方法【英文标题】:Actionscript 3 error 1009: Cannot access a property or method of a null object reference 【发布时间】:2018-09-15 20:29:07 【问题描述】:我正在尝试在 Animate CC 上制作一个简单的游戏。一切似乎都正常,除了当我查看输出时,我收到以下错误:
TypeError:错误 #1009:无法访问 null 的属性或方法 对象引用。 在 _2D_CW2_Game_v10_8_fla::MainTimeline/move() 在_2D_CW2_Game_v10_8_fla::MainTimeline/updateOb()
所以我知道问题可能出在哪里,并且我已经尝试调整代码好几天了,谷歌搜索可能的解决方案,但无济于事......
我的整个源代码如下。任何反馈/建议将不胜感激。
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundMixer;
//==================================================
// Variable declaration
//==================================================
// defines the variables for boundaries
var left:Number = 0;
var top:Number = 0;
var right:Number = stage.stageWidth;
var bottom:Number = stage.stageHeight;
var velX:Number = 0;
var velY:Number = 0;
var gravity:Number = 1;
var friction:Number = 0.8;
var bounce:Number = -0.5;
var score:Number = 2;
var cv:Number = 0;
var curCount:Number = 30; // countdown 30s
var rightKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var touchGround:Boolean = false;
// create and place player object on stage
var player:Player = new Player();
player.x = 110;
player.y = 460;
addChild(player);
// create obstacle array
var obstacles:Array = new Array();
var numOb:Number = 3;
// create and place enemies on stage
for (var i:Number = 0; i < numOb; i++)
var ob:Npc = new Npc();
ob.x = 800;
ob.y = 470;
ob.scaleX = -1;
ob.vx = Math.random() * 20 + 1;
addChild(ob);
obstacles.push(ob);
//==================================================
// Event handlers
//==================================================
stage.addEventListener(Event.ENTER_FRAME, EntFrame);
addEventListener(Event.ENTER_FRAME, updateOb);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
//==================================================
// Functions
//==================================================
function keyDown(e:KeyboardEvent):void
if (e.keyCode == Keyboard.RIGHT)
rightKeyDown = true;
if (e.keyCode == Keyboard.LEFT)
leftKeyDown = true;
if (e.keyCode == Keyboard.UP)
// if player isn't already jumping and is on the ground
if (!upKeyDown && touchGround)
// then start jumping
isJumping();
upKeyDown = true;
function keyUp(e:KeyboardEvent):void
if (e.keyCode == Keyboard.RIGHT)
rightKeyDown = false;
if (e.keyCode == Keyboard.LEFT)
leftKeyDown = false;
if (e.keyCode == Keyboard.UP)
upKeyDown = false;
function EntFrame(e:Event):void
player.x += velX;
player.y += velY;
velX *= friction;
velY += gravity;
if (player.y >= 450)
touchGround = true;
player.y = 450;
// boundary checks
if (player.x + player.width/2 > right)
player.x = right - player.width/2;
player.velX *= bounce;
else if (player.x - player.width/2 < left)
player.x = left + player.width/2;
player.velX *= bounce;
// make player move left or right
controls();
if (curCount > 0)
cv++;
if (cv >= 30)
curCount--;
cv = 0;
timertext.text = String(curCount);
if (curCount == 0)
restart();
gotoAndStop("gameOverWon");
function updateOb(e:Event):void
// make obstacles move
for (var i:Number = 0; i < numOb; i++)
var ob:Npc = obstacles[i];
move(ob);
if (player.hitTestObject(obstacles[i]))
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y - player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y - player.height/2, true))*/
bumpOb(obstacles[i]);
scoretext.text = String(score);
if (score == 0)
restart();
gotoAndStop("gameOverLost");
// applies basic velocity to enemies
function move(moveOb)
moveOb.x -= moveOb.vx;
if (moveOb.x + moveOb.width/2 > right)
moveOb.x = right - moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = -1;
if (moveOb.x - moveOb.width/2 < left)
moveOb.x = left + moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = 1;
function bumpOb(p)
if (p)
p.removeEventListener(Event.ENTER_FRAME, updateOb);
if (p.parent)
removeChild(p);
score--;
function restart()
if(contains(player))
removeChild(player);
for (var i:int = 0; i < numOb; i++)
if (contains(obstacles[i]) && obstacles[i] != null)
removeChild(obstacles[i]);
obstacles[i] = null;
// returns a new array that consists of a range of elements from the original array,
// without modifying the original array
obstacles.slice(0);
function controls()
if (rightKeyDown)
velX += 3;
player.scaleX = 1;
if (leftKeyDown)
velX -= 3;
player.scaleX = -1;
function isJumping()
touchGround = false;
velY = -15;
//==================================================
// Sound control for background music
//==================================================
btnMute.addEventListener(MouseEvent.CLICK, mute);
function mute(e:MouseEvent):void
SoundMixer.soundTransform = new SoundTransform(0);
btnMute.removeEventListener(MouseEvent.CLICK, mute);
btnMute.addEventListener(MouseEvent.CLICK, unmute);
function unmute(e:MouseEvent):void
SoundMixer.soundTransform = new SoundTransform(1);
btnMute.removeEventListener(MouseEvent.CLICK, unmute);
btnMute.addEventListener(MouseEvent.CLICK, mute);
【问题讨论】:
欢迎来到 ***!作为提示,不要仅仅发布所有代码,而是解释您实际尝试过的内容以及您认为问题所在。期望人们筛选您的整个代码,除了错误消息之外没有任何解释,这是一个很大的时间承诺。 最有可能的是,您正在拼接您的数组,但仍有一个 for 循环试图遍历不再存在的项目。如果迭代一个数组,最好使用数组的长度属性,而不是一个固定的数字——所以不要i < numOb;
做 i < obstacles.length;
你确定 obstacels[i] 总是不为空吗?
AS3 error #1009的可能重复
@BadFeelingAboutThis 表示感谢。我调试了电影,问题似乎出在“move(moveOb)”方法中的moveOb.x -= moveOb.vx;
和“updateOb”中的move(ob);
。它告诉我move(ob);
是未定义的,但我不知道为什么它是未定义的......我尝试了这里给出的所有建议,但不幸的是它们都没有奏效:(
【参考方案1】:
我在为动画创建交互式元素时遇到了同样的问题。检查交互式对象在哪一层。当对象与位于上层的某物重叠时,也会发生类似的错误。
【讨论】:
【参考方案2】:你可以试试...
1) 你的 Npc
是一个类/库对象,对吧?
给出源 MC/Sprite、moveOb
或 p
的实例名称。
2) 或者尝试...使用函数参数 (这是一种更好的编码风格) :
(2a) 既然你说..
var ob:Npc = obstacles[i];
move(ob);
ps: 为什么不简化(没有var
)为:move( obstacles[i] );
...?
(2b) 你的移动函数应该指定一个数据类型和你的参数名...
//# applies basic velocity to enemies
//# Wrong...
//function move(moveOb)
//# Better...
function move( moveOb : Npc )
//# Aso fix as...
function bumpOb(p : Npc )
通过使用函数参数,您现在可以为 (函数的) 输入参数赋予唯一的名称,但保持引用相同(或兼容的)数据类型。
告诉我进展如何。
【讨论】:
嗨,是的,Npc 是一个库对象。我已根据您的建议更新了我的代码,但仍然遇到相同的错误。不过,我感谢您的帮助和您提供的一些有用的提示。谢谢! :)【参考方案3】:障碍物数组中间可能有空元素。如果添加条件为 null 则继续呢?
function updateOb(e:Event):void
// make obstacles move
for (var i:Number = 0; i < obstacles.length; i++)
var ob:Npc = obstacles[i];
if(!ob) continue;
move(ob);
if (player.hitTestObject(ob))
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y -
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y -
player.height/2, true))*/
bumpOb(obstacles[i]);
scoretext.text = String(score);
if (score == 0)
restart();
gotoAndStop("gameOverLost");
【讨论】:
以上是关于Actionscript 3 错误 1009:无法访问空对象引用的属性或方法的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 PHP 文件中调用方法时出现 ActionScript 错误
Flex / ActionScript3 - 对象属性/变量 null
TypeError:错误 #1009:无法访问空对象的属性或方法