PDA

View Full Version : passing a class type and declare it



xEnOn
August 2nd, 2007, 03:50 AM
i am trying to pass a class type into a function which then declares it to an array this way:

i have this class which i am using the add movieclips form the library.


package {
import flash.display.MovieClip;

public class TestClass extends MovieClip {

private var allTypes:Array;

public function TestClass {
allTypes = new Array();
}

public function addType(i:Number, type:*) {
//then i wanna declare the type of the class that is put into this method
allTypes[i] = new type("name", 10, 50);
addChild(allTypes[i]);
}
}
}


and then on my main timeline, i have this document class, which will have an instance of the TestClass, and add in types which i want to add onto the stage. i wish to pass in the type of the class name of the exported movieclips which i have in my libraries.


package {
import flash.display.MovieClip;
import TestClass;

public function MainMovie extends MovieClip {

var TestClass:TestClass;

public function MainMovie() {
TestClass = new TestClass();
//now i want to add the Type of the class into the paramaters
//these types are class names of the movieclips in the library that are exported for actionscript in linkage
TestClass.addType(0, Plane);
TestClass.addType(1, Bird);
}
}
}


however, this doesn't seem to work. any idea how should the syntax or the right way be for doing this?
thanks. :)

Sirisian
August 2nd, 2007, 04:08 AM
lol, it looks like you are trying to do templates... (C++ and java concept)

Most people make an ObjectFactory
http://en.wikipedia.org/wiki/Factory_object

aka:

package {
public final class ObjectFactory {
public static function Create(typeEnumeration:uint):BaseClassNameOfTheObj ects{
switch(typeEnumeration){
case ObjectTypes.PLANE:
return new Plane();
case ObjectTypes.BIRD:
return new Bird();
}
}
}
}enumerations are easy: click here (http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000065.html)
So an ObjectTypes enumeration might be used as I am showing.
On a side note using push command might be a lot easier.
var objectInstance:BaceClassName = ObjectFactory.Create(ObjectTypes.PLANE);
//set data
arrayFoo.push(objectInstance);

Then again I might be off on my idea... why don't you just do:
addChild(new Plane("name", 10, 50));
addChild(new Bird("name", 10, 50));
instead of:
TestClass.addType(0, Plane);
TestClass.addType(1, Bird);

You are going to have to explain your plans more I think.

xEnOn
August 2nd, 2007, 04:30 AM
thanks sirisian for the response. you are right! i am trying to work out a template. :)

i am trying to work on a template that allows me to add in different types of movieclips in the library and then show it on the stage. and the reason why i am not doing the hardcoding way by typing new Plane("name", 10, 50), is because the class name that I pass in may be formed using a string.

so for example, in a case where


for(var i:Number = 0; i<5; i++) {
//i could do this:
TestClass.addType(i, "Plane"+i)
}


and in my library, i could have 5 different kind of planes, which i could use. there could be in some situations where i don't know which plane to use, as it is according to the user's input.

the other situation is as i mentioned, this would be a template, which works like an API, i don't know what other kind of classes i may add in the future. i may have planes today and maybe plants tomorrow. and so I thought have a way which allows me to addChild of dynamic movieclip classes would be kind of easier for me in the future.

xEnOn
August 2nd, 2007, 04:43 AM
by the way, i also kind of not sure some of the parts in the enumeration code you had. i am having the questions as comments in the code.



package {
public final class ObjectFactory {
public static function Create(typeEnumeration:uint):BaseClassNameOfTheObj ects{
//isn't unit a number? then what should i pass in this param?
switch(typeEnumeration){
//if I am passing in a number in the typeEnumberation, then how would the cases below
//determine if the typeEnumberation is a Plane or a Bird?
case ObjectTypes.PLANE:
//where do these ObjectTypes come from? do i have to declare it as a static class variable in the ObjectFactory class?
return new Plane();
case ObjectTypes.BIRD:
return new Bird();
}
}
}
}