我几天来一直试图在我的玩家类和一些“建筑物”之间添加一个简单的hitTest而没有成功。 我已经尝试将hitTest添加到播放器类&什么时候没工作,建筑类。我已经尝试了两种代码的变体也没有运气。我已经阅读了几天的教程和无论我尝试什么,似乎没有任何工作 - 所以,我必须做一些如此简单的错误,以至于常识只能让我失望。
我有三个可以/应该受hitTest影响的类:播放器类,构建类,& /或objectPlacer类。 对于这个问题,我让玩家类保持不变,以为稍后我会根据建筑类的hitTest为它的运动添加一个布尔值。 ObjectPlacer类在start时实例化。它的功能是实例化建筑物,植物群,动物群等等......我在思考这个“建筑”类是我希望应用hitTest的地方时保持不变。
这是我的建筑代码:
package com.gamecherry.gunslinger
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class BuildingsLeft03 extends MovieClip
{
private var speed:Number = 1;
private var startX:Number;
private var startY:Number;
private var stageRef:Stage;
private var target:Cowboy;
public function BuildingsLeft03(stageRef:Stage, startX:Number, startY:Number, target:Cowboy)
{
this.stageRef = stageRef;
this.target = target;
x = startX;
y = startY;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
y+= speed;
if (y > 544)
removeSelf();
if (hitTestObject(target.hit))
{
trace("hitTestObject");
}
}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
这都会导致以下输出错误: TypeError:错误#1009:无法访问空对象引用的属性或方法。 在com.gamecherry.gunslinger :: BuildingsLeft03 / loop()
我究竟做错了什么?我已经尝试了我能想到的一切(我是编码新手)&如果我没有收到此错误,那么我会收到其他错误。
我希望通过不发布我的所有代码来尊重我所要求的帮助(这对你们所有人来说都很好,并且无需阅读我公认的业余代码的三页就可以提供帮助)但是,也许在我的无能为力上发光更有助于缩小问题范围。
这是我的播放器类代码:
package com.gamecherry.gunslinger
{
import flash.display.MovieClip;
import flash.display.Stage;
import com.senocular.utils.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Cowboy extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;
private var speed:Number = 0.5;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.93;
private var maxSpeed:Number = 2;
private var fireTimer:Timer;
private var canFire:Boolean = true;
public function Cowboy(stageRef:Stage)
{
this.stageRef = stageRef;
key = new KeyObject(stageRef);
fireTimer = new Timer(300, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
if (key.isDown(Keyboard.SPACE))
fireBullet();
if (key.isDown(Keyboard.LEFT))
vx -= speed;
else if (key.isDown(Keyboard.RIGHT))
vx += speed;
else
vx *= friction;
if (key.isDown(Keyboard.UP))
vy -= speed;
else if (key.isDown(Keyboard.DOWN))
vy += speed;
else
vy *= friction;
if (key.isDown(Keyboard.SPACE) && key.isDown(Keyboard.LEFT))
fireBullet();
x += vx;
y += vy;
//rotation = vx * 12;
if (vx > maxSpeed)
vx = maxSpeed;
else if (vx < -maxSpeed)
vx = -maxSpeed;
if (vy > maxSpeed)
vy = maxSpeed;
else if (vy < -maxSpeed)
vy = -maxSpeed;
//scaleX = (maxSpeed - Math.abs(vx)) / (maxSpeed * 4) + 0.75;
//If Cowboy touches edge of screen - he bounces back
if (x > stageRef.stageWidth)
{
x = stageRef.stageWidth;
vx = -vx;
}
else if (x < 0)
{
x = 0;
vx = -vx
}
if (y > stageRef.stageHeight)
{
x = stageRef.stageHeight;
vy = -vy;
}
else if (y < 0)
{
y = 0;
vy = -vy
}
}
private function fireBullet() : void
{
if (canFire)
{
stageRef.addChild(new PlayerBullet(stageRef, x - 10 + vx, y - 10, vx, vy));
canFire = false;
fireTimer.start();
}
}
private function fireTimerHandler(e: TimerEvent) : void
{
canFire = true;
}
}
}
这是我的ObjectPlacer代码:
package com.gamecherry.gunslinger
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class ObjectPlacer extends MovieClip
{
private var Build01Timer:Timer;
private var Build02Timer:Timer;
private var CactiSet01Timer:Timer;
private var build01Place:Boolean = false;// if true - addChild starts at first frame
private var build02Place:Boolean = false;
private var cactiSet01Place:Boolean = false;
private var stageRef:Stage;
private var target:Cowboy;
public function ObjectPlacer(stageRef:Stage)
{
this.stageRef = stageRef;
var Build01Timer = new Timer(1000, 1);
var Build02Timer = new Timer(25000, 1);
var CactiSet01Timer = new Timer(0, 1);
Build01Timer.addEventListener(TimerEvent.TIMER, build01TimerHandler, false, 0, true);
Build02Timer.addEventListener(TimerEvent.TIMER, build02TimerHandler, false, 0, true);
CactiSet01Timer.addEventListener(TimerEvent.TIMER, cactiSet01TimerHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
Build01Timer.start();
Build02Timer.start();
CactiSet01Timer.start();
}
public function loop(e:Event): void
{
BuildSet01();
BuildSet02();
CactiSet01();
}
private function BuildSet01(): void
{
if (build01Place)
{
var Buildings01Right:BuildingsLeft = new BuildingsLeft(stage, 720, -624);
Buildings01Right.scaleX = -1;
stageRef.addChildAt((Buildings01Right), 2);
var target:Cowboy = new Cowboy(stage);
var Buildings03Left:BuildingsLeft03 = new BuildingsLeft03(stage, 0, -720, target);
stageRef.addChildAt((Buildings03Left), 2);
build01Place = false;
}
}
private function BuildSet02(): void
{
if (build02Place)
{
var Buildings04Left:BuildingsLeft04 = new BuildingsLeft04(stage, 0, -800);
stageRef.addChildAt((Buildings04Left), 2);
build02Place = false;
}
}
private function CactiSet01(): void
{
if (cactiSet01Place)
{
var cactus01:GScactus01 = new GScactus01(stage, 0, 300);
stageRef.addChildAt((cactus01), 2);
var cactus01b:GScactus01 = new GScactus01(stage, 190, 50);
stageRef.addChildAt((cactus01b), 2);
var cactus01c:GScactus01 = new GScactus01(stage, 660, -40);
stageRef.addChildAt((cactus01c), 2);
cactus01c.scaleX = -1;
var cactus01d:GScactus01 = new GScactus01(stage, 710, 340);
stageRef.addChildAt((cactus01d), 2);
cactus01d.scaleX = -1;
var cactus02:GScactus02 = new GScactus02(stage, 420, -50);
stageRef.addChildAt((cactus02), 2);
var cactus02b:GScactus02 = new GScactus02(stage, 600, 180);
stageRef.addChildAt((cactus02b), 2);
var cactus02c:GScactus02 = new GScactus02(stage, 300, 240);
stageRef.addChildAt((cactus02c), 2);
var cactus03:GScactus03 = new GScactus03(stage, 540, 100);
stageRef.addChildAt((cactus03), 2);
var cactus03b:GScactus03 = new GScactus03(stage, 30, 5);
stageRef.addChildAt((cactus03b), 2);
cactus03b.scaleX = -1;
var cactus03c:GScactus03 = new GScactus03(stage, 520, 420);
stageRef.addChildAt((cactus03c), 2);
cactiSet01Place = false;
}
}
private function build01TimerHandler(e: TimerEvent) : void
{
build01Place = true;
}
private function build02TimerHandler(e: TimerEvent) : void
{
build02Place = true;
}
private function cactiSet01TimerHandler(e: TimerEvent) : void
{
cactiSet01Place = true;
}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
显然有更多的actionscript文件可以与这些文件进行交互,但这些文件应该是唯一受“hitTest”影响的文件
答案 0 :(得分:2)
关于错误1009,loop()方法包含
target.hit
引用,我看到空指针的唯一潜力。
我看到了一个真正的可能性:您正在构建一个BuildingsLeft03的某个外部类中将null传递给BuildingsLeft03的构造函数,以获取目标属性。
BuildingsLeft03(stage, 0, 0 null) //the null
这会使
target == null
target.hit //ERROR 1009