我会说这是一个很好的方向。在您的 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
}
}