views:

25

answers:

2

Hi,
I have three balls. What do I do?

They should look like they are being juggled when animated, but I'm not indexing them correctly. Please help.
Thanks

Edit
Maybe I should just use an array. getChildAt sort of works, but it's not looping though perfectly. It moves one or two balls, not one by one...

//JUGGLING ANIMATION WITH 3 BALLS
var myTimer:Timer = new Timer(444);
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
var T:Number =0; 

//WHERE I'M SETTING IT WRONG
T++; 
T %= 3;
var dr:*;
dr = getChildAt(numChildren - 1);
for(var i:int; i <T; i++){
setChildIndex(dr,i);
trace(i);
}
}

Output
The output traces zero, and the animation looks like it moves either 1 or 2 balls at once, rather than looping through each.

+1  A: 

Edit:

Don't know what you really want to achieve but change only one index within one call

var depth:Number =0;

var myTimer:Timer = new Timer(444);

myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();

function someFunction(event:TimerEvent):void {
  depth =(depth + 1) % 3;

  var dr:DisplayObject = getChildAt(numChildren - 1);

  setChildIndex(dr, depth);

  trace(depth);
}

Your T value is wrong you can't increment and doing modulo in the same time=>

T = (T + 1) % 3;

or

T++; 
T %= 3;

and you are also reseting T to 0 every time within your repeated function, put it outside of the function :

var T:Number =0;

var myTimer:Timer = new Timer(444);
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();

function someFunction(event:TimerEvent):void {
  T =(T + 1) % 3;
  var dr:DisplayObject = getChildAt(numChildren - 1);
  for(var i:int=0; i <T; i++){
    setChildIndex(dr,i);
    trace(i);
  }
}
  • You also have forget to set a value for i into your loop.
  • And you are changing in the same loop, T times the child index, so at the end only the last one set will be visible on your screen.
Patrick
@Patrick, sorry this is messy. I tried and it's still animating the same way. I'm try to learn for loops and indexing.
VideoDnd
@Patrick, thanks, that's working ok.
VideoDnd
+1  A: 

You aren't very clear on what you're trying to achieve. If you want to animate 3 clips using one timer, but animating 1 clip at a time, you could try something like:

var myTimer:Timer = new Timer(444);
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
var i:int = (myTimer.currentCount - 1)%3;
var dr:DisplayObject = getChildAt(i);
setChildIndex(dr,i);
trace(i);
}
  1. You declare T inside your function, so wouldn't that reset to 0 ? Then you loop through multiple clips, do you want to control all three at the same time ? That isn't clear.
  2. You can use Timer's currentCount to get 'the total number of times the timer has fired since it started at zero'
  3. I understand this is a sample/snippet, but: someFunction doesn't say much about what you're trying to do, variable names aren't consistent(e.g. T), getChildAt() returns a DisplayObject that you can cast to Sprite/MovieClip, depending on usage, but using * like that isn't a good usage of the language.

HTH

George Profenza
@George Profenza, they are instances on stage "r" "g" "b" I'm animating z-depth. I want to send getChildAt to the top or the back of the stack.
VideoDnd