我會說這是一個很好的方向。在您的AI類中,創建一個addedToStage監聽器,並在該處理程序中創建一個受保護或公共的ENTER_FRAME處理程序,如果您對不同字符類型的需求略有不同,則可以覆蓋其部分行爲。
public class CharacterBase extends Sprite {
public function CharacterBase():void {
this.addEventListener(Event.ADDED_TO_STAGE,addedToStage,false,0,true);
this.removeEventListener(Event.REMOVED_FROM_STAGE,removedFromStage,false,0,true);
}
private function addedToStage(e:Event):void {
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler, false,0,true);
}
private function removedFromStage(e:Event):void {
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
protected function enterFrameHandler(e:Event):void {
//do your AI moving around logic
walk();
}
protected function walk():void {
this.x += 2; //the default walk behavior
}
}
一個字符覆蓋默認動作:
public class Character1 extends CharacterBase {
public function Character1():void {
super();
}
override protected function walk():void {
this.x += 5; //this character needs to be faster than default
}
}
你可以有很多ENTER_FRAME監聽器/處理器,只要你願意 – BadFeelingAboutThis