removeChild

Switching parents at runtime

Wednesday, November 19th, 2008 | examples | Comments

This example comes from a conversation I had with Andy. You can actually do a lot of neat tricks by swapping parents, especially when you start rotating the parents.


source

package
{
	import gs.TweenMax;
 
	import org.papervision3d.core.math.Matrix3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class SwitchingParentsAtRuntime extends BasicView
	{
		private var parent1:Sphere;
		private var parent2:Sphere;
		private var childSphere:Sphere;
 
		private var light:PointLight3D;
 
		public function SwitchingParentsAtRuntime()
		{
			var headerText:headerContainer = new headerContainer();
			headerText.header.text = "Click a red sphere to set as parent";
			addChild(headerText);
 
			viewport.interactive = true;
			light = new PointLight3D();
 
			var parentMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000);
			parentMaterial.interactive = true;
			var childMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0x00cc00);
 
			parent1 = new Sphere(parentMaterial, 300, 10, 10);
			parent1.x = -500;
			parent1.y = 500;
			parent1.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, objectPressHandler);
			parent1.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, objectOverHandler);
			parent1.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, objectOutHandler);
 
			parent2 = new Sphere(parentMaterial, 300, 10, 10);
			parent2.x = 500;
			parent2.y = -500;
			parent2.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, objectPressHandler);
			parent2.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, objectOverHandler);
			parent2.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, objectOutHandler);
 
			childSphere = new Sphere(childMaterial, 100, 10, 10);
			childSphere.name = "child";
 
			scene.addChild(parent1);
			scene.addChild(parent2);
			parent1.addChild(childSphere);
 
			childSphere.x = 500;
 
			TweenMax.to(parent1, 2, {y:-500, yoyo:true});
			TweenMax.to(parent2, 2, {y:500, yoyo:true});
 
			startRendering();
		}
 
		private function objectPressHandler(event:InteractiveScene3DEvent):void
		{
			var parentSphere:Sphere = Sphere(event.target);
			if(childSphere.parent == parentSphere)
			{
				//do nothing
			}else
			{
				var childSphereWorldMatrix:Matrix3D = childSphere.world;
				var inverse:Matrix3D = new Matrix3D();
				var tempParentMatrix:Matrix3D = new Matrix3D();
 
				inverse.calculateInverse(parentSphere.world);
 
				tempParentMatrix.calculateMultiply(inverse, childSphereWorldMatrix);
 
				childSphere.copyTransform(tempParentMatrix);
				//get the parent of the child to
				//remove the child from the parent :)
				childSphere.parent.removeChild(childSphere);
				parentSphere.addChild(childSphere);
			}
		}
 
		private function objectOverHandler(event:InteractiveScene3DEvent):void
		{
			viewport.buttonMode = true;	
		}
 
		private function objectOutHandler(event:InteractiveScene3DEvent):void
		{
			viewport.buttonMode = false;	
		}
	}
}

Tags: , , ,

Search

Recommended Books

Speaking at FITC Toronto

 

May 2012
M T W T F S S
« May    
 123456
78910111213
14151617181920
21222324252627
28293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...