最干净的方法是创建一个基类。你可以在你的 .fla 文件旁边创建一个PlayerArmBase.asfile,让它看起来像这样:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class PlayerArmBase extends MovieClip {
public function PlayerArmBase(){
//don't do anything until this item has been added to stage/timeline
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false,0,true);
}
protected function addedToStage(e:Event):void {
//this is the equivalent of where timeline code runs
//put your code here for your arms
//for example:
this.addEventListener(Event.ENTER_FRAME, enterFrame, false, 0, true);
}
protected function enterFrame(e:Event):void {
//do something every frame tick like point to the mouse position
}
}
}
现在,你可以让你的双臂扩展这个基类。为此,请右键单击 flash pro 中的每个手臂,然后转到它们的属性。选中高级设置中的“Export For Actionscript”复选框,然后在“Base Class”字段中键入您的类的名称。
现在该类中的所有代码都将应用于两个 arm MovieClip。
或者,您可以将所有通用代码放在基类中,然后再创建 2 个类(每个分支一个),并在其中放置特定代码并让它们扩展基类。这与上图相同,只是在类字段中输入LeftArm,而不是基类字段。
package {
import flash.events.Event;
public class LeftArm extends PlayerArmBase {
public function LeftArm(){
}
//we can override the addedToStage function from the base class
override protected function addedToStage(e:Event):void {
super.addedToStage(e); //call the base class version of this function
//do stuff specific to the left arm
}
}
}
这样,您可以在自己的类中拥有特定于每个手臂的代码,但将所有通用代码放在一个位置。超类可以访问使用 public 或 protected 关键字声明的所有函数和变量。 protected 就像私有的,除了超类仍然可以访问它。 private 只能在你定义它的类中使用。