A sometimes reflection on Eve, Coding and Life-in-General online...

Sunday, July 27, 2008

Journeys through Flash AS3 - The Carousel

Neato - my first carousel is now live and what have I learned? Well there's a ton of great examples of carousels around - that's the first thing. The second? Always check if something is in AS2 before you try to compile it in AS3. Duh. Now for my own meager contribution: intervals. I had my fancy carousel flying around and I wanted to slow down each item as it arrived at the front. Sounds easy? Sure, but I'm no maths wiz so it took me a while to pen, paper and scratch forehead on this one. Here's the final idea I came up with:

  1. A function which ascertains how big the remainder of a division is
  2. An arbitrary increment based on circumference divided by the number of objects
  3. Testing the function on the position around the circumference divided by _2_ above
  4. Slowing the shifted increment of the carousel as a result of a true return from the function
Here's the code:




// if ((shift / intIncrement) == round number) { shift = an interval }
// and shift by a smaller increment as a result

function isCloseToRound(num:Number):Boolean {
if (( num%1 ) < 0.1 ) {
return true;
}
else {
return false;
}
}


var intIncrement = (( Math.PI*2 ) / numObj );
if (isCloseToRound(shift / intIncrement)) {
shift += shiftIncrement / 2;
}
else
{
shift += shiftIncrement; // 0.02 at start
}


No comments:

About Me

My photo
Web Designer, Gamer & Photographer in no particular order.