只是想知道索引是否会影响对象的范围,因为我正在创建一个游戏,出于某种原因,我得到如下错误
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.objects::Torret/updateObject()
at com.objects::EngineApi/loop()
取决于我将物体放在舞台上的位置。我可以防止这种错误发生。我所要做的就是改变或删除以下代码。
eApi.setChildIndex(this, (eApi.numChildren - 1));
在我的舞台上有一个恒定的物体流进出,因此这段代码可以防止我的物体掉落在一个新物体下面。只有一个对象抛出了这个错误,那就是我的Turret类。另一类是我的船级。炮塔类可以参考船级,因此它可以射击它。
Turret类是唯一抛出此错误的类。下面是我的两个类的代码。是的,我知道我拼错了炮塔。致谢
package com.objects{
import flash.events.Event;
public class Torret extends gameObject{
public var currentAngle:Number;
protected var newAngle:Number;
public var shoot:Boolean = false;
public var autoRotate:Boolean = false;
public var updated:Boolean = false;
public var target:Avatar;
public var enemyLock:Boolean = false;
public var attackDelay:Number;
public var delay:Boolean = false;
public var wielder;
public var smokeDelay:Number = 500;
public function Torret():void
{
health = 1;
maxHealth = 1;
currentAngle = rotation;
newAngle = currentAngle;
lastTime = getTime();
}
public function Hit(dmg:Number = .01):void {
if(health > 0)
health -= dmg;
if(health < 0)
health = 0;
}
override public function updateObject():void
{
eApi.setChildIndex(this, (eApi.numChildren - 1));
if(health <= 0)
{
dead = true;
blowUp();
}
if(y > 949)//If boss doesnt work, this is why
{
garbage = true;
}
if(!dead)
{
if(wielder)
{
scaleX = wielder.scaleX;
scaleY = wielder.scaleY;
}
if(currentAngle != newAngle || autoRotate == true)
{
rotation += 3;
currentAngle = rotation;
updated = false
}
else
{
updated = true
}
if(shoot)
{
if((getTime() - lastTime) > attackDelay && delay == true)
{
var stingerlaser = new StingerLaser();
stingerlaser.laserDir = rotation;
stingerlaser.x = x;
stingerlaser.y = y;
eApi.addGameChild(stingerlaser);
lastTime = getTime();
}
}
if(enemyLock)
{
var dx = target.x - x;
var dy = target.y - y;
var angle = Math.atan2(dy,dx);
rotation = angle * 180/Math.PI;
}
}
}
protected function blowUp():void
{
if((getTime() - lastTime) > smokeDelay)
{
var smoke:MissileSmoke = new MissileSmoke();
smoke.x = x;
smoke.y = y;
smoke.dir = -1;
eApi.addGameChildAt(5,smoke);
eApi.setChildIndex(smoke, (eApi.numChildren - 4));
lastTime = getTime();
}
}
protected function degreesToRadians(degrees:Number):Number {
return degrees * Math.PI / 180;
}
protected function rotate(angle:Number):void
{
newAngle = angle;
}
}
}
以下是我的船类
package com.objects{
import flash.display.MovieClip
public class Avatar extends gameObject {
public var targets:Array;
public var delay:Number = 3000;
public var weapon:Number = 1;
public function Avatar():void
{
rotation = -90;
lastTime = getTime();
targets = new Array();
}
override public function Attack(dir:Number = -40):void
{
switch(weapon){
case 1:
var bullet1:Bullet = new Bullet();
bullet1.wielder = this;
bullet1.x = x + 35;
bullet1.y = y + 30;
bullet1.bulletDir = rotation;
eApi.addGameChild(bullet1);
var bullet2:Bullet = new Bullet();
bullet2.bulletDir = rotation;
bullet2.wielder = this;
bullet2.x = x - 35;
bullet2.y = y + 30;
eApi.addGameChild(bullet2);
break;
case 2:
if((getTime() - lastTime) > delay)
{
var missle = new Missile();
missle.x = x;
missle.y = y;
missle.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle);
lastTime = getTime();
}
break;
default:
}
}
public function Hit():void
{
trace("ouch");
}
override public function updateObject():void
{
eApi.setChildIndex(this, (eApi.numChildren - 1));
}
}
}
只是让大家知道,updateObject是我所有对象的循环。我有一个集中循环,在该循环中,它调用已放置在舞台上的对象数组。所有这些都包含方法updateObject。更新对象的状态。 addGameChild()是一个封装的addChild(),它不仅将对象添加到舞台,而且将其放在一个数组中,以便可以调用它的updateObject()方法。它还用于使垃圾收集更容易。
答案 0 :(得分:0)
首先,我认为使用addGameChildAt
是不必要的,因为您不需要额外的数组来存储对象以调用它们上的updateObject。
for(var i:int;i<numChildren;i++) { gameObject(getChildAt(i)).updateObject(); }
然后我建议用大写字母开始你的班级名称,即GameObject
,方法名称应以小写字母开头,即attack()
我建议你检查你的对象引用eApi是否存在(非null),并且在设置索引之前你的对象是eApi的一部分
if(eApi && eApi.contains(this)) { eApi.setChildIndex(this, (eApi.numChildren - 1)); }
我想知道你为什么还要使用setChildIndex
,因为我认为它有一个常量循环,它会调用当前updateObject
子对象的eApi
因此你不断移动对象其updateObject
方法目前被执行到顶部,这似乎毫无意义?