package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
public class supportForce extends MovieClip
{
public function supportForce()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
}
private function onUp(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
}
GreenLight.x -= defaultSpeed;
}
}
}
所以这里的抛射物是Projectile1,它在keyCheck函数中被声明为GreenLight。所以在同一个函数(ENTER.FRAME函数)中,GreenLight应该向左移动。这不会发生,也会出现错误,说明TypeError:错误#1009:无法访问空对象引用的属性或方法。 在supportForce / keyCheck()出现。提前谢谢。
答案 0 :(得分:0)
在你的keyCheck函数中,如果_keyDownStatus [90]为假,GreenLight将为null
所以把GreenLight作为一个类变量
private var lights:Array = [];
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
for each (var l:Projectile1 in lights)
{
l.x -= defaultSpeed;
}
lights.push(GreenLight);
}
else
{
//remove all Projectile1s in lights from stage and clear lights if you want
}
}
答案 1 :(得分:0)
你在if条件范围内创建GreenLight
,如果keyCode不是90,则Projectile1
的对象未创建,GreenLight.x -= defaultSpeed;
那里GreenLight为空