PDA

View Full Version : OOP class variables question



Dragy
January 29th, 2008, 04:49 AM
Hey :smirk:

Not too long ago I started using Flash CS3 and AS3. OOP is completely new for me, but I'm getting the hang of it with time. But I have a question -

I have a class which extends a movieclip, that class is a unit. In my game, there's a player unit and an enemy unit. I'm using the same class for both, the only difference is a boolean inside that tells if it's an enemy or a player unit, which affects what array of units in the above class they should be looping and checking for foes nearby.

To create a unit, what I do, and I figured is wrong, is after creating the unit in a create unit function I have, only then I set the boolean, from the outside, for instance:
unit = new unit_mc;
unit.isPlayer = true;

So 'in the long run' it plays fine, but when I create the class first, it already run loops and ifs regarding the isPlayer boolean, which wasn't yet defined, but only a line after.

So my question is, is there a way to define a boolean from the outside in the same time of creation? If not, how else can I solve this issue?
I thought about making a class that extends Unit, that only sets the boolean (using super.isPlayer, I believe?), but is it right to work that way?

Thanks a bunch!

burn_hh
January 29th, 2008, 06:22 AM
why don't you just pass the boolean value inside the constructor? e.g.



package
{
import flash.display.MovieClip;

class Unit extends MovieClip
{
private var _enemy:Boolean;
public function Unit(enemy:Boolean)
{
_enemy = enemy;
}

public function get enemy():Boolean
{
return _enemy;
}
}
}



i would propose using a getter method for the private variable _enemy instead of setting it pubilc.


best regards,

burn

burn_hh
January 29th, 2008, 06:27 AM
Hey :smirk:

Not too long ago I started using Flash CS3 and AS3. OOP is completely new for me, but I'm getting the hang of it with time. But I have a question -

I have a class which extends a movieclip, that class is a unit. In my game, there's a player unit and an enemy unit. I'm using the same class for both, the only difference is a boolean inside that tells if it's an enemy or a player unit, which affects what array of units in the above class they should be looping and checking for foes nearby.

To create a unit, what I do, and I figured is wrong, is after creating the unit in a create unit function I have, only then I set the boolean, from the outside, for instance:
unit = new unit_mc;
unit.isPlayer = true;

So 'in the long run' it plays fine, but when I create the class first, it already run loops and ifs regarding the isPlayer boolean, which wasn't yet defined, but only a line after.

So my question is, is there a way to define a boolean from the outside in the same time of creation? If not, how else can I solve this issue?
I thought about making a class that extends Unit, that only sets the boolean (using super.isPlayer, I believe?), but is it right to work that way?

Thanks a bunch!

btw what you're talking about are instance variables since they may be different for every single instance (hence the name). class variables are static variables (marked by the keyword static) which contains the same values for every instance of the specific class.
e.g. when you've got a static variable inside of your Unit class

private static var _uniformColor:uint = 0x00FF00; every single instance of the class will have a green uniform unless you change it to something else which will cause everybody else's uniform color to change.

Dragy
January 29th, 2008, 06:51 AM
I feel stupid, I didn't know it was possible to attach the boolean in the constructor. And thanks for the additional info! :D