강력한 pv3d로 상호작용 가능한 입방체 만들기
이전에oo를 이해하지 못할 때 썼는데 코드가 깨끗하지 않아요.
package
{
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import gs.TweenLite;
import gs.easing.Back;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.materials.special.CompositeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.view.BasicView;
/**
*
*/
[SWF(backgroundColor = 0)]
public class JdBox extends BasicView
{
private var box:Cube;
private var plane : Plane;
private var plane_line_top: Plane;
private var plane_line_bottom : Plane;
private var red : ColorMaterial;
private var line:WireframeMaterial;
private var redZu:CompositeMaterial;
private var material:BitmapMaterial;
private var planeLineList:Array=new Array();
private var planeList : Array = new Array();
public function JdBox()
{
super();
stage.frameRate = 40;
init();
startRendering();
}
private function init():void
{
initBox();
setDefaultRotation();
initPlanes();
interactiveBox();
}
private function initBox():void
{
var rect:Shape = new Shape();
rect.graphics.beginFill(0xFF0000,.5);
rect.graphics.lineStyle(2,0x000000,1);
rect.graphics.drawRect(0,0,300,300);
rect.graphics.endFill();
var bmp:BitmapData = new BitmapData(300,300,true,0x0);
bmp.draw(rect);
material = new BitmapMaterial(bmp);
material.doubleSided = true;
// line = new WireframeMaterial(0x000000,0.3,1);
//
// red = new ColorMaterial(0xFF0000,.3);
//
// redZu = new CompositeMaterial();
// redZu.doubleSided = false;
// redZu.addMaterial(red);
// redZu.addMaterial(line);
var materialsList : MaterialsList = new MaterialsList();
materialsList.addMaterial(material,"front");
// materialsList.addMaterial(material,"back");
// materialsList.addMaterial(material,"left");
// materialsList.addMaterial(material,"right");
// materialsList.addMaterial(material,"top");
// materialsList.addMaterial(material,"bottom");
box = new Cube(materialsList,300,300,300);
scene.addChild(box);
}
private function initPlanes():void
{
//----top----planeList[0]-----planeLineList[0]-----initPlaneLine[,,,,0,,,]
initSinglePlane( new Plane(material,300,300),0,150,0,0,0,0);
initPlaneLine(new Plane(material,300,1),0,150,150,0,90,0,0);
//----bottom----planeList[1]-----planeLineList[1]-----initPlaneLine[,,,,1,,,]
initSinglePlane( new Plane(material,300,300),0,-150,0,0,0,0);
initPlaneLine(new Plane(material,300,1),0,-150,-150,1,90,0,0);
//----left----planeList[2]-----planeLineList[2]-----initPlaneLine[,,,,2,,,]
initSinglePlane( new Plane(material,300,300),-150,0,0,0,0,90);
initPlaneLine(new Plane(material,1,300),-150,0,150,2,0,90,0);
//----right----planeList[3]-----planeLineList[3]-----initPlaneLine[,,,,3,,,]
initSinglePlane( new Plane(material,300,300),150,0,0,0,0,90);
initPlaneLine(new Plane(material,1,300),150,0,-150,3,0,90,0);
//----front----planeList[4]-----planeLineList[4]-----initPlaneLine[,,,,4,,,]
initSinglePlane( new Plane(material,300,300),0,150,0,0,0,0);
initPlaneLine(new Plane(material,300,1),0,-150,-150,4,0,0,0);
interactivePlane();
}
private function initSinglePlane(tmpPlane : Plane,
x : int,y : int,z : int,
rotX : int,rotY : int,rotZ : int):void
{
tmpPlane.x = x;
tmpPlane.y = y;
tmpPlane.z = z;
tmpPlane.localRotationX = rotX;
tmpPlane.localRotationY = rotY;
tmpPlane.localRotationZ = rotZ;
planeList.push(tmpPlane);
}
private function initPlaneLine(planeLine : Plane ,
x : int , y : int , z : int,
planeindex:int,
rotX : int,rotY : int,rotZ : int):void
{
planeLine.x += x;
planeLine.y += y;
planeLine.z += z;
planeLine.localRotationX = rotX;
planeLine.localRotationY = rotY;
planeLine.localRotationZ = rotZ;
planeLine.addChild(planeList[planeindex]);
planeLineList.push(planeLine);
box.addChild(planeLine);
}
private function setDefaultRotation():void
{
box.localRotationX = 15;
box.localRotationY = -30;
box.localRotationZ = 0;
}
private function interactiveBox():void
{
viewport.interactive = true;
// redZu.interactive = true;
material.interactive = true;
box.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,onOverHandler);
box.addEventListener(InteractiveScene3DEvent.OBJECT_OUT,onOutHandler);
box.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onClickHandler);
}
private function interactivePlane():void
{
for(var i:int=0; i < planeList.length;i++){
planeList[i].addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,planeClickHandler);
}
}
/**
* plane, , plane
*/
private function planeClickHandler(event : InteractiveScene3DEvent):void
{
rotateBox(event.target as Plane);
rotateCrtPlane(event.target as Plane);
}
private function rotateBox(tmpPlane : Plane):void
{
var tmpIndex:int=planeList.indexOf(tmpPlane,0);
if(tmpIndex == 3){
TweenLite.to(box,.2,{localRotationY:box.localRotationY-90,
onComplete:onFinishTween1});
// TweenLite.to(clip_mc, 5, {alpha:0.5, x:120, ease:Back.easeOut, delay:2,
// onComplete: onFinishTween, onCompleteParams:[5, clip_mc]});
}
}
private function onFinishTween1():void
{
TweenLite.to(box,.2,{localRotationZ:box.localRotationZ-90,
onComplete:onFinishTween2});
}
private function onFinishTween2():void
{
trace("finish 2");
TweenLite.to(box,.2,{localRotationX:box.localRotationX-90});
}
private function rotateCrtPlane(tmpPlane : Plane):void
{
var tmpIndex:int=planeList.indexOf(tmpPlane,0);
var tmpPlaneLine : Plane = planeLineList[tmpIndex];
switch(tmpIndex){
case 0: //top ,the same to bottom
case 1: //bottom
switch(tmpPlaneLine.localRotationX){
case 0:
TweenLite.to(tmpPlaneLine,2,{localRotationX:90,ease:Back.easeOut});
break;
case 90:
TweenLite.to(tmpPlaneLine,2,{localRotationX:0,ease:Back.easeOut});
break;
}
break;
case 2: //left
case 3: //right
switch(tmpPlaneLine.localRotationY){
case 0:
TweenLite.to(tmpPlaneLine,2,{localRotationY:90,ease:Back.easeOut});
break;
case 90:
TweenLite.to(tmpPlaneLine,2,{localRotationY:0,ease:Back.easeOut});
break;
}
break;
case 4: //front
switch(tmpPlaneLine.localRotationX){
case 0:
TweenLite.to(tmpPlaneLine,2,{localRotationX:90,ease:Back.easeOut});
break;
case 90:
TweenLite.to(tmpPlaneLine,2,{localRotationX:0,ease:Back.easeOut});
break;
}
break;
}
}
private function onOverHandler(event : InteractiveScene3DEvent):void
{
trace("ok");
viewport.buttonMode = true;
// box.materials.getMaterialByName("back").fillAlpha = .8;
}
private function onOutHandler(event : InteractiveScene3DEvent):void
{
trace("no");
viewport.buttonMode = false;
// box.materials.getMaterialByName("back").fillAlpha = .3;
}
/**
* , plane
*/
private function onClickHandler(event : InteractiveScene3DEvent):void
{
randomRotateBox();
// rotationBox();
}
private function randomRotateBox():void
{
var tmpNum : Number = Math.random();
if(tmpNum > .6){
TweenLite.to(box,2,{localRotationX:box.localRotationX+50,ease:Back.easeOut});
}else if(tmpNum < .3){
TweenLite.to(box,2,{localRotationY:box.localRotationY+50,ease:Back.easeOut});
}else{
TweenLite.to(box,2,{localRotationZ:box.localRotationZ+50,ease:Back.easeOut});
}
}
private function rotationBox():void
{
TweenLite.to(box,2,{rotationX :box.rotationX +50,ease:Back.easeOut});
// box.yaw(30);
}
// override protected function onRenderTick(e:Event=null):void
// {
// //box.localRotationX++;
// //box.localRotationY++;
// //box.localRotationZ++;
// super.onRenderTick();
// }
}
}
전재 대상:https://www.cnblogs.com/JD85/archive/2010/05/09/1730923.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.