我正在编写代码让敌人检测到与玩家的碰撞。在我的敌人课程中,我有以下内容: import flash.display.MovieClip; import flash.events.Event;
public class Enemy extends MovieClip {
var Player: MovieClip;
public function Enemy() {
this.addEventListener(Event.ENTER_FRAME, EnemyUpdate);
}
function setPlayer(_Player: MovieClip) {
Player = _Player;
}
function EnemyUpdate(_event: Event) {
var enemyHit: Boolean = this.hitTestObject(Player.Character.Legs);
if (enemyHit) {
trace("OUCH!!");
}
}
}
在我的Main Class中,我尝试使用以下命令将Player MovieClip发送到Enemy Class脚本:
public function Main() {
enemy.setPlayer(player);
}
MovieClip敌人附有敌人脚本。当我运行程序时,Player变量为null。如何让播放器识别播放器MovieClip?
答案 0 :(得分:0)
这是因为您在实例化敌人后设置了播放器。在Enemy构造函数中,您有EnterFrame侦听器。要修复错误,请更改代码,如下所示:
public function Enemy() {
// empty constructor, you can remove it if there is no other logic in it
}
function setPlayer(_Player: MovieClip) {
Player = _Player;
// the Player variable is not null anymore.
this.addEventListener(Event.ENTER_FRAME, EnemyUpdate);
}