Michael Jackson Pearl Jam Stevie Ray Vaughan Ozzy Osbourne Finntroll MP3 list A Fine Frenzy Capone Phil Collins Shakira Pink Floyd Keane

Advanced Interactivity (Without InteractiveScene3DEvent)

advancedinteractivity3

download-source

There are multiple ways of accomplishing any task. With papervision that statement holds true. When you want to interact with 3D objects one way we have found works very well is to apply the MouseEvent's to the textureFile and bypass the InteractiveScene3DEvent all together.

Here are a few things to remember when working with anything interactive in Papervision

1) Always set your ViewPort to interactive
2) Always set your Material to interactive
3) Set your Material animation to "true" if you want to see the material update
4) Be sure that your render is running so you can see your material update

Here is some sample code that shows the basic building blocks of an animated / interactive papervision object:

Actionscript:
  1. //Viewport set to interactive
  2. viewport.interactive                      = true;
  3.  
  4. //Create your symbol - can be a class or MovieClip exported in your library
  5. var textureMC:TextureSymbol     = new TextureSymbol();
  6. textureMC.id                = i;
  7. textureMC.clickButton.addEventListener(MouseEvent.CLICK, onMovieMatClicked);
  8.  
  9. //Create your movie Material - animated / interactive set to true
  10. var movieMat:MovieMaterial   = new MovieMaterial(textureMC, true, true);
  11. movieMat.doubleSided            = true;
  12. movieMat.interactive          = true;
  13. movieMat.smooth             = true;

This approach is nice because you can deal with native Flash events.

Here is the full class:

Actionscript:
  1. package
  2. {
  3.     import flash.events.Event;
  4.     import flash.events.MouseEvent;
  5.     import gs.easing.Quint;
  6.     import gs.TweenLite;
  7.     import org.papervision3d.materials.MovieMaterial;
  8.     import org.papervision3d.materials.utils.MaterialsList;
  9.     import org.papervision3d.objects.primitives.Cube;
  10.  
  11.     import org.papervision3d.view.BasicView;
  12.     import TextureSymbol;
  13.    
  14.     /**
  15.      * ...
  16.      * @author Charlie Schulze, charlie[at]woveninteractive[dot]com
  17.      */
  18.    
  19.     public class Main extends BasicView
  20.     {
  21.         protected var cube:Cube;
  22.         protected var interactiveMats:Array;
  23.         protected var materialsList:MaterialsList;
  24.        
  25.         public function Main():void
  26.         {
  27.             super();
  28.             init();
  29.         }
  30.         protected function init():void
  31.         {
  32.             createChildren();
  33.             commitProperties();
  34.             startRendering();
  35.         }
  36.         protected function createChildren():void
  37.         {
  38.             //Set the viewport to interactive
  39.             viewport.interactive = true;
  40.            
  41.             //Create Materials:
  42.             materialsList = new MaterialsList();
  43.             interactiveMats = ["front", "back", "top", "bottom", "left", "right"];
  44.            
  45.             for (var i:int = 0; i <interactiveMats.length; i++)
  46.             {
  47.                 //Create the texture symbol from the library
  48.                 var textureMC:TextureSymbol     = new TextureSymbol();
  49.                 textureMC.id                    = i;
  50.                 textureMC.clickButton.addEventListener(MouseEvent.CLICK, onMovieMatClicked);
  51.                
  52.                 //Create the MovieMat
  53.                 var movieMat:MovieMaterial   = new MovieMaterial(textureMC, true, true);
  54.                 movieMat.doubleSided            = true;
  55.                 movieMat.interactive          = true;
  56.                 movieMat.smooth                 = true;
  57.                
  58.                 materialsList.addMaterial(movieMat, interactiveMats[i]);
  59.             }
  60.            
  61.             //Create Cube
  62.             cube    = new Cube(materialsList, 100, 100, 100);
  63.            
  64.             //Add cube to the scene
  65.             scene.addChild(cube);
  66.         }
  67.        
  68.         protected function onMovieMatClicked(evt:MouseEvent):void
  69.         {
  70.             var randomX:Number = Math.random() * 600 - 300;
  71.             var randomY:Number = Math.random() * 200 - 100;
  72.             TweenLite.to(cube, 1, { x:randomX, y:randomY, ease:Quint.easeInOut});
  73.         }
  74.        
  75.         protected function commitProperties():void
  76.         {
  77.             //Set the properties of our camera
  78.             camera.zoom = 200;
  79.             camera.y    = 1000;
  80.         }
  81.         override protected function onRenderTick(event:Event = null):void
  82.         {
  83.             super.onRenderTick(event);
  84.             cube.yaw(2);
  85.         }
  86.     }
  87. }

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

6 Responses to “Advanced Interactivity (Without InteractiveScene3DEvent)”


  • when i click on the ‘click me’
    nothing happened,
    why?

  • It works well, I’ve tried. :)

  • Hi, im new to papervision and want to learn a few things about it. Could you tell me the purpose of a few things. If you have time of course.
    1: what is super, and why do people always access it when using papervision
    2:what is the purpose of the get and set functions in the texture AS file
    3:What is protected function, and how come the overide protected function works as an onEnterFrame would.

    Thanks

  • Hi, im new in papervision too.

    charlie i want to thank god at first and then thank you for the things you have done here for every single article and tutorials.

    Dex:

    1.super() mean that to call the base function

    2.get and set functions give you ability to do things that you are not able in a simple variable.imagine that you want to call another function when this happen : (scale = 2) see DisplayObject3D.as

    3.and protected attribute: Specifies that a variable, constant, method, or namespace is available only to the class that defines it and to any subclasses of that class. so the papervision decided to get us ability to override them and if we want to have those method coexists with our method functionality we use this as i mentioned : super.onRenderTick(event);

  • First of all, thank you for these introductory tutorials on Papervision. They have been a huge help in getting started. I have some specific questions about interactivity of dynamically loaded materials, such as you have in this tutorial. I noticed you are loading a MovieClip symbol from the Library. I assume that it is within the MovieClip’s code that you will find the functionality for the rollovers, and whatever other interactivity one might want. I have been setting up something very similar to this and have found that I can’t ever seem to get the mouse to change to the hand cursor when rolled over buttons or other elements that have Events attached to them. I’ve tried setting the buttonMode, mouseChildren, and useHandCursor properties in just about every combination possible (to the viewport, scene, materials, and actual elements inside the loaded MovieClip), but no luck so far. Is this a known issue? Is there a workaround for it? Or am I just doing something wrong? Any feedback would be greatly appreciated, thanks!

  • Sorry, have to add some things.
    super refers to the parent object. So when you have a class Bla extends Sprite and you want to refer to a function or property of the Sprite class, you use super.function() or super.property

    get and set are the two main things that hide behind the term encapsulation. Instead of making all your variables public to access them troughout your coding, you access them through getter and setter functions. This seems a little unnecessary but helps to prevent yourself from doing mistakes like accidentally overriding a variable which you didnt want to in the first place. there are more advantages but I forgot, computer classes are already dead past for me

    a protected function is a function that is meant to be overwritten with the keyword override. this is so you dont accidentally override the function by defining a function which coincidentially has the same name as a function which is already definied in the super class of your actual class. with that in mind, coders define functions they want to be overwritten troughout your coding and do this intentially with the keywords protected function… because you know that those functions are protected, you override them

Leave a Reply


Follow WovenCharlie on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed