我刚开始在 AS3 中进行一些编程,但仍有一些我不清楚的地方。
我有 3 个不同文件的项目twinrunner.fla
:twinrunner.as
和wall.as
.
我让墙从右到左在屏幕上滚动。当它击中玩家时,我想让它识别它。我试图用hitTestObject
. 但不幸的是,我只得到错误。
twinrunner.as
public class TwinRunner extends MovieClip
{
private var _timer:Timer;
public function TwinRunner()
{
//Timer initialize
_timer = new Timer(500, 1);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onUpdateTime);
_timer.start();
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
//Add event listeners
button.addEventListener(MouseEvent.CLICK, onGuessButtonClick);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
//Remove the onEnterFrame event if
//this object is removed from the stage
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onGuessButtonClick(event:Event):void
{
addChild(new Wall());
}
private function onEnterFrame(event:Event):void
{
}
private function onUpdateTime(event:Event):void
{
addChild(new Wall());
_timer.reset();
_timer.start();
}
}
墙.as
public class Wall extends MovieClip
{
private var _vx:int;
public function Wall()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
x = stage.width;
y = 300;
_vx = -5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage)
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
trace("wall removed");
}
private function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT)
{
_vx = -20;
}
}
private function onEnterFrame(event:Event):void
{
x += _vx;
if(this.hitTestObject(Player))
{
trace("player hit");
}
if (x + width / 2 < 0)
{
parent.removeChild(this);
}
}
}
每次我尝试编译它时,我都会收到错误 1046:
找不到类型或不是编译常量:播放器
我想这是一个非常愚蠢的错误,但我完全迷路了,不知道在哪里搜索。