我得到錯誤:null對象引用的方法。我很困惑,不知道什麼是真正的原因。
所以我在舞臺上有一個「player_mc」實例的播放器movieClip,它將通過我的Document類傳遞到Player類中。
Player.as從另一個類調用一個類的movieclip,錯誤#1009
import flash.display.*;
import flash.events.*;
public class Player extends MovieClip
{
public var myPlayer:MovieClip;
public function Player(player:MovieClip)
{
myPlayer = player;
addEventListener(Event.ENTER_FRAME,on_enter);
}
Document.as
import flash.display.*;
import Components.Player.Player;
public class Game_Main extends MovieClip
{
public var player:Player;
public function Game_Main()
{
player = new Player(player_mc);
}
}
現在在這裏,我認爲是問題的來源。我有一個基地級敵人舞臺上的green_enemy movieclip。
Enemy.as
import flash.display.MovieClip;
import Components.Player.Player;
import flash.events.Event;
public class Enemy extends MovieClip
{
var theplayer:Player;
public function Enemy()
{
this.addEventListener(Event.ENTER_FRAME,on_enter);
}
public function on_enter(e:Event):void
{
if (this.hitTestObject(theplayer.myPlayer)) //calls player_mc from Player class
{
trace("hi");
}
}
}
我喜歡對敵人的功能做的就是當敵人「player_mc」(這是在舞臺上),它會做一些碰撞。也許我的代碼是錯誤的。
任何幫助/技巧將不勝感激。謝謝!
我知道這很愚蠢,但你能告訴我你是什麼意思嗎?我最近剛剛進展到as3,所以很抱歉沒有成爲愚蠢的人。 –
沒有道歉。更改'var theplayer:Player;'到'public var theplayer:Player;'就像John Dodson說的那樣。別忘了定義green_enemy.theplayer = yourPlayer。另見我的更新回答 – ZuzEL
好吧!感謝你們。 –