views:

64

answers:

5

Hi,
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, countdown);
function countdown(event:TimerEvent) {
if (myArray.length>0){
tx.text = myArray.shift();
}
}
tm.start();

I tried this

if (++myArray.length % 10 == 0)
+3  A: 

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.

sharvey
if (++myArray.length % 10 == 0) I tried this, but it said text must be non null. Do you have an example of how this would work.
pixelGreaser
where does the 10 come from, there seems to be just 7 words in that array. Also you cant change the array.length property like that.
sharvey
+2  A: 

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();
back2dos
@back2dos 'first one runs butshows null ref, second one's perfect. Thanks
pixelGreaser
+1  A: 

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 :)

George Profenza
@George Profenza, This works great, I get it now.
pixelGreaser
+1  A: 

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".

aaaidan
@aaaidan, thanks, I still use Timeline animation.
pixelGreaser
Well that's good to know! Sometimes I find I am tempted to use code where timeline animation is better suited, is all. Cheers!
aaaidan
A: 
// 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
} 
aaaidan