Keeping the reference to the parameter of a function


#1

Hi,
I am trying to apply the same tweens to different items, but I am not able to keep the reference to the generated tweens.
First of all, I am with Actuate.
I want to tween n different items using the same tweens, so I have created a function for it

private function moveItem(item: DisplayObject, actuator: GenericActuator<Dynamic>): Void {
	actuator = Actuate.tween(item, time, {y: yada yada yada});
}

Then, if I call

moveItem(itemA, actuatorA);

and I look into actuatorA I find that actuatorA is null, while actuator contains the GenericActuator: how do I keep the reference between actuator and the parameter actuatorA received by the function? I should dereference actuator, right? But how? Maybe I am missing this part of Haxe…
Thanks


#2

Haxe works like this:

function test (reference:Sprite):Void {
    reference = new Sprite ();
    reference.name = "SecondSprite";
}

var ref = new Sprite ();
ref.name = "FirstSprite";
test (ref);

trace (ref.name); // FirstSprite

You pass in a reference like this, and you can overwrite the reference, but it will not overwrite the original variable you passed in.

On alternative is to pass an object:

function test (reference:Dynamic):Void {
    reference.sprite = new Sprite ();
    reference.sprite.name = "SecondSprite";
}

var ref = { sprite: new Sprite () };
ref.sprite.name = "FirstSprite";
test (ref);

trace (ref.sprite.name); // SecondSprite

…but the more common way is to return a value:

function test ():Sprite {
    var sprite = new Sprite ();
    sprite.name = "SecondSprite";
    return sprite;
}

var ref = new Sprite ();
ref.name = "FirstSprite";
ref = test ();

trace (ref.name); // SecondSprite

#3

Uh, ok, the problem is that I want to keep track of the actuator cycling through various functions similar to moveItem: moveItem1, moveItem2, moveItem3…

The tween has an onComplete callBack (that I removed from the example) that forwards the actuator to the following function, first to moveItem2, then to moveItem3 etc., this is why I needed to keep the reference to the actuator-box I passed in the first place.
I could use the Object solution, it is nearer to my idea.

Thanks Joshua!


#4

Hi GiG,

I have not tested this, I use Actuate very simply for now but if I understand what you are after I think you should do something like

private function moveItem(item: DisplayObject, actuator: GenericActuator<Dynamic>): GenericActuator<Dynamic>{
	return Actuate.tween(item, time, {y: yada yada yada}, actuator);
}

and then call

actuatorA = moveItem(itemA, actuatorA);

if actuatorA is null initially it will reference the GenericActuator after the call. If it’s not null then it should just work as well.


#5

Oh, this way you are forcing Actuate to use my actuator instead of creating a new one, very interesting approach, I am underestimating the possibility to pass an existing actuator to Actuate… Great!

Thank you too, Matse!