package { import away3d.cameras.HoverCamera3D; import away3d.containers.View3D; import away3d.primitives.GeodesicSphere; import away3d.primitives.Sphere; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; [SWF(width="500", height="400", frameRate="50", backgroundColor="#FFFFFF")] public class Basic08_geodesicsphere extends Sprite { private var cam:HoverCamera3D; private var lastKey:uint; private var keyIsDown:Boolean = false; private var View:View3D; private var cover:Cover; private var sphere:Sphere; private var geoSphere:GeodesicSphere; public function Basic08_geodesicsphere() { // create a "hovering" camera cam = new HoverCamera3D(); cam.z = -1000; // make sure the camera is positioned away from the default 0,0,0 coordinate cam.panangle = 0; cam.tiltangle = 0; cam.targetpanangle = 0; cam.targettiltangle = 0; cam.mintiltangle = -90; cam.zoom = 5; // create a viewport View = new View3D({camera:cam,x:250,y:200}); addChild(View); // add a huge surrounding sphere so we really can see what we're doing geoSphere = new GeodesicSphere({radius:200,material:"red#black",x:200}); geoSphere.fractures = 5; geoSphere.invertFaces(); View.scene.addChild(geoSphere); // a sphere with 756 faces sphere = new Sphere({radius:200,material:"red#black",x:-200,segmentsW:12,segmentsH:13}); sphere.invertFaces(); View.scene.addChild(sphere); // update the view cam.hover(); View.render(); // only run when user is above the SWF cover = new Cover(this,500,400,"Roll over and Click to activate. Use Space key to show both and arrow keys to toggle."); addChild(cover); // listen for key events and run every frame this.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown); this.stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp); this.addEventListener(Event.ENTER_FRAME,onEnterFrame); } private function onEnterFrame(e:Event):void { if(!cover.visible) { if(keyIsDown){ // if the key is still pressed, just keep on moving switch(lastKey){ case Keyboard.LEFT : showLeft(); break; case Keyboard.RIGHT : showRight(); break; case Keyboard.SPACE : showBoth(); break; } } geoSphere.rotationX += 1; sphere.rotationX += 1; // render the view cam.hover(); View.render(); } } private function showBoth():void { geoSphere.visible = true; sphere.visible = true; } private function showLeft():void { geoSphere.visible = true; sphere.visible = false; } private function showRight():void { geoSphere.visible = false; sphere.visible = true; } private function onKeyDown(e:KeyboardEvent):void { lastKey = e.keyCode; keyIsDown = true; } private function onKeyUp(e:KeyboardEvent):void { keyIsDown = false; } } }