本文共 1892 字,大约阅读时间需要 6 分钟。
package com.ddd
{importflash.display.Sprite;importflash.display.StageScaleMode;importflash.events.Event;/**
* 3d多物体点旋转
* @author Administrator
**/
public class RotateY extendsSprite
{private var balls:Array=[];private var num:int=10;private var vpX:Number=stage.stageWidth / 2;private var vpY:Number=stage.stageHeight / 2;private var fl:Number=250;public functionRotateY()
{super();
stage.scaleMode=StageScaleMode.NO_SCALE;
init();
}private functioninit():void
{for (var i:int=0; i < num; i++)
{var ball:Ball=new Ball;
this.addChild(ball);
ball.xpos=Math.random() * 200 - 100;
ball.ypos=Math.random() * 200 - 100;
ball.zpos=Math.random() * 200 - 100;
balls.push(ball);
}
this.addEventListener(Event.ENTER_FRAME, onEnterFrame)
}private functiononEnterFrame(e:Event):void
{//注意这里,这是朝着鼠标坐标方向的运动方式
var angleY:Number=(mouseX - vpX) * .001;var angleX:Number=(mouseY - vpY) * .001;for (var i:int=0; i < num; i++)
{var ball:Ball=balls[i];
rotateY(ball, angleY);
rotateX(ball, angleX);
dopetive(ball);
}
sortZ();
}private function rotateY(ball:Ball, _angle:Number):void
{var xp:Number=Math.cos(_angle) * ball.xpos - Math.sin(_angle) *ball.zpos;var zp:Number=Math.cos(_angle) * ball.zpos + Math.sin(_angle) *ball.xpos;
ball.xpos=xp;
ball.zpos=zp;
}private function rotateX(ball:Ball, _angle:Number):void
{var yp:Number=Math.cos(_angle) * ball.ypos - Math.sin(_angle) *ball.zpos;var zp:Number=Math.cos(_angle) * ball.zpos + Math.sin(_angle) *ball.ypos
ball.ypos=yp;
ball.zpos=zp;
}private functiondopetive(ball:Ball):void
{if (ball.zpos > -fl)
{var scale:Number=fl / (fl +ball.zpos);
ball.scaleX=ball.scaleY=scale;
ball.x=vpX + ball.xpos *scale;
ball.y=vpY + ball.ypos *scale;
ball.visible=true;
}else{
ball.visible=false;
}
}private functionsortZ():void
{
balls.sortOn("zpos", Array.DESCENDING |Array.NUMERIC);for (var i:int=0; i < num; i++)
{var ball:Ball=balls[i];
this.setChildIndex(ball, i)
}
}
}
}
转载地址:http://hcima.baihongyu.com/