Flex in the Away3d object drag and drop movement (mesh up and down mouse drag to change objects)

Source: Internet
Author: User
Tags addchild

The use of drag3d can be a convenient implementation of the object's drag and drop, below through the sample demo.

Implementation features:
(1) on the stage background, hold down the mouse and drag, change camera angle.
(2) Hold the mouse and drag on the square, then move the square position.
(3) The default square in the XY surface move, press the keyboard 1, 2, 3 keys, mobile face should be renamed XY, XZ, Zy on the move.
(4) To facilitate positioning, add axes on the stage Trident
The effect chart is as follows:


The code is as follows

The code is as follows Copy Code

package{
Import Flash.display.Sprite;
Import flash.display.StageAlign;
Import Flash.display.StageScaleMode;
Import flash.events.Event;
Import flash.events.KeyboardEvent;
Import flash.events.MouseEvent;
Import Flash.ui.Keyboard;

Import Away3d.containers.View3D;
Import Away3d.controllers.HoverController;
Import away3d.debug.Trident;
Import Away3d.entities.Mesh;
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

[Embed (source= "assets/cubetexture3.jpg")]
private Var Cubetextureclass:class;

private Var cubetexturematerial: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;
private var _scale:number = 1000;

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.tiltangle = 15;

AddChild (_VIEW3D);
}

/**
* Initialization of the material
*/
Private Function Initmaterials (): void
{
cubetexturematerial = new Texturematerial (Cast.bitmaptexture (Cubetextureclass));
}

/**
* Initialize Object
*/
Private Function initobjects (): void
{
Add coordinate system
var trident:trident = new Trident (500);
_view3d.scene.addchild (Trident);

Create a square in a three-dimensional stage
var Cube1:mesh = new Mesh (new Cubegeometry (1, 1, 1, false),
Cubetexturematerial);
_view3d.scene.addchild (CUBE1);

Turn on Mouse event support
Cube1.mouseenabled = true;
Add listening
Cube1.addeventlistener (Mouseevent3d.mouse_over, cubemouseover);
Cube1.addeventlistener (Mouseevent3d.mouse_out, cubemouseout);
Cube1.addeventlistener (Mouseevent3d.mouse_down, Cubemousedown);

Add Drag to Box
_drag = new Drag3d (_view3d, Cube1, Drag3d.plane_xy);
}

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

Mouse move into
Private Function Cubemouseover (Event:mouseevent3d): void{
var Mesh:mesh = event.object as mesh;
Mesh.showbounds = true;
}

Mouse move out
Private Function Cubemouseout (Event:mouseevent3d): void{
var Mesh:mesh = event.object as mesh;
Mesh.showbounds = 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);
Stage.addeventlistener (keyboardevent.key_up, Keyuphandler);
OnResize ();
}

The keyboard responds to events and the control blocks move in a plane area
Private Function Keyuphandler (event:keyboardevent): void
{
Switch (Event.keycode)
{
Case keyboard.number_1:
_drag.plane = Drag3d.plane_xy;
Break
Case keyboard.number_2:
_drag.plane = DRAG3D.PLANE_XZ;
Break
Case Keyboard.number_3:
_drag.plane = Drag3d.plane_zy;
Break
}
}

/**
* 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 (event:mouseevent): void
{
if (_movecamera)
{
var dist:number = CAMERACONTROLLER.DISTANCE/25;
if (Event.delta > 0)
{
_scale-= dist;
}
Else
{
_scale + = dist;
}
if (_scale > 40000)
{
_scale = 40000;
}
if (_scale < 100)
{
_scale = 100;
}
Cameracontroller.distance = _scale;
}
}

/**
* 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.