views:

82

answers:

2

I have different cards, I need to reveal them in a wave like form, i.e. the cards open and close at different timings sequentially and systematically such that they form a wave.

I have the cards in an array alr. How do I implement this animation in the most efficient way? Thx!

+2  A: 

You should get comfortable using Math.sin() and Math.cos(). Look into simple trig functions and memorize converting radians and degrees. There are many cool and interesting tricks you can re-use in different contexts once you have a good grasp on these concepts. The following snippet demonstrates how to move an object with "a wave like form". It may not be exactly what you are looking for but it should help you get where you trying to go. It is written in the AS3 using the CS4 IDE.

var n:Number = 0;
var ball:MovieClip = new MovieClip();
ball.graphics.beginFill( 0xFFCC00, 1 );
ball.graphics.drawCircle( 0, 0, 15 );
addChild( ball );

ball.x = stage.stageWidth;
ball.y = stage.stageHeight * .5;

var prev:Point = new Point(ball.x, ball.y);

addEventListener( Event.ENTER_FRAME, onEnterFrameHanlder );

function onEnterFrameHanlder( event:Event ):void
{
    n+=3;
    ball.x = Math.cos( n * .25 * Math.PI/180 ) * ( stage.stageWidth * .5 ) + ( stage.stageWidth * .5 );
    ball.y = Math.sin( n * Math.PI/180 ) * ( stage.stageHeight * .5 ) + ( stage.stageHeight * .5 );

    graphics.lineStyle( 1, 0xFFCC00 );
    graphics.moveTo( ball.x, ball.y );
    graphics.lineTo( prev.x, prev.y );

    prev.x = ball.x;
    prev.y = ball.y;
}
jeremynealbrown
Thanks for the interesting animation, but this is not what I am looking for. I wanted to open 1 card, close it slowly, open another card, close it slowly... for eg when I reached the 4th card, the 1st card is already closed...
yeeen
What jeremynealbrown is saying is that you should look at Math.sin and Math.cos, which are very good to use for 'wave' like functions. Try changing some parameters to see what happens and you'll most likely be able to write whatever suits what you need
Antti
Check out my other related qn here: http://stackoverflow.com/questions/2161123/move-objects-in-an-array-producing-a-stadium-wave-effect
yeeen
+2  A: 

presuming what you are meaning is a stadium "wave" effect, try to define an array with an offset, this will depend if you are using a tweening engine etc or if you are using another function.

The Timer class for example will do you well in this one:

private var t:Timer = new Timer(100, 0);
    private var index:int = 0;
    t.addEventListener(TimerEvent.TIMER, ping);
    t.start();

    private function ping(ev:TimerEvent) {
        if(index < waveArray.length){
            waveArray[index].startAnimation(); //If animated by object
            startAnimation(waveArray[index]); //If animated by container
            index ++;
        }
        else {
            t.stop();
            endAnimation(); //All cards have animated
        }
    }
shortstick
Yup, I am refering to a stadium wave effect. It seems like I can't use the word "private", I have removed it. Why is if-else used not a for loop?
yeeen
@yeeen: these animations are triggered by `TimerEvent`s ... using a for loop would start them all at once, whereas using a timer will cause them to start one after another ...
back2dos
what this is doing is every 100ms (first value of the Timer() constructor) it runs the 'ping' function, the if/else loop is only there to stop the timer once all of the items have gone through and prevent an out of range error on the array. As the index variable slowly loops through the array you will get an offset to each animation starting. Basically this is a for loop with an offset to each call.
shortstick
Check out my other related qn here: http://stackoverflow.com/questions/2161123/move-objects-in-an-array-producing-a-stadium-wave-effect
yeeen