PDA

View Full Version : Assigning a class movieClip an additional class



kingofnukes
January 31st, 2008, 07:01 AM
normally if i wanted to add a class movieclip i would do something like:

var newChar:specificChar = new specificChar();
this.addChild(newChar);

Now is there a way where I could declare newChar to become a player or AI by adding another class to it.

kingofnukes
January 31st, 2008, 03:35 PM
bump

Dazzer
February 1st, 2008, 12:06 AM
public class playerChar extends specificChar

public class nonPlayerChar extends specificChar

kingofnukes
February 1st, 2008, 09:01 PM
yeah, but how can i say, spawn a character AS a player or AI. That way I don't need to make a player and AI class for each character since I plan to make a generic AI that can work equally with each AI.

Dazzer
February 2nd, 2008, 01:41 AM
All the more you should create separate classes for each.

Trust me. More classes = Win (in most cases).

But if you're adamant, you can store a variable in your specificChar class. And pass that in through the constructor.

hobbbz
February 3rd, 2008, 02:17 AM
implements

playerClass implements characterClass extends MovieClip

AIClass implements characterClass extends MovieClip

implements means that the subclass is required to implement all the methods and variables of the class it's 'implementing'. Extends of course means that the class is a subtype of a specific class. In this way you can guarantee that if you define the method moveLeft() in characterClass, that it must be made available in playerClass and AIClass.

kingofnukes
February 3rd, 2008, 06:54 PM
hobbbz, could u give me a real world example? lets say I have 2 characters charlee and leeroy which both have a class that extends a class called character. How would I make one a player and one AI, determining which during spawn?

ex: createCharacter("AI", "Charlee");

Dazzer
February 3rd, 2008, 07:33 PM
You can subclass.
So you have SpecificCharacter
Then you can have PlayerCharacter, and AICharacter, that are both subclasses of SpecificCharacter. Then you can do this



var player:SpecificCharacter = new PlayerCharacter("Name");

spasticfantastic
February 4th, 2008, 06:39 AM
You can use a decorator pattern for this, which is essentially a good use of interfaces. See http://en.wikipedia.org/wiki/Decorator_pattern