Traing_tree on 2010-10-29
tree
/**
* Copyright Toa.DH ( http://wonderfl.net/user/Toa.DH )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hTM0
*/
package{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.geom.Vector3D;
[SWF (width="550", height="500", backgroundColor="0xffffff", frameRate="30")]
public class Training3d_tree extends Sprite{
private var _rectWidth:Number = 100;
private var _rectHeight:Number = 100;
private var _totalNum:Number = 50;
private var _treeHeight:Number = 500;
private var _treeXpos:Number;
private var _treeStartY:Number;
private var _tweenArray:Array;
private var _itemArray:Array;
private var _branchArray:Array;
private var _itemIdArray:Array;
private var _treeSprite:Sprite;
private var _testArray:Array;
public function Training3d_tree():void{
stage.align = StageAlign.TOP_LEFT;
drawTree();
}
private function drawTree():void{
_treeXpos = stage.stageWidth/2;
_treeStartY = stage.stageHeight;
_treeSprite = new Sprite();
_treeSprite.x = _treeXpos;
_treeSprite.z = 0;
addChild(_treeSprite);
_itemArray = new Array();
//몸체
var rootSprite:Sprite = new Sprite();
rootSprite.graphics.beginFill(0x663333);
rootSprite.graphics.drawRect(0,0, 2, Math.round(Math.random() * _treeHeight + 100));
rootSprite.graphics.endFill();
rootSprite.y = stage.stageHeight-rootSprite.height;
_treeSprite.addChild(rootSprite);
for(var i:int=0; i<_totalNum; i++){
//가지
var branchColor:Number = Math.random()*0xffffff; //가지 색깔
var branchHeight:Number = Math.round(Math.random()*100)+150; //가지 길이
var branchY:Number = _treeStartY - Math.round((Math.random()*(rootSprite.height-100))+100);//가지 높이
var branchRotationY:Number = Math.random()*360; //가지rotationY:degree
var branchRotationZ:Number = Math.round(Math.random()*70) +10; //가지 기울기:degree
var tempAngle:Number = branchRotationZ/180*Math.PI; //가지 기울기값 :radian
var panelR:Number = Math.cos(tempAngle)*branchHeight; //패널이 가질 반지름, 가지의 기울기로 결정
var panelRadian = branchRotationY/180*Math.PI; //패널이 그릴 원의 각도:radian
var tempBranch:Shape = new Shape(); //기울기를 가지고 0값을 갖기위해 덮어씌움
tempBranch.graphics.beginFill(branchColor, 0.3);
tempBranch.graphics.drawRect(0, 0, branchHeight, 1);
tempBranch.rotation = -1*branchRotationZ;
var branch:Sprite = new Sprite();
branch.addChild(tempBranch);
branch.y = branchY;
branch.rotationY = branchRotationY; //degree
//판자
var panel:Sprite = new Sprite();
panel.graphics.beginFill(branchColor, 0.5);
panel.graphics.drawRect(-_rectWidth/2, -_rectHeight/2, _rectWidth, _rectHeight);
panel.graphics.endFill();
panel.y = branchY-Math.sin(branchRotationZ/180*Math.PI)*branchHeight; //가지 높이 - 가지 기울기에 의한 높이
panel.x = Math.cos(panelRadian)*panelR; //위에서 구한 radian으로 입체를 표현할 원을 그림
panel.z = -1*Math.sin(panelRadian)*panelR; //플래시 각도는 수학이랑 반대라서...(?)
panel.rotationY = branchRotationY+90; //가지가 갖고있는 degree + 90
_treeSprite.addChild(branch);
_treeSprite.addChild(panel);
_itemArray.push(panel);
}
sortItems();
addEventListener(Event.ENTER_FRAME, rotationEnterFrame);
}
private function sortItems():void{
_itemArray.sort(depthSort);
for(var i:int=0; i<_itemArray.length; i++){
_treeSprite.setChildIndex(_itemArray[i] as Sprite, i);
}
}
private function depthSort(objA:DisplayObject, objB:DisplayObject):int{
var posA:Vector3D = objA.transform.matrix3D.position;
posA = _treeSprite.transform.matrix3D.deltaTransformVector(posA);
var posB:Vector3D = objB.transform.matrix3D.position;
posB = _treeSprite.transform.matrix3D.deltaTransformVector(posB);
return posB.z - posA.z;
}
private function rotationEnterFrame(e:Event):void{
_treeSprite.rotationY = _treeSprite.rotationY%360 + 2;
sortItems();
}
}
}