To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 03-05-2010, 04:56 PM   #1
ziriguidum
Member
 
Join Date: Jul 2009
Location: Florianópolis
Posts: 79
Instantiate a Class picked from a Class Array

I would like to change this:

Code:
obj = new _objectClass();
into something like this:

Code:
var objClass:Class = _objectClassArray[range(0, _objectClassArray.length)] as Class;
obj = new objClass();

trace (_objectClassArray[range(0, _objectClassArray.length)]) // returns [class MyClip] or [class MyClip2] and so on..
Actually this last piece of code throws the following error:

TypeError: Error #1007: Instantiation attempted on a non-constructor.


Full explanation:

This code goes inside an ObjectPool from HYPE Framework of Joshua Davis.

I´ve created a "MultipleObjectPool" and changed some little details , that is: the constructor, instead of receiving a Class as a parameter, receives then a Class Array, so each time it should create an object (via the request() function) of a Class that is picked randomically from the Class Array.

Is that possible ?

Thanks in advance!

Last edited by ziriguidum; 03-05-2010 at 05:11 PM. Reason: add stuff, clarify
ziriguidum is offline   Reply With Quote
Old 03-05-2010, 05:38 PM   #2
5TonsOfFlax
Will moderate for beer
 
Join Date: Apr 2007
Location: Austin, TX
Posts: 5,399
Are you sure that the objects in your class array are Classes? Does range return a single random integer in the specified range?

If both of those are true, it should work.

I suspect you have something other than classes in your array (such as String), causing objClass to be null.
5TonsOfFlax is offline   Reply With Quote
Old 03-05-2010, 08:18 PM   #3
cancerinform
Mod
 
Join Date: Mar 2002
Location: press the picture...
Posts: 12,744
This example will give you the error you got, because Green is a string:

var myArray:Array = new Array("Green");
var objClass:Class = myArray[0] as Class;
var objisplayObjectContainer = new objClass() as DisplayObjectContainer;
addChild(obj);

Changing the first line will correct the mistake:
var myArray:Array = new Array(Green);

Probably the array members are all strings.
__________________

- The right of the People to create Flash movies shall not be infringed. -
| www.Flashscript.biz | Help a little girl, Ana, who has cancer. | Flashscript Biz Classes/Components | The new Event design pattern | Clothing for Haiti |
cancerinform is offline   Reply With Quote
Old 03-05-2010, 08:25 PM   #4
ziriguidum
Member
 
Join Date: Jul 2009
Location: Florianópolis
Posts: 79
solved! now, how to decently extend a class?

Hi, I solved it this way:

obj = new _objectClassArray[range(0, _objectClassArray.length - 1)];

simple

I wonder now how could I extend the class ObjectPool
Cause I've just copied the whole code into a new .as file I called "MultipleObjectPool" and changed very little stuff on it.
But I know that's definitely not good practice.

So, these are the changes I've made in the "MultipleObjectPool" class:

1st - I added the "_objectClassArray" instance variable (not sure if it's called this way) , and removed the "_objectClass" instance variable since I didn't need it anymore.

2nd - The constructor changed:

Code:
		public function ObjectPool(objectClass:Class, max:uint) {
			_objectClass = objectClass;
becomes

Code:
		public function ObjectPool(objectClassArray:Array, max:uint) {
			_objectClassArray = objectClassArray;
3rd - in function request():

Code:
obj = new _objectClass();
becomes

Code:
obj = new _objectClassArray[range(0, _objectClassArray.length - 1)];
I'm not good enough in OOP to know how to do this... so please help! Then I can share it with you!

Thanks!
ziriguidum is offline   Reply With Quote
Old 03-05-2010, 08:31 PM   #5
ziriguidum
Member
 
Join Date: Jul 2009
Location: Florianópolis
Posts: 79
hey, thanks for answering,

The objects in the array ware actually Classes.

i 'm still don't know why this didn't work:

Code:
var objClass:Class = _objectClassArray[range(0, _objectClassArray.length)] as Class;
obj = new objClass();
but the important thing is that this works:

Code:
obj = new _objectClassArray[range(0, _objectClassArray.length - 1)];
please read my previous post!

thanks!
ziriguidum is offline   Reply With Quote
Reply

Tags
array, class, hype, object, pool

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:53 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.