Flex combined with Sandy engine Creation

Source: Internet
Author: User

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "Init ()"> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import MX. core. uicomponent; <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. *; </P> <p> private var scenne: scene3d; <br/> private var camera: camera3d; <br/> private var rAny: Number = 0.5; <br/> private var box: box; </P> <p> private function Init (): void <br/>{< br/> var UI: uicomponent = new uicomponent (); // create a component <br/> var sprite: SPRITE = new sprite (); <br/> UI. addchild (sprite); <br/> canvas. addchild (UI); // Add a container <br/> camera = new camera3d (300,300); // create a camera <br/> camera. z =-400; <br/> var root: Group = createscene (); <br/> scenne = new scene3d ("scene1", Sprite, 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", 100,100,100); <br/> var materialattr: materialattributes = new materialattributes (<br/> New lineattributes (0.5, 0x2111bb, 0.4), <br/> New lightattributes (true, 0.1) <br/>); // create material attributes, linear light, </P> <p> var material: material = new colormaterial (0xffcc33, 1, materialattr); // create a color material <br/> material. lightingenable = true; <br/> var app: appearance = new appearance (material); // create a material surface for an object </P> <p> box. rotatex = 10; <br/> box. rotatey = 0; <br/> box. appearance = app; // specify the material surface <br/> G. addchild (box); <br/> box. rotatex = 30; <br/> box. rotatey = 30; <br/> G. addchild (box); <br/> return g; </P> <p >}</P> <p> private function run (Event: Event ): void <br/> {<br/> scenne. render (); // Rendering scenario <br/> box. rotatex + = 1; // Let the cube rotate on the X axis <br/> box. rotatey + = 1; // Let the cube rotate on the Y axis </P> <p >}</P> <p>] --> <br/> </MX: SCRIPT> <br/> <mx: canvas id = "canvas" x = "72" Y = "66" width = "411" Height = "359"> <br/> </MX: canvas> </P> <p> </MX: Application> <br/>

 

Using flex combined with the sandy engine to create some 3D objects is good.

 

 

Procedure:

Import files related to the sandy engine to the flex project.

 

During initialization, we first drag a canvas container in the scenario and add sub-components to the container.

VaR UI: uicomponent = new uicomponent (); // create a component <br/> var sprite: SPRITE = new sprite (); <br/> UI. addchild (sprite); <br/> canvas. addchild (UI); // Add a container

 

Note:

Scenne = new scene3d ("scene1", Sprite, camera, root );

The second parameter. When developing with Flash IDE, we only use this to specify the second parameter. This indicates a reference to a Sprite class object.

Scenne = new scene3d ("scene1", this, camera, root); // create a scenario with a camera

 

The following is a comparison of how to use ide To create a cube:

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. *; </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 (400,400); // set the camera size and position <br/> camera. z =-300; <br/> var root: Group = createscene (); </P> <p> scenne = new scene3d ("scene1", this, camera, root); // create a scenario with a camera <br/> addeventlistener (event. enter_frame, run); </P> <p >}< br/> private function createscene (): group <br/>{< br/> var G: group = new group (); <br/> box = new box ("box", 100,100,100); <br/> var materialattr: materialattributes = new materialattributes (<br/> New lineattributes (0.5, 0x2111bb, 0.4), <br/> New lightattributes (true, 0.1) <br/> ); // create material attributes, linear light, </P> <p> var material: Material = new colormaterial (0xffcc33, 1, materialattr ); // create a color material <br/> material. lightingenable = true; <br/> var app: appearance = new appearance (material); // create a material surface for an object </P> <p> box. rotatex = 10; <br/> box. rotatey = 0; <br/> box. appearance = app; // specify the material surface <br/> G. addchild (box); <br/> return g; </P> <p >}< br/> private function run (Event: Event ): void <br/> {<br/> scenne. render (); <br/> box. rotatex + = 1; // Let the cube rotate on the X axis <br/> box. rotatey + = 1; // Let the cube rotate on the Y axis </P> <p >}< br/>}

 

The difference is that the sprite class objects are learned above.

To display the cube we created in flex. First, you must specify a Sprite object.

 

If not, the following error occurs:

Failed to forcibly convert the type: Flash. display: SPRITE @ 1726041 cannot be converted to MX. Core. iuicomponent.

 

Because mx: application itself is a component, if you are using addchild to create a subitem, that is

VaR sprite: SPRITE = new sprite ();
Addchild (sprite );

The sprite object is already in the application component, but the Flash. Display sprite object cannot be added to the application.

 

To display the created cube, we define a component and add the sprite object to the container as its sub-project.

The canvas container also adds the self-created components to the container as the parent container.

 

VaR UI: uicomponent = new uicomponent (); // create a component
VaR sprite: SPRITE = new sprite ();
Ui. addchild (sprite );
Canvas. addchild (UI); // Add a container

 

Canvas container ---> UI ----> -- Sprite

This will show the effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.