ActionScript 3 Subclass Reference Functions
I am setting up a system to manage cutscenes in my game. However, I am having a tricky problem and I am having a hard time trying to describe it on Google. I need to access a function that Flash tells me is "undefined".
Here's what I have;
Cutscene
- base class containing basic functions for all videos
Cutscene1
expands Cutscene
and contains information about a single video. This information includes functions and variables. Later will come Cutscene2,
Cutscene3
, all of which expandCutscene
CutsceneHandler
accepts Cutscene
, determines the next step, Cutscene
and tells Cutscene
to execute the function defined by the step.
CutsceneHandler
accepts only Cutscene
So we give this handler a new instance Cutscene1
. The handler says, "Hey, that Cutscene
's cool." But now the handler tells Cutscene
to execute the function defined only in its subclass. Handler says, "Whoa, the class Cutscene
has no such function!" and throws an error. Call the function undefined.
How can we get around this? Is this a problem with how we call this function? I've included some 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!")
}
}
source to share
It looks like a job for a template
Simply put, the idea would be to encapsulate the specifications of each step into separate instances of classes, which all implement the interface with a single method execute
. A handler class can invoke steps through this interface without any knowledge of the specific subclass the Cutscene
steps belong to.
Something like the following should get you moving in the right direction.
ICommand interface:
public interface ICommand {
function execute():void
}
Specific commands:
public class HelloCommand implements ICommand {
public function execute():void {
trace("hello");
}
}
public class GoodbyCommand implements ICommand {
public function execute():void {
trace("goodbye");
}
}
Cutscene subclass
public class Cutscene1 extends Cutscene {
// Declare steps variable in the base class since
// all subclasses will use it
public function Cutscene1() {
// Define the steps specific to this subclass
steps = [
new HelloCommand(),
new GoodbyeCommand()
];
}
}
Handler class
// Only extend Sprite if the class needs to be displayed
public class CutsceneHandler {
var activeCutscene:Cutscene
public function CutsceneHandler() {
//empty constructor
}
public function beginCutscene(cutscene:Cutscene) {
activeCutscene = cutscene;
// Cast the step as ICommand and execute it
(activeCutscene.steps[0] as ICommand).execute();
}
}
source to share