Referencing functions defined by subclasses in Actionscript 3 -
i setting system manage cutscenes in game. however, have run tricky problem, , have hard time trying describe google. need access function flash tells me "undefined".
here's i'm have;
cutscene base class contains basic functions cutscenes
cutscene1 extends cutscene, , contains information individual cutscene. such information includes functions , variables. later come cutscene2, cutscene3, of extend cutscene
cutscenehandler takes cutscene, determines cutscene's next step, , tells cutscene execute function defined step.
cutscenehandler accepts cutscene
so give handler newly instantiated cutscene1. handler says "hey, it's cutscene, everything's cool.". now, handler tells cutscene perform function defined in subclass. handler says "whoa, cutscene class has no function this!" , throws error. call possibly undefined function.
how can around this? issue how call function? i've included simplified code below.
public class cutscenehandler extends sprite { var activecutscene:cutscene public function cutscenehandler() { //empty constructor } public function begincutscene(cutscene:cutscene) { activecutscene = cutscene executestep(activecutscene.steps[0]) } public function executestep(step:object) { if (step.action) { activecutscene.action() } } } __
public class cutscene1 extends cutscene { public var steps = [ {action:somefunction,someimportantdetail:true}, {action:someotherfunction,someimportantdetail:false} ] public function cutscene1() { //empty constructor } public function somefunction() { trace ("hello!") } public function someotherfunction() { trace ("goodbye!") } }
this sounds job command pattern!
in simple terms, idea encapsulate specifics of each step in separate instances of classes implement interface single execute method. handler class can invoke steps via interface without knowledge of specific subclass of cutscene steps belong.
something following should moving in right direction.
the icommand interface:
public interface icommand { function execute():void } the concrete commands:
public class hellocommand implements icommand { public function execute():void { trace("hello"); } } public class goodbycommand implements icommand { public function execute():void { trace("goodbye"); } } the subclass of cutscene
public class cutscene1 extends cutscene { // declare steps variable in base class since // subclasses use public function cutscene1() { // define steps specific subclass steps = [ new hellocommand(), new goodbyecommand() ]; } } the handler class
// extend sprite if class needs displayed public class cutscenehandler { var activecutscene:cutscene public function cutscenehandler() { //empty constructor } public function begincutscene(cutscene:cutscene) { activecutscene = cutscene; // cast step icommand , execute (activecutscene.steps[0] icommand).execute(); } }
Comments
Post a Comment