Menu Home

5

osbo.com | Flash | Papervision3D | 5

Add interaction to the object.

Example

5

Instructions

  1. 1
  2. 2
  3. 3
  4. 4
  5. Import interactive event class
    import org.papervision3d.events.InteractiveScene3DEvent;
  6. Set viewport to interactive
    viewport.interactive = true;
  7. Set material to interactive
    material.interactive = true;
  8. Listen for object over event
    object.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, handleObjectOver);
  9. Listen for object out event
    object.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, handleObjectOut);
  10. Listen for object press event
    object.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, handleObjectPress);
  11. Handle object over event
    private function handleObjectOver(e:InteractiveScene3DEvent):void {}
  12. Set mouse icon to hand
    viewport.containerSprite.buttonMode = true;
  13. Handle object out event
    private function handleObjectOut(e:InteractiveScene3DEvent):void {}
  14. Set mouse icon to pointer
    viewport.containerSprite.buttonMode = false;
  15. Handle object press event
    private function handleObjectPress(e:InteractiveScene3DEvent):void {}
  16. Set material color to random
    material.fillColor = Math.random() * 0xffffff;

Code

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    import org.papervision3d.cameras.Camera3D;
    import org.papervision3d.events.InteractiveScene3DEvent;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;

    public class Papervision3D5 extends Sprite
    {
        private var scene    :Scene3D;
        private var camera   :Camera3D;
        private var viewport :Viewport3D;
        private var renderer :BasicRenderEngine;
        private var material :ColorMaterial;
        private var object   :Plane;

        public function Papervision3D5()
        {
            scene = new Scene3D();

            camera = new Camera3D();

            viewport                  = new Viewport3D();
            viewport.autoScaleToStage = true;
            viewport.interactive      = true;
            addChild(viewport);

            renderer = new BasicRenderEngine();

            material             = new ColorMaterial(0x000000);
            material.doubleSided = true;
            material.interactive = true;

            object = new Plane(material);
            scene.addChild(object);

            object.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, handleObjectOver);
            object.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, handleObjectOut);
            object.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, handleObjectPress);

            addEventListener(Event.ENTER_FRAME, handleEnterFrame);
        }

        private function handleEnterFrame(e:Event):void
        {
            object.yaw(1);

            renderer.renderScene(scene, camera, viewport);
        }

        private function handleObjectOver(e:InteractiveScene3DEvent):void
        {
            viewport.containerSprite.buttonMode = true;
        }

        private function handleObjectOut(e:InteractiveScene3DEvent):void
        {
            viewport.containerSprite.buttonMode = false;
        }

        private function handleObjectPress(e:InteractiveScene3DEvent):void
        {
            material.fillColor = Math.random() * 0xffffff;
        }
    }
}

Download