/* ============================================================================== FakeButton demonstrates how to create a fake button on an Object Petit, petit@petitpub.com =============================================================================== */ import sandy.core.data.*; import sandy.core.group.*; import sandy.primitive.*; import sandy.view.*; import sandy.core.*; import sandy.core.face.*; import sandy.skin.*; import sandy.util.*; import sandy.core.transform.*; import sandy.events.*; import flash.display.BitmapData; var screen:ClipScreen; // Presentation surface var cam:Camera3D; var world:World3D = World3D.getInstance(); // Our 3D world var faces:Array; var rotation:Transform3D; function init( Void ):Void { // Create a clipScreen as presentation surface for the World3D screen = new ClipScreen( this.createEmptyMovieClip('screen', 1), 400, 400 ); // Create a camera, so we can see our world. cam = new Camera3D( 700, screen ); // Position the camera at a negative distance along the z axis cam.setPosition(0,0,-600); // Attach the camera to the world world.addCamera( cam ); // Listen to the onRenderEVENT to do per frame actions world.addEventListener(World3D.onRenderEVENT, this, camMove ); // Create the root group var bg:Group = new Group(); // and attach it to the world. world.setRootGroup( bg ); // Create the scen and attach it to the rootGroup createScene( bg ); // Finally we start rendering the world world.render(); } // For each fram, move the camera if special keys are pressed function camMove():Void{ var cam:Camera3D = World3D.getInstance ().getCamera (); // Move the camera along its x and y axes if (Key.isDown (Key.UP)){cam.moveUpwards(5);} if (Key.isDown (Key.DOWN)){cam.moveUpwards(-5);} if (Key.isDown (Key.LEFT)){cam.moveSideways(5);} if (Key.isDown (Key.RIGHT)){cam.moveSideways(-5);} // Move the camera along its line of sight if (Key.isDown (Key.HOME)){cam.moveForward(5);} if (Key.isDown (Key.END)){cam.moveForward(-5);} cam.lookAt( 0, 0, 0 ); } // The onPressEVENT event handler for the faces function onPress(event:ObjectEvent ){ var obj:TriFace3D = event.getTarget(); var id = obj.getId(); trace("Face: " + id ); report.text = id; if (id > 9 && id < 14) hit.text = "You clicked the button"; else hit.text =""; } // Create the root group of the world function createScene( bg:Group ):Void { // Start by creating a Plad3D primitive. var plane:Object3D = new Plane3D(300,300, 4, 'tri'); // We can create a skin with a fake button var holder:MovieClip = this.createEmptyMovieClip("holder", this.getNextHighestDepth()); holder.attachMovie("fakeButtonClip", "fake", 1); holder._alpha = 0; // Hide the MovieClip on Stage var skin:MovieSkin = new MovieSkin( holder ); // Apply the skin to the plane. plane.setSkin( skin ); // Draw the default skin on the back side plane.enableBackFaceCulling = false; // Loop through the faces, enable object events and add us as listener faces = plane.getFaces(); for ( var i = 0; i < faces.length ; i++ ){ faces[i].enableEvents(true); faces[i].addEventListener( ObjectEvent.onPressEVENT, this, onPress ); } // To get a better view of the plane, let's give it a transformation var tg:TransformGroup = new TransformGroup(); // We create Transform3D to carry out a transformation in 3 dimensions rotation = new Transform3D(); // We want it to rotate our plane rotation.rot(40,30,0); // Set the transform to the group which will hold our cube tg.setTransform( rotation ); // Our plane is added to the transform group tg.addChild( plane ); // Overlapping reference plane just to show the grid of faces var plane1:Object3D = new Plane3D(300,300, 4, 'tri'); var lineSkin = new SimpleLineSkin(0.5, 0x05113F, 60); plane1.setSkin(lineSkin); tg.addChild( plane1 ); // And finally the transformgroup is added as a child to the rootGroup bg.addChild( tg ); } // Start creating the world init();