Skip to content


return class; = [type Function] ?

[Edit: sorry about all the italics and bolds. i really need to get some sort of style sheet for actionscript code]
[Edit 2: and i got one. have to do some color changes. IG: Syntax Hiliter]
i found something i don’t quite understand today (not that much of a shock) when trying to make a CharacterManager class to control the registration and reference of classes extending a Character class. In short, when i make a new character that a user can use, i just want to create a new class that extends the Character class, go into my main class register it and drop the new character movieclip into the library, brush off my hands give a little grunt and call it a day.
Now i found this odd, but technically i know very little, so i was hoping someone out there might be able to explain it to me.
The way i want this to work is when i register a new character, i say something like this:

__characterManager.registerCharacterClass(MyNewCharacterClass, “class name”);

- where __characterManager is the singleton

….(in Character Manager)
[as]
public function registerCharacterClass(classToAdd, classNameStr)
{
__classList[ classNameStr ] = classToAdd;
}
[/as]

- and then when i want to retrive that class when i create a new character i say:

(1)
[as]
Application.addCharacter(”class name”, “symbol linkage id”, “instance name”);
[/as]
(2)
[as]
function addCharacter(classNameStr, clipID, name)
{

var characterClass:Character =

characterManager.getCharacterClassByName(classNameStr);

var character:Character = characterClass.create(clipID, name);
}
[/as]
- what i had in the CharacterManager class before i found the reason why i’m asking this question is this:
[as]
public function getCharacterClassByName(classNameStr):Character
{
return __classList[ classNameStr ];
}
[/as]

- but that always returned the [type Function]- never a reference to the class i was trying to get.
So i peeked into the CommandTemplate class within the ariaware package, and noticed how they did it.
Using this:

return new __classList[ classNameStr]();

Now this makes sense to me, since when you create an instance of a class you use new most of the time. But i guess i’m wondering why it would return [type Function] before, and not even [object Object], or something else. and if this is always true, can i not create a Singleton from here, or should i make a CharacterSingletonManager class and return __classList[ classNameStr].getInstance(); ?
The answer is probably simple and can be summed up in one line, and here i write a novel out of a question.. but oh well.

Posted in Flash.


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Ash says

    You’re actually being passed the constructor function inside your class, which in fact is all you’re using when you’re making a new instance of your class.

  2. todd anderson says

    Ahhh. that makes perfect sense. thanks ash. and you kept it to one line :)



Some HTML is OK

or, reply to this post via trackback.