-
animate Score Numbers climbing up
hi. i know this should be easy, but my scripting sucks. i have a dynamic text field and i want the points to count up. say the user just finished a round and they got a score of $4000. i want the text to climb from $0 to $4000 in say 3 seconds. can someone point me to a movie or tutorial?
thanking you kindly.
j
-
Ever heard of TweenMax ?
http://www.greensock.com/tweenmax/
This is what I'd do with it:
Actionscript Code:
import com.greensock.plugins.*; TweenPlugin.activate([EndVectorPlugin]);
var v:Vector.<Number> = new Vector.<Number>(); //var for current state var end:Vector.<Number> = new Vector.<Number>(); //var for end state TweenMax.to(v, .5, { endVector:end, onUpdate:function(){_txt.text = Number(v).toFixed(2);} } ); //toFixed(2) adds two numbers after the dot - 50.00
-
Although TweenMax is free, it says I have to pay a license in order to use it in games I charge for. What if I don't? Can they detect games being sold using TweenMax not paying for the license?
-
having problems with TweenMax and i cant believe theres not a simpler way to do this. isnt there a way to do something simple, with built in Tweens.
myNum = 100;
myNum = myNum + 100;
if (myNum = 4000) then stop (adding).
p.s. i know that my code is a joke, but ya know..
-
Tweening is actually pretty easy, you can do it without the TweenMax class. (Note this is untested code; I just wrote it up. May have compile errors.)
Code:
import flash.utils.getTimer;
scoreStart:Number = 0;
scoreCurrent:Number = 0;
scoreGoal:Number = 0;
startTime:Number = 0;
setScore(4000);
// ENTER_FRAME event handler
function scoreInterpolate(event:Event)
{
var currentTime:Number = getTimer();
var durationTime:Number = 3000;
var t:Number = (currentTime - startTime) / durationTime;
if (t < 1)
{
// Not finished counting up; interpolate value
scoreCurrent = linearInterpolate(t, scoreStart, scoreGoal);
}
else
{
// At or exceeded the maximum time
// Set the score to the target and terminate the event
scoreCurrent = scoreGoal;
removeEventListener(Event.ENTER_FRAME, scoreInterpolate)
}
trace(scoreCurrent);
}
// Performs a linear interpolation between two values
function linearInterpolate(t:Number, minVal:Number, maxVal:Number) : Number
{
return minVal + t * (maxVal - minVal);
}
// Invokes an ENTER_FRAME event handler that will count up scoreCurrent to the provided goal
function setScore(newScore:Number)
{
scoreStart = scoreCurrent;
scoreGoal = newScore;
startTime = getTimer();
addEventListener(Event.ENTER_FRAME, scoreInterpolate);
}
This will perform a linear interpolation to count up. (ie the amount will grow by the same amount each frame) You could easily provide a function that will do exponential growth or anything else you wanted. (See here.)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|