My text animation works perfectly, but doesn't repeat. How do I get this to repeat? Sorry I don't know Flash that well, but I just want this to play over and over. Thanks.
var myArray:Array = ["Big",
"Holiday",
"Sale",
"Buy",
"Now",
"And",
"Save"];
Timer
var tm:Timer = new Timer(500,0);
tm.addEventListener(TimerEvent.TIMER, countd开发者_C百科own);
function countdown(event:TimerEvent) {
if (myArray.length>0){
tx.text = myArray.shift();
}
}
tm.start();
I tried this
if (++myArray.length % 10 == 0)
Instead of shift()ing stuff from your array, keep the index you are at (0 at first) and increment it in your countdown, modulo the length of the array.
simple solution:
myArray.push(tx.text = myArray.shift());
but sharvey's solution is signifficantly better. it'd work like this:
var myArray:Array = ["Big",
"Holiday",
"Sale",
"Buy",
"Now",
"And",
"Save"];
var tm:Timer = new Timer(500,0);
var index:int = 0;
tm.addEventListener(TimerEvent.TIMER, countdown);
function countdown(event:TimerEvent) {
tx.text = myArray[index];
index = (index + 1) % myArray.length;//increment and "wrap around"
}
tm.start();
What sharvey means is something similar to this:
var myArray:Array = ["Big",
"Holiday",
"Sale",
"Buy",
"Now",
"And",
"Save"];
var tm:Timer = new Timer(500);
tm.addEventListener(TimerEvent.TIMER, countdown);
function countdown(event:TimerEvent) {
tx.text = myArray[(tm.currentCount-1)%myArray.length];
}
tm.start();
We subtract 1 from tm.currentCount to use the count as an array index(0 based), then use modulo(%) to 'loop/constrain' the count to the length of the array. Also, the timer now runs 'forever'.
We're all saying the same thing in slightly different ways :)
Hey, I don't wanna be a big party pooper, but wouldn't something like this be better solved by using Flash's Timeline? I.e. create a looping animation in Flash itself? That way you'd just export it to actionscript and add the animation as a child in your code.
var anim:MyOffensiveAnimation = new MyOffensiveAnimation();
addChild(anim); // that's it, animation starts playing
Or better yet, add it to whatever MovieClip it should be in.
For the record, though, I really liked back2dos's "simple solution".
// OP's Timer-related code ommitted
var i:int = 0;
function countdown(e:Event) {
tx.text = myArray[i];
i = (i+1) % myArray.length; // resets i to zero when it gets to the size of the array
}
精彩评论