forked from: flash on 2010-1-9

by hacker_lra4mj5w forked from flash on 2010-1-9 (diff: 8)
...
@author Charlie Schulze, charlie[at]woveninteractive[dot]com
♥0 | Line 109 | Modified 2012-10-24 23:31:01 | MIT License
play

ActionScript3 source code

/**
 * Copyright hacker_lra4mj5w ( http://wonderfl.net/user/hacker_lra4mj5w )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/qPv1
 */

// forked from mirkofresa's flash on 2010-1-9
package 
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import com.greensock.easing.Strong;
    import gs.TweenLite;
    import org.papervision3d.materials.BitmapFileMaterial;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.view.BasicView;
    
    /**
     * ...
     * @author Charlie Schulze, charlie[at]woveninteractive[dot]com
     */
    
    public class Main extends BasicView 
    {
        protected var planes:Array = [];
        protected var numItems:Number = 20;
        protected var radius:Number = 685;
        protected var currentItem:Number = 0;
        
        protected var mat:BitmapFileMaterial;
        protected var planesHolder:DisplayObject3D;
        protected var rightBtn:Sprite;
        protected var leftBtn:Sprite;
        
        public function Main():void 
        {
            super();
            init();
        }
        protected function init():void 
        {
            createChildren();
            createButtons();
            commitProperties();
            startRendering();
        }
        protected function createChildren():void 
        {
            
            planesHolder = new DisplayObject3D();
            
            //Create Material
            mat             = new BitmapFileMaterial("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQpfb6NV1nuaVpIFL6w7M7GY_LJs6v3NvUJl8_f9PapsJl4NNA");
            mat.smooth         = true;
            mat.doubleSided = false;
            
            for (var i:int = 0; i < numItems; i++) 
            {
                var plane:Plane = new Plane(mat, 285, 351);
                planes.push(plane);
                
                //Add plane to the scene
                planesHolder.addChild(plane);
            }
            scene.addChild(planesHolder);
        }
        
        protected function commitProperties():void 
        { 
            //Set properties of our planes
            for (var i:int = 0; i < planes.length; i++) 
            {
                var angle:Number     = Math.PI * 2 / numItems * i;
                var plane:Plane     = planes[i];
                plane.x             = Math.cos(angle) * radius;
                plane.z             = Math.sin(angle) * radius;
                plane.rotationY     = -360 / numItems * i - 90;
            }
            
            //Adjust camera
            camera.y = 50;
            
            //Rotate once
            rotate();
        }

        //Rotates the carousel
        protected function rotate():void 
        {
            var rotateTo:Number = (-360 / numItems) * currentItem + 90;
            TweenLite.to(planesHolder, 1, { rotationY:rotateTo, ease: Strong.easeOut } );
        }
        
        /*
         * Everything below this point is just for creating / setting events for 
         * controlling the carousel. 
         */

        protected function createButtons():void 
        {
            //Create Buttons
            rightBtn = createButton();
            leftBtn = createButton();
                
            addChild(leftBtn);
            addChild(rightBtn);
            
            //Add button listeners
            rightBtn.buttonMode = true;
            leftBtn.buttonMode = true;
            rightBtn.addEventListener(MouseEvent.CLICK, buttonClick);
            leftBtn.addEventListener(MouseEvent.CLICK, buttonClick);
                        
            //Place buttons on stage
            rightBtn.x             = stage.stageWidth - 120;
            leftBtn.x             = 100;
            rightBtn.y             =  stage.stageHeight -30;
            leftBtn.y             =  (stage.stageHeight -30) + 20;
            leftBtn.rotation     = 180;
        }
        
        //Button actions
        protected function buttonClick(evt:MouseEvent):void 
        {
            switch (evt.target)
            {
                case rightBtn:
                currentItem --;
                break;
                
                case leftBtn:
                currentItem ++;
                break;
            }
            rotate();
        }
        
        //Creates a simple arrow shape / returns the sprite
        protected function createButton():Sprite
        {
            var btn:Sprite = new Sprite();
            
            btn.graphics.beginFill(0x333333);
            btn.graphics.moveTo(0, 0);
            btn.graphics.lineTo(0, 20);
            btn.graphics.lineTo(10, 10);
            btn.graphics.lineTo(0, 0);
            btn.graphics.endFill();
            return btn;
        }
    }
}