- Home /
iTween ValueTo onUpdateParams passing
I'm trying to make 4 HP bar to drop down smoothly using iTween The HP bar is a 64-pixel-wide GUITexture
var hpBar : GUITexture[];
function Tween()
{
iTween.ValueTo (gameObject, iTween.Hash("from" , hpBar[0].pixelInset.width,
"to" , chara0curHP/chara0totalHP*64,
"time" , 0.5,
"onUpdate" , "HPBar0Tween")
);
iTween.ValueTo (gameObject, iTween.Hash("from" , hpBar[1].pixelInset.width,
"to" , chara1curHP/chara1totalHP*64,
"time" , 0.5,
"onUpdate" , "HPBar0Tween")
);
iTween.ValueTo (gameObject, iTween.Hash("from" , hpBar[2].pixelInset.width,
"to" , chara2curHP/chara2totalHP*64,
"time" , 0.5,
"onUpdate" , "HPBar0Tween")
);
iTween.ValueTo (gameObject, iTween.Hash("from" , hpBar[3].pixelInset.width,
"to" , chara3curHP/chara3totalHP*64,
"time" , 0.5,
"onUpdate" , "HPBar0Tween")
);
}
function HPBar0Tween(val : int) { hpBar[0].pixelInset.width = val;}
function HPBar1Tween(val : int) { hpBar[1].pixelInset.width = val;}
function HPBar2Tween(val : int) { hpBar[2].pixelInset.width = val;}
function HPBar3Tween(val : int) { hpBar[3].pixelInset.width = val;}
My question is, how can I cut down the number of onUpdate functions by providing the 2nd parameter to onUpdateParams function? I've tried to supply Hashtable as argument for onUpdateFunction and iTween doesn't accept it. I've already tried to supply JavaScript Object as the argument and iTween treat them as int
What would be my other options rather than this long,messy, unelegant way of handling it?
I appreciate any feedback
Answer by Wolfram · Jul 05, 2012 at 01:06 PM
With the solution prevented by karsten here, you only need to change 3 lines of code in iTween, and can then use an inline delegate for "onupdate"/"oncomplete"/"onstart" (instead of an explicit function) to execute arbitrary code with any parameters you like.
So for example you could then do something like:
string colorToChange="_Color";
iTween.ValueTo(gameObject, iTween.Hash(
"from", a,
"to", b,
"onupdate", (Action<object>) (newVal => {
Debug.Log("running iTween onupdate for "+colorToChange+" "+(float)newVal);
renderer.material.SetColor(colorToChange,Color.white*(float)newVal);
}
));
NOTE: I don't know whether this works in US/JS, too, or whether the syntax would change.
Your answer
Follow this Question
Related Questions
Using iTween ValueTo with onupdateparams 1 Answer
using iTween.ValueTo for the rotation of a GameObject with RotateAround 1 Answer
iTween with multiple ValueTo calls on same GameObject 1 Answer
Using private class strings with iTween event calls 0 Answers
iTween JavaScript orienttopath bug? 0 Answers