Random Pulsing Tiles with Flash and TweenLite

by carl schooff on March 9, 2011 · View Comments

in AS3 Tips,GreenSock Tweening Platform Tips

This movie requires Flash Player 9

I was “walking by” a tv that was showing American Idol the other night and was mesmerized by this mega screen of pulsing tiles that was behind the performers. I was instantly compelled to try to build the effect with Flash. With a little timer, loop, conditional logic and TweenLite it was a breeze. Check out I had built it with Flash and see the same effect running with JavaScript and CSS animations.

Watch the Video

What’s that?!?! JavaScript mentioned on a Flash blog!?!?!

Simmer down folks. Its all good. Yes, my friend Matt took up the challenge to see if this could be built with JavaScript/CSS. I was very impressed with his results:

View Matt’s Tile Pulse with JavaScript and CSS animation

(FF4, Sarari, Chrome)

There’s a lot of debate going on about HTML5 vs Flash. I’m all for using the right tool for the job. For me the Flash platform offers a quick and fun way to create visual assets and power them with ONE language. I also enjoy not spending my time testing how my creations work in a plethora of browsers. I’m convinced that right now Flash offers a quicker and more enjoyable path to building engaging multi-media experiences that will perform consistently across operating systems / browsers / devices.

It is absolutely true that a lot of what Flash can do can be handled by HTML5 and its family of friends: CSS3 and numerous JavaScript frameworks like jQuery. I’ll even admit I often see jQuery animations running smoother than Flash. In order to get going on that side though, one needs to have a solid understanding of html, css, and javascript and also be constantly aware of which version of which browser supports the features of each. As browsers normalize (which they haven’t shown any sign of doing over the past 15 years) and tools are created to work with these technologies, I’m sure we’ll continue to see bigger and better things.

Ultimately each technology has its place, purpose, strengths and weaknesses. I’m certain both will find a way to live long happy lives.

/end rant:)

How does it work?

My file uses a Timer that calls a function that randomly chooses which tiles to pulse. Once a tile is chosen to pulse, we figure out whether or not it needs to scale up or down. After that we just fire off the necessary tween.

Here is the AS3 Meat


//import the greensock classes for tweening and plugins
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.TweenPlugin;
import com.greensock.plugins.ScalePlugin;
TweenPlugin.activate([ScalePlugin]);

//figure out how many tiles there are
var numTiles:Number=tileContainer_mc.numChildren;

//set up a timer to call the fadeSome function
var tileAnimator:Timer=new Timer(650,600);
tileAnimator.addEventListener(TimerEvent.TIMER, fadeSome);
tileAnimator.start();

function fadeSome(e:Event = null) {
	//loop through all the tiles
	for ( var i:Number = 0; i < numTiles; i++ ) {

               //create reference to current tile in the loop
		var currentTile:MovieClip = tileContainer_mc.getChildAt( i ) as MovieClip;

	//give each tile a 50% chance of animating
       if( Math.random() > .5 ){

                  //figure out which way to tween: up to 1 or down to 0
		   var scaleTo:Number = ( currentTile.front_mc.scaleX == 1 ) ? 0 : 1

                   //do the tween
		   TweenLite.to( currentTile.front_mc, .5, { scale:scaleTo } );
		   }

	}
}

//randomly adjust alpha of each tile in tileContainer_mc
for ( var i:Number = 0; i < numTiles; i++ ) {
	tileContainer_mc.getChildAt( i ).alpha = Math.random() + .4;
}

Download Flash CS4 Source

SNORKLtv-tilePulse-onStage

Next

I will be showing you how to create a grid of objects via code very soon along with some other handy tips

Additional Resources

Official GreenSock TweenLite info
More GreenSock Tutorials from Snorkl.tv

Post to Twitter Post to Facebook

  • http://matthewlein.com Matthew Lein

    This is exactly why I stopped walking by stores that sell TVs.

  • Martin

    Thanks Carl, like the the inline short hand conditional as code saver, long live flash IDE!!

    MC

  • http://www.snorkl.tv/ carl schooff

    i agree! and yeah the shorthand stuff makes things pretty easy. i was going to show the following to replace even the random conditional:

    var tween:TweenLite = (Math.random() < .5) ? TweenLite.to(currentTile.front_mc, .5, {scale:(currentTile.front_mc.scaleX == 1) ? 0 : 1}) : null;

    but that would be ridiculous;)

    glad you dropped in.

  • http://www.snorkl.tv/ carl schooff

    then I guess you won’t appreciate my random charlie sheen quote generator i’m doing next week…

  • Snefer

    hahahaha i am already laughing… go carl ! go carl !

  • http://www.facebook.com/profile.php?id=290301287 Devon Franklyn

    Great work Carl, It’s really useful thanks.

    Can you make a Tutorial of a Slider you can slide that controls a TweenMaxTimeline in real time as your sliding it? and maybe continues to play from where you drop it. Thanks :D

  • http://www.snorkl.tv/ carl schooff

    hi Devon,

    the slider is a great idea. x10 already made a video and provided some
    sample files.

    get files here
    http://forums.greensock.com/viewtopic.php?f=1&t=4642&p=18579#p18579
    -once you download the zip you will have an fla only. need to use your own
    greensock classes.
    -also you will need to change you publish settings as the fla wants to
    publish to a /bin folder.

    you can watch his video here:
    http://www.greensock.com/learning/ 2nd one from the top.

    Carl

  • http://www.facebook.com/profile.php?id=290301287 Devon Franklyn

    Thanks Carl :)

  • http://twitter.com/BCultureMedia B Culture Media

    Adobe Flash is still the bomb! jQuery is cool but Flash’s design abilities are vast which B Culture Media discusses in this blog post http://buzzculture.wordpress.com/2012/01/17/flash-is-alive/.

blog comments powered by Disqus

Previous post:

Next post: