Reputation: 13
To be clear, I want to check a class, not an instance of that class.
public function changeScene(newScene:Class):void
{
if(newScene isExtending Scene)
//...
}
It's a variable of type Class.
Edit: More details. What the function does (simplified):
public function changeScene(newScene:Class):void
{
currentScene.finish(); //Finish the scene that is about to change
//Check if the new scene don't exist prior this point
if (!scenes[newScene]) //Look in dictionary
addScene(newScene); //Create if first time accessing it
scenes[newScene].caller = currentScene["constructor"];
currentScene = scenes[newScene];
currentScene.start();
}
This question doesn't work for me because I don't create new instances all the time, I reused them most of the time. The instances are stored in a dictionary using the class as the key.
Upvotes: 1
Views: 144
Reputation: 5255
The other question doesn't work for me because I don't create new instances all the time, I reused them most of the time. The instances are stored in a dictionary using the class as the key.
I beg to differ.
The factory pattern encapsulates the entire creation of the object of a certain class. That also includes limiting how often the class is instantiated.
If you only want the factory to ever produce one object, that's possible. It will turn into a Singleton
.
Upvotes: 0
Reputation: 14406
Here is the only way I can think of to do this without instantiating objects:
You use flash.utils.getQualifiedSuperclassName
the get the super class of a class. Since that returns a string, you have to then use flash.utils.getDefinitionByName
to get the actual class reference.
So you could write a function that walks up the inheritance until it either finds a match, or reaches Object
(the base of everything).
import flash.utils.getQualifiedSuperclassName;
import flash.utils.getQualifiedClassName;
import flash.utils.getDefinitionByName;
function extendsClass(cls:Class, base:Class):Boolean {
while(cls != null && cls != Object){
if(cls == base) return true;
cls = getDefinitionByName(getQualifiedSuperclassName(cls)) as Class; //move on the next super class
}
return false;
}
trace(extendsClass(MovieClip,Sprite)); //true
trace(extendsClass(MovieClip,Stage)); //false
Upvotes: 1