Papervision3D: understanding Plane object – part 4

Understanding planes has never been so much fun, so here we go with the 4th part.

This is full of new features, such as:

* Making interactive objects

* Giving objects a name

* Changing object colors on the fly

This is the same old script as seen in Papervision3D: understanding Plane object – part 3 and previous tutorials, with some modifications.

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.events.InteractiveScene3DEvent;
	public class papervision extends Sprite {
		public var viewport:Viewport3D=new Viewport3D(500,400,false,true);
		public var scene:Scene3D = new Scene3D();
		public var camera:Camera3D = new Camera3D();
		public var renderer:BasicRenderEngine = new BasicRenderEngine();
		public var back_material:ColorMaterial=new ColorMaterial(Math.random()*0xffffff);
		public var front_material:ColorMaterial=new ColorMaterial(Math.random()*0xffffff);
		public var front_plane:Plane=new Plane(front_material,200,300,4,5);
		public var back_plane:Plane=new Plane(back_material,200,300,4,5);
		public var rotation_speed=4;
		public function papervision() {
			addChild(viewport);
			camera.focus=100;
			camera.zoom=10;
			back_plane.rotationY=180;
			front_material.interactive=true;
			back_material.interactive=true;
			scene.addChild(front_plane);
			scene.addChild(back_plane);
			front_plane.name = "FRONT";
			back_plane.name = "BACK";
			back_plane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,on_plane_clicked);
			front_plane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,on_plane_clicked);
			addEventListener(Event.ENTER_FRAME, render);
		}
		public function render(e:Event) {
			front_plane.yaw(rotation_speed);
			back_plane.yaw(rotation_speed);
			renderer.renderScene(scene, camera, viewport);
		}
		public function on_plane_clicked(e:InteractiveScene3DEvent) {
			rotation_speed*=-1;
			switch (e.displayObject3D.name) {
				case "FRONT" :
					front_material.fillColor=Math.random()*0xffffff;
					break;
				case "BACK" :
					back_material.fillColor=Math.random()*0xffffff;
					break;
			}
		}
	}
}

Let’s see what’s new:

Line 10: Importing InteractiveScene3DEvent class. It’s the class that will allow us to make objects interactive

Line 12: we also have to declare the viewport in a new way… we need the fourth parameter set to true in order to make the viewport interactive. Interactive objects inside a non-interactive viewport become non-interactive themselves.

Let’s see the four parameters:

viewportWidth:Number (default = 640) — Width of the viewport

viewportHeight:Number (default = 480) — Height of the viewport

autoScaleToStage:Boolean (default = false) — Determines whether the viewport should resize when the stage resizes

interactive:Boolean (default = false) — Determines whether the viewport should listen for Mouse events by creating an InteractiveSceneManager

Lines 16-17: This is the creation of a color material as seen in part 2, but this time I am rendering random colors

Lines 26-27: Making the planes interactive

Lines 30-31: Assigning names to both planes.

Lines 32-33: Adding the listener to planes. InteractiveScene3DEvent.OBJECT_PRESS works in the same way as MouseEvent.CLICK

Now let’s see what happens when I click on a plane:

Line 42: Changing the rotation speed of both planes

Lines 43-50: Retrieving the name of the plane I clicked, and according to its name changing the color of the front or back plane. Please notice I am not changing the plane material, but I am changing the material fill color.

And that’s it:

Click on the rotating plane to see what happens.

Again, just cut/paste the code in the file you can download at Papervision3D for the absolute beginners.

Next time, real gambling cards flipping…

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers