# Trying to understand this code!

**URL:** <https://forum.kirupa.com/t/trying-to-understand-this-code/297726>\
**Category:** flash\
**Created:** [October 7, 2009, 5:24pm UTC](https://forum.kirupa.com/t/trying-to-understand-this-code/297726 "2009-10-07T17:24:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Galland](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@Galland](https://forum.kirupa.com/u/Galland)\
**Post date:** [October 7, 2009, 5:24pm UTC](https://forum.kirupa.com/t/trying-to-understand-this-code/297726/1 "2009-10-07T17:24:14Z")

</div>

Hi!

I’m trying to understand a particular couple of lines of code.

A couple of members here have helped me some so far (much appreciated!).

The code is an AS3 carousel - adapted from Lee Brimelow by Matbury @ [actionscript.org](http://actionscript.org).  
(I`ve included complete AS3 code at the end of this post.)

I understand the idea of the for Loop - and that it is applying variables to an object created from the class shown below, but where I’m stumped is what the following line…

item.angl = i\*((Math.PI\*2) / numOfItems);

…means and HOW it relates in particular to the variable `angl:Number` in the class:

package {

public class Item extends ItemInner {

public var angl:Number;

public function Item() {  
//  
}  
}  
}

Anyone explain this briefly?

Thanks for any help - most impressed with the knowledge here & the willingness to share it.

My next step is Moock`s Essential AS3 - got a feeling that will help me a lot with things of this complexity.

Regards, Galland

/\*  
The ‘reflections’ will only work if ‘bitmap caching’ for the Icon  
instance ‘ref’ and the ‘masker’ instance is turned on in the properties panel.  
_/  
var numOfItems:uint = 5; // number of Items to put on stage  
var radiusX:uint = 250; // width of carousel  
var radiusY:uint = 75; // height of carousel  
var centerX:Number = 560; // x position of center of carousel  
var centerY:Number = 320; // y position of center of carousel  
var speed:Number = 0.05; // initial speed of rotation of carousel  
var itemArray:Array = new Array(); // store the Items to sort them according to their ‘depth’ - see sortBySize() function.  
init();  
// place Items on stage  
function init():void {  
for(var i:uint = 0; i \< numOfItems; i++) {  
var item:Item = new Item();  
// public var angl:Number; is in Item class.  
// Item class extends ItemInner in FLA library.  
item.angl = i \* ((Math.PI \* 2) / numOfItems);  
item.ref.mask = item.masker;  
item.alpha = 0.5;  
itemArray.push(item);  
addChild(item);  
item.addEventListener(Event.ENTER\_FRAME, enterFrameHandler);  
// listen for MouseEvents only on icons, not on reflections  
item.icon.addEventListener(MouseEvent.CLICK, clickHandler);  
item.icon.addEventListener(MouseEvent.ROLL\_OVER, rollOverHandler);  
item.icon.addEventListener(MouseEvent.ROLL\_OUT, rollOutHandler);  
}  
}  
// position Items in elipse  
function enterFrameHandler(event:Event):void {  
event.target.x = Math.cos(event.target.angl) \* radiusX + centerX; // x position of Item  
event.target.y = Math.sin(event.target.angl) \* radiusY + centerY; // y postion of Item  
// scale Item according to y position to give perspective  
var s:Number = event.target.y / (centerY + radiusY);  
event.target.scaleX = event.target.scaleY = s;  
event.target.angl += speed; // speed is updated by mouseMoveHandler  
sortBySize();  
}  
// set the display list index (depth) of the Items according to their  
// scaleX property so that the bigger the Item, the higher the index (depth)  
function sortBySize():void {  
// There isn’t an Array.ASCENDING property so use DESCENDING and reverse()  
itemArray.sortOn(“scaleX”, Array.DESCENDING | Array.NUMERIC);  
itemArray.reverse();  
for(var i:uint = 0; i \< itemArray.length; i++) {  
var item:Item = itemArray_;  
setChildIndex(item, i);  
}  
}  
function clickHandler(event:MouseEvent):void {  
// clean up your listeners before you do anything else to free up  
// user’s CPU resources  
for(var i:uint = 0; i \< itemArray.length; i++) {  
var item:Item = itemArray\*;  
item.removeEventListener(Event.ENTER\_FRAME, enterFrameHandler);  
item.icon.removeEventListener(MouseEvent.CLICK, clickHandler);  
item.icon.removeEventListener(MouseEvent.ROLL\_OVER, rollOverHandler);  
item.icon.removeEventListener(MouseEvent.ROLL\_OUT, rollOutHandler);  
// optional:  
removeChild(item);  
// to replace the carousel again see reInit() function  
}  
// put your own code here.  
}  
function rollOverHandler(event:MouseEvent):void {  
event.target.parent.alpha = 1;  
}  
function rollOutHandler(event:MouseEvent):void {  
event.target.parent.alpha = 0.5;  
}  
addEventListener(MouseEvent.MOUSE\_MOVE, mouseMoveHandler);  
/\*  
Update the speed at which the carousel rotates accoring to the distance of the mouse from the center of the stage. The speed variable only gets updated when the mouse moves over the Item Sprites.  
_/  
function mouseMoveHandler(event:MouseEvent):void {  
speed = (mouseX - centerX) / 6000;  
}  
// click to put Items back on stage  
rewButton.buttonMode = true;  
rewButton.addEventListener(MouseEvent.CLICK, reInit);  
// put items back on stage  
function reInit(event:MouseEvent):void {  
for(var i:uint = 0; i \< itemArray.length; i++) {  
var item:Item = itemArray_;  
item.alpha = 0.5;  
addChild(item);  
item.addEventListener(Event.ENTER\_FRAME, enterFrameHandler);  
item.icon.addEventListener(MouseEvent.CLICK, clickHandler);  
item.icon.addEventListener(MouseEvent.ROLL\_OVER, rollOverHandler);  
item.icon.addEventListener(MouseEvent.ROLL\_OUT, rollOutHandler);  
}  
}
