A long time ago, we could use Sandy's camera to move things, observe things, and use a keyboard to control the movement and rotation of the camera. Today, we try to interact with the mouse to control the object of Sandy through the mouse interaction.
I. Objectives
In the core programming of the display class, the sprite class occupies a large seat. By listening to The mouseevent, we can implement mouse interaction. In the same way, we can create a cube object to interact with each other by clicking on the object.
Ii. inheritance relationship:
Before taking a look at the demo, let's first take a look at the inheritance relationship of the box class.
Package |
Sandy. Primitive |
Class |
Public class box |
Inheritance |
Box ---> shape3d ----> atransformable ---> node |
Implements |
Primitive3d |
Since the box class inherits the shape3d class, we can use this class to find a shape3d container attribute. Through this class, we can return the sprite object we want.
|
Container: Sprite[Read-Only] The contianer for this object |
public function get container():Sprite
That is to say, through the help of this attribute, we can achieve some mouse interaction.
For example
Box. Container. buttonmode = true;
Box. Container. addeventlistener (mouseevent. Click, clickhandler );
In this way, the mouse can interact with the object created by sadny.
Simple Demonstration:
Package <br/> {<br/> Import flash. display. sprite; <br/> Import flash. events. *; <br/> Import Sandy. core. scene3d; <br/> Import Sandy. core. scenegraph. *; <br/> Import Sandy. primitive. *; <br/> Import Sandy. core. data. *; <br/> Import Sandy. materials. *; <br/> Import Sandy. materials. attributes. *; <br/> Import Sandy. primitive. *; </P> <p> public class my3d2 extends sprite <br/>{< br/> private var scenne: scene3d; <br/> private var camera: camera3d; <br/> private var rAny: Number = 0.5; <br/> private var box: box; </P> <p> Public Function my3d2 () <br/> {<br/> camera = new camera3d (300,300); <br/> camera. z =-400; <br/> camera. lookat (0, 0); </P> <p> var root: Group = createscene (); <br/> scenne = new scene3d ("scene1", this, camera, root); <br/> addeventlistener (event. enter_frame, run); <br/>}< br/> private function createscene (): group <br/>{< br/> var G: Group = new group (); <br/> box = new box ("box", 50, 50); <br/> box. X =-20; <br/> box. y = 0; <br/> box. z =-100; <br/> box. rotatex = 50; <br/> G. addchild (box); </P> <p> var materialattr: materialattributes = new materialattributes (<br/> New lineattributes (0.5, 0x2111bb, 0.4 ), <br/> New lightattributes (true, 0.1) <br/>); </P> <p> var material: Material = new colormaterial (0x0000ff, 1, materialattr ); <br/> material. lightingenable = true; <br/> var app: appearance = new appearance (material); <br/> box. appearance = app; <br/> box. container. buttonmode = true; // mouse interaction <br/> box. container. addeventlistener (mouseevent. click, clickhandler); // box listener <br/> return g; </P> <p >}< br/> private function run (Event: Event ): void <br/> {<br/> scenne. render (); </P> <p >}< br/> private function clickhandler (Event: mouseevent ): void <br/>{</P> <p> trace ("You clicked me"); <br/>}< br/>}
In fact, you can add a reference attribute to the mouse to listen.
Effect:
Continue with the above program to copy three graphs in the scenario and listen to them. Color Texture the three cubes.
Package <br/> {<br/> Import flash. display. sprite; <br/> Import flash. events. *; <br/> Import Sandy. core. scene3d; <br/> Import Sandy. core. scenegraph. *; <br/> Import Sandy. primitive. *; <br/> Import Sandy. core. data. *; <br/> Import Sandy. materials. *; <br/> Import Sandy. materials. attributes. *; <br/> Import Sandy. primitive. *; </P> <p> public class my3d extends sprite <br/> {<br/> private var scenne: scene3d; <br/> private var camera: camera3d; <br/> private var rAny: Number = 0.5; <br/> private var box: box; </P> <p> Public Function my3d () <br/> {<br/> camera = new camera3d (300,300); <br/> camera. z =-400; <br/> camera. lookat (0, 0); </P> <p> var root: Group = createscene (); <br/> scenne = new scene3d ("scene1", this, camera, root); <br/> addeventlistener (event. enter_frame, run); <br/>}< br/> private function createscene (): group <br/>{< br/> var G: Group = new group (); <br/> box = new box ("box", 50, 50); <br/> box. X =-20; <br/> box. y = 0; <br/> box. z =-100; <br/> box. rotatex = 50; </P> <p> var box2: box = new box ("box2", 50, 50); <br/> box2.x = 50; <br/> box2.y = 0; <br/> box2.z =-100; <br/> box2.rotatex = 50; </P> <p> var box3: box = new box ("box3", 50, 50); <br/> box3.x = 120; <br/> box3.y = 0; <br/> box3.z =-100; <br/> box3.rotatex = 50; </P> <p> G. addchild (box); <br/> G. addchild (box2); <br/> G. addchild (box3); <br/> var materialattr: materialattributes = new materialattributes (<br/> New lineattributes (0.5, 0x2111bb, 0.4), <br/> New lightattributes (true, 0.1) <br/>); </P> <p> var material: Material = new colormaterial (0x0000ff, 1, materialattr); <br/> var material: material = new colormaterial (0x00ff00, 1, materialattr); <br/> var materi_3: Material = new colormaterial (0xff0000, 1, materialattr); <br/> material. lightingenable = true; <br/> material2.lightingenable = true; <br/> material3.lightingenable = true; <br/> var app: appearance = new appearance (material ); <br/> var app2: appearance = new appearance (material); <br/> var app3: appearance = new appearance (materi_3); <br/> box. appearance = app; <br/> box2.appearance = app2; <br/> box3.appearance = app3; </P> <p> box. container. buttonmode = true; <br/> box2.container. buttonmode = true; <br/> box3.container. buttonmode = true; <br/> box. container. addeventlistener (mouseevent. click, clickhandler); <br/> box2.container. addeventlistener (mouseevent. click, clickhandler2); <br/> box3.container. addeventlistener (mouseevent. click, clickhandler3); <br/> return g; </P> <p >}< br/> private function run (Event: Event ): void <br/> {<br/> scenne. render (); </P> <p >}< br/> private function clickhandler (Event: mouseevent ): void <br/>{</P> <p> trace ("the first one you clicked"); <br/>}< br/> private function clickhandler2 (event: mouseevent): void <br/>{</P> <p> trace ("second you clicked me "); <br/>}< br/> private function clickhandler3 (Event: mouseevent ): void <br/>{</P> <p> trace ("third you clicked me"); <br/>}< br/>}
In this way, we can interact with them more effectively, such as links or other interesting textures.