00:00
00:00
Newgrounds Background Image Theme

Wetrinas just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

class validation

454 Views | 10 Replies
New Topic Respond to this Topic

class validation 2014-04-20 09:16:52


Hey guys.

I have a variable:

var myClass:Class;

how can i check if

myClass

extends an other specific parent class?
something like that:

if(myClass extends parentClass) {...};

I found a workaround, but I don't like it, because it creates a new useless object:

if(new myClass() is parentClass) {...};

tanks!

Response to class validation 2014-04-20 10:11:35


how can i check if myClass extends an other specific parent class?
something like that:

if(myClass extends parentClass) {...};

why would you ever need this? class extensions are not created at runtime, if you need something like this, you're probably doing something wrong.

take a look at describeType

but explain why you need to use this, because you shouldn't have to.

Response to class validation 2014-04-20 10:54:27


I have different classes (units in the game), which implements the same Interface.
However. The classes needs different parameters for the constructor.

So if I can check myClass which class it belongs to, I can change dynamicly which unit is created...
mabe something like this:

private var myClass:Class;

private function create unit():void{
   if (myClass == ParentClass1) new myClass(paramter1, paramter2);
   else if (myClass == ParentClass2) new myClass(someOtherParameter);
   else if ...
}

Response to class validation 2014-04-20 11:01:19


At 4/20/14 10:54 AM, Stoerwelle wrote: I have different classes (units in the game), which implements the same Interface.
However. The classes needs different parameters for the constructor.

That sounds like poor design, but you can use ApplicationDomain.getDefinition() with a string id rather than a class object.

Response to class validation 2014-04-20 13:10:58


There's a lot of different ways to do things like this. But I have no idea what you're doing, so I can only talk generally. The common options are:

1. don't lump the creation of various different types under one umbrella function. call one one you need one and another when you should. Maybe instead of setting a class, set the instance directly.

2. make all of your class types have no constructor arguments, but give them all a function:

public function init(args:Object):void{}

then in create():

var obj:IShape = new myClass();
if(obj is Box) obj.init({width:10, height:15});
if(obj is Circle) obj.init({radius:10});
return obj;

Response to class validation 2014-04-20 13:12:36


getQualifiedClassName(obj)

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to class validation 2014-04-20 13:16:34


At 4/20/14 01:12 PM, egg82 wrote: getQualifiedClassName(obj)

Doesn't that need to be called on an instance rather than a type. That doesn't really tell you if it extends another specific class. you can use getQualifiedSuperClass() but that only works if its a direct extension, rather than something derived from a derivation, and so on...

Response to class validation 2014-04-20 14:35:03


factory pattern

Response to class validation 2014-04-20 20:23:37


At 4/20/14 02:35 PM, milchreis wrote: factory pattern

++++++++1

var unit:Unit = UnitFactory.create().setType(Unit.BUILDING_TYPE).setHealth(2000).instance();

Personally, a component-entity system would be my personal choice. Literally cut 50% (or more) of my work once the brain of my game was made.

I might post a tutorial here if I have the time.

Response to class validation 2014-04-20 23:43:30


You've peaked my interest, I found this

Chapter 2

Response to class validation 2014-04-21 00:33:17


At 4/20/14 11:43 PM, GeoKureli wrote: You've peaked my interest, I found this

Chapter 2

My example is actually an instance builder. I prefer leaving factory instance creation to the likes of only Java (and similar) projects where abstract classes can actually be used unlike how its done using AS3 with interfaces.