Flex Away3d Add following tags to objects (picture tags, text labels)

Source: Internet
Author: User
Tags addchild

When playing a game, you will often see the effect of the tag following. such as the blood on the head, the name on the head of the monster. These labels will always follow the movement of the characters, and no matter how the characters turn, the label is always facing us.

The following example shows how to implement a picture, or the label of text to follow the effect.

1, add a picture label

In order to keep the picture always facing us, here we use the Sprite3d. and set it to offset, add to the corresponding Mesh can be.




package{
Import Flash.display.Sprite;
Import flash.display.StageAlign;
Import Flash.display.StageScaleMode;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Flash.geom.Vector3D;

Import Away3d.containers.View3D;
Import Away3d.controllers.HoverController;
Import Away3d.entities.Mesh;
Import Away3d.entities.Sprite3D;
Import Away3d.events.MouseEvent3D;
Import away3d.materials.TextureMaterial;
Import Away3d.primitives.CubeGeometry;
Import Away3d.tools.utils.Drag3D;
Import Away3d.utils.Cast;

[SWF (Framerate=, backgroundcolor= "#FFFFFF")]
public class S6 extends Sprite {

private Var _view3d:view3d;
Private var cameracontroller:hovercontroller;//360 panoramic display camera controller

Object picture (used as a texture for squares)
[Embed (source= "Assets/snow_diffuse.png")]
private Var Blockclass:class;

Floor picture (used as a texture for the floor)
[Embed (source= "assets/road.jpg")]
private Var Floorclass:class;

Alarm picture (used as following tag)
[Embed (source= "Assets/alert.png")]
public static Var Alertclass:class;

Object Material
private Var blockmaterial:texturematerial;

Floor Material
private Var floormaterial:texturematerial;

Alarm Material
private Var alertmaterial:texturematerial;

private Var _drag:drag3d;
Record whether the current is a mobile camera or a moving object
private var _movecamera:boolean = true;

private var _lastx:number = 0;
private var _lasty:number = 0;

Public Function S6 () {
Initengine ();
Initmaterials ();
Initobjects ();
Initlisteners ();
}

/**
* Initialize engine
*/
Private Function Initengine (): void
{
Stage.scalemode = Stagescalemode.no_scale;
Stage.align = Stagealign.top_left;

Create a viewport
_view3d = new View3D ();
_view3d.antialias = 4; Set anti-aliasing level

Initializing the camera
Cameracontroller = new Hovercontroller (_view3d.camera);
/*cameracontroller.distance = 1000;
Cameracontroller.mintiltangle = 0;
Cameracontroller.maxtiltangle = 90;
Cameracontroller.panangle = 45;*/
Cameracontroller.tiltangle = 15;

AddChild (_VIEW3D);
}

/**
* Initialization of the material
*/
Private Function Initmaterials (): void
{
The texture of the floor
floormaterial = new Texturematerial (Cast.bitmaptexture (Floorclass));
The texture of the square
blockmaterial = new Texturematerial (Cast.bitmaptexture (Blockclass));

Warning picture Texture
alertmaterial = new Texturematerial (Cast.bitmaptexture (Alertclass));
Alertmaterial.alpha = 1
alertmaterial.alphathreshold=0.5//Photo background transparent
}

/**
* Initialize Object
*/
Private Function initobjects (): void
{
Create a square (floor) in a three-dimensional stage
var Cube1:mesh = new Mesh (new Cubegeometry (M, m), floormaterial);
_view3d.scene.addchild (CUBE1);

Create a square on a three-dimensional stage (a square on the floor)
var Cube2:mesh = new Mesh (new Cubegeometry (M, m), blockmaterial);
Cube2.position = new Vector3D (0,35,0);
Turn on Mouse event support
Cube2.mouseenabled = true;
Cube2.addeventlistener (Mouseevent3d.mouse_down, Cubemousedown);
_view3d.scene.addchild (CUBE2);

Add a picture label to a box
var sprite3d:sprite3d = new Sprite3d (alertmaterial,64,64);
SPRITE3D.Y = 90;
Cube2.addchild (Sprite3d);

Add Drag to Box
_drag = new Drag3d (_view3d, Cube2, DRAG3D.PLANE_XZ);
_drag.planeposition = cube2.position;
}

Mouse Down
Private Function Cubemousedown (Event:mouseevent3d): void
{
_movecamera = false;
}

/**
* Initialization Monitoring
*/
Private Function Initlisteners (): void
{
AddEventListener (Event.enter_frame, _onenterframe);
Mouse Event Monitoring
Stage.addeventlistener (Mouseevent.mouse_down, OnMouseDown);
Stage.addeventlistener (mouseevent.mouse_up, onMouseUp);
Stage.addeventlistener (Mouseevent.mouse_wheel,onwheel);
Stage.addeventlistener (Event.resize, onResize);
OnResize ();
}

/**
* Render View
*/
Private Function _onenterframe (e:event): void
{
Render View
_view3d.render ();
}

/**
* Use stage size always full screen
*/
Private Function onResize (event:event = null): void
{
_view3d.width = Stage.stagewidth;
_view3d.height = Stage.stageheight;
}

/**
* Mouse Wheel Event
*/
Private Function Onwheel (e:mouseevent): void
{
if (E.delta > 0) {
if (Cameracontroller.distance < 1000)
Cameracontroller.distance + 100;
}else{
if (Cameracontroller.distance > 600)
Cameracontroller.distance-= 100;
}
}

/**
* Mouse Down Event
*/
Private Function OnMouseDown (event:mouseevent): void
{
if (_movecamera)
{
_view3d.stage.addeventlistener (Mouseevent.mouse_move, Mousemovehandler);
_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
}

/**
* Mouse Bounce Event
*/
Private Function OnMouseUp (event:mouseevent): void
{
_view3d.stage.removeeventlistener (Mouseevent.mouse_move, Mousemovehandler);
_movecamera = true;
}

/**
* Mouse Movement Events
*/
Private Function Mousemovehandler (event:mouseevent): void
{
if (_movecamera)
{
Mobile camera
var dx:number = _view3d.mousex-_lastx;
var dy:number = _view3d.mousey-_lasty;

Cameracontroller.panangle = + dx;
Cameracontroller.tiltangle + dy;

_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
Else
{
Move 3D Balls
_drag.updatedrag ();
}
}
}
}

2, add text labels
First create TextField to fill in the text, and then convert it to BitmapData. The following steps are the same as the picture labels.




package{
Import Flash.display.BitmapData;
Import Flash.display.Sprite;
Import flash.display.StageAlign;
Import Flash.display.StageScaleMode;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Flash.geom.Matrix;
Import Flash.geom.Vector3D;
Import Flash.text.TextField;
Import flash.text.TextFieldAutoSize;
Import Flash.text.TextFormat;

Import Away3d.containers.View3D;
Import Away3d.controllers.HoverController;
Import Away3d.entities.Mesh;
Import Away3d.entities.Sprite3D;
Import Away3d.events.MouseEvent3D;
Import away3d.materials.TextureMaterial;
Import Away3d.primitives.CubeGeometry;
Import Away3d.tools.utils.Drag3D;
Import Away3d.utils.Cast;

[SWF (Framerate=, backgroundcolor= "#FFFFFF")]
public class S7 extends Sprite {

private Var _view3d:view3d;
Private var cameracontroller:hovercontroller;//360 panoramic display camera controller

Object picture (used as a texture for squares)
[Embed (source= "Assets/snow_diffuse.png")]
private Var Blockclass:class;

Floor picture (used as a texture for the floor)
[Embed (source= "assets/road.jpg")]
private Var Floorclass:class;

Object Material
private Var blockmaterial:texturematerial;

Floor Material
private Var floormaterial:texturematerial;

Label material
private Var textmaterial:texturematerial;

private Var _drag:drag3d;
Record whether the current is a mobile camera or a moving object
private var _movecamera:boolean = true;

private var _lastx:number = 0;
private var _lasty:number = 0;

Public Function S7 () {
Initengine ();
Initmaterials ();
Initobjects ();
Initlisteners ();
}

/**
* Initialize engine
*/
Private Function Initengine (): void
{
Stage.scalemode = Stagescalemode.no_scale;
Stage.align = Stagealign.top_left;

Create a viewport
_view3d = new View3D ();
_view3d.antialias = 4; Set anti-aliasing level

Initializing the camera
Cameracontroller = new Hovercontroller (_view3d.camera);
/*cameracontroller.distance = 1000;
Cameracontroller.mintiltangle = 0;
Cameracontroller.maxtiltangle = 90;
Cameracontroller.panangle = 45;*/
Cameracontroller.tiltangle = 15;

AddChild (_VIEW3D);
}

/**
* Initialization of the material
*/
Private Function Initmaterials (): void
{
The texture of the floor
floormaterial = new Texturematerial (Cast.bitmaptexture (Floorclass));
The texture of the square
blockmaterial = new Texturematerial (Cast.bitmaptexture (Blockclass));

Add text labels to squares
var Text:textfield = new TextField ();
Text.autosize = Textfieldautosize.left;
Text.width = 128;
Text.height = 32;
Text.text = "I am a square";
var mytf:textformat=new TextFormat ();
Mytf.size = 19;
Mytf.color = 0x000000;
Mytf.font= "Blackbody";
Text.settextformat (MYTF);

Convert labels to bitmaps
var bmpwidth:int = 128;
var bmpheight:int = 32;
var textbmpdata:bitmapdata = new BitmapData (bmpwidth,bmpheight);
var Matrix:matrix = new Matrix ();
Matrix.tx = (bmpwidth-text.width)/2; X Direction offset
Matrix.ty = (bmpheight-text.height)/2; Y-direction offset
Textbmpdata.draw (Text,matrix);

Create label material
textmaterial = new Texturematerial (Cast.bitmaptexture (textbmpdata));
}

/**
* Initialize Object
*/
Private Function initobjects (): void
{
Create a square (floor) in a three-dimensional stage
var Cube1:mesh = new Mesh (new Cubegeometry (M, m), floormaterial);
_view3d.scene.addchild (CUBE1);

Create a square on a three-dimensional stage (a square on the floor)
var Cube2:mesh = new Mesh (new Cubegeometry (M, m), blockmaterial);
Cube2.position = new Vector3D (0,35,0);
Turn on Mouse event support
Cube2.mouseenabled = true;
Cube2.addeventlistener (Mouseevent3d.mouse_down, Cubemousedown);
_view3d.scene.addchild (CUBE2);

Add follow tag
var sprite3d:sprite3d = new Sprite3d (textmaterial,128,32);
SPRITE3D.Y = 70;
Sprite3d.scale (1);
Cube2.addchild (Sprite3d);

Add Drag to Box
_drag = new Drag3d (_view3d, Cube2, DRAG3D.PLANE_XZ);
_drag.planeposition = cube2.position;
}

Mouse Down
Private Function Cubemousedown (Event:mouseevent3d): void
{
_movecamera = false;
}

/**
* Initialization Monitoring
*/
Private Function Initlisteners (): void
{
AddEventListener (Event.enter_frame, _onenterframe);
Mouse Event Monitoring
Stage.addeventlistener (Mouseevent.mouse_down, OnMouseDown);
Stage.addeventlistener (mouseevent.mouse_up, onMouseUp);
Stage.addeventlistener (Mouseevent.mouse_wheel,onwheel);
Stage.addeventlistener (Event.resize, onResize);
OnResize ();
}

/**
* Render View
*/
Private Function _onenterframe (e:event): void
{
Render View
_view3d.render ();
}

/**
* Use stage size always full screen
*/
Private Function onResize (event:event = null): void
{
_view3d.width = Stage.stagewidth;
_view3d.height = Stage.stageheight;
}

/**
* Mouse Wheel Event
*/
Private Function Onwheel (e:mouseevent): void
{
if (E.delta > 0) {
if (Cameracontroller.distance < 1000)
Cameracontroller.distance + 100;
}else{
if (Cameracontroller.distance > 600)
Cameracontroller.distance-= 100;
}
}

/**
* Mouse Down Event
*/
Private Function OnMouseDown (event:mouseevent): void
{
if (_movecamera)
{
_view3d.stage.addeventlistener (Mouseevent.mouse_move, Mousemovehandler);
_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
}

/**
* Mouse Bounce Event
*/
Private Function OnMouseUp (event:mouseevent): void
{
_view3d.stage.removeeventlistener (Mouseevent.mouse_move, Mousemovehandler);
_movecamera = true;
}

/**
* Mouse Movement Events
*/
Private Function Mousemovehandler (event:mouseevent): void
{
if (_movecamera)
{
Mobile camera
var dx:number = _view3d.mousex-_lastx;
var dy:number = _view3d.mousey-_lasty;

Cameracontroller.panangle = + dx;
Cameracontroller.tiltangle + dy;

_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
Else
{
Move 3D Balls
_drag.updatedrag ();
}
}
}
}

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.