Pointer-like functionality in AS3?
Is there any way at all to store a C++-like pointer in AS3? What I need is some way to keep track of an object's state without regularly passing that object as a parameter. For example, let an enemy have a pointer to a player to attack without needing to regularaly tell the enemy where the player is, and without storing the player object itself as part of the enemy.
Pass the player in to the enemy and save it in a member variable, perhaps in a set property, perhaps in a function called setTarget() - whichever floats your boat.
In AS3, as in most managed languages, what you're really passing around are references to objects - in other words, pointers by another name.
In pseudo-AS3:
class enemy
{
private var _target:Player;
function set target(player:Player):void {
_target = player;
}
function update():void {
if (_target != NULL)
attack(_target);
}
}
In AS3, as in most managed languages, what you're really passing around are references to objects - in other words, pointers by another name.
In pseudo-AS3:
class enemy
{
private var _target:Player;
function set target(player:Player):void {
_target = player;
}
function update():void {
if (_target != NULL)
attack(_target);
}
}
sindisil - Thanks for pointing that out about the references in AS3.
My main problem here, though, isn't the memory issue - it's more that I would prefer not to need to keep passing the object to another every frame.
I guess I'd better just get used to AS3 and stop trying to use C++ everywhere.
My main problem here, though, isn't the memory issue - it's more that I would prefer not to need to keep passing the object to another every frame.
I guess I'd better just get used to AS3 and stop trying to use C++ everywhere.
Quote:Original post by bepawuca
sindisil - Thanks for pointing that out about the references in AS3.
My main problem here, though, isn't the memory issue - it's more that I would prefer not to need to keep passing the object to another every frame.
Sure.
So the mechanism I've outlined gets you exactly that.
Quote:
I guess I'd better just get used to AS3 and stop trying to use C++ everywhere.
Yes. It is virtually always better to use a language idiomatically.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement