很久没有来这里写文章里,csdn的服务器终于优化好了。这次又可以继续发表我的flash文章了。
之前看了一本书,把里面的程序调试成功了,现在可以运行制作一个三维的效果。还是不错的程序
cude.as
package tom{
import flash.display.Sprite;
import flash.events.*;
import flash.display.Graphics;
public class cude extends Sprite {
private var points:Array=new Array();
private var vpX:Number=stage.stageWidth/2;
private var vpY:Number=stage.stageHeight/2;
private var numPoints:int=8;
private var triangles:Array;
public function cude() {
points[0] = new Point3D(-100, -100, -100);
points[1] = new Point3D( 100, -100, -100);
points[2] = new Point3D( 100, 100, -100);
points[3] = new Point3D(-100, 100, -100);
points[4] = new Point3D(-100, -100, 100);
points[5] = new Point3D( 100, -100, 100);
points[6] = new Point3D( 100, 100, 100);
points[7] = new Point3D(-100, 100, 100);
for (var i:int = 0; i < numPoints; i++) {
points[i].setVanishingPoint(vpX, vpY);
points[i].setCenter(0, 0, 200); }
triangles=new Array();
triangles[0] = new Triangle(points[0], points[1],points[2], 0x6666cc);
triangles[1] = new Triangle(points[0], points[2],points[3], 0x6666cc); // top
triangles[2] = new Triangle(points[0], points[5],points[1], 0x66cc66);
triangles[3] = new Triangle(points[0], points[4],points[5], 0x66cc66); //back
triangles[4] = new Triangle(points[4], points[6],points[5], 0xcc6666);
triangles[5] = new Triangle(points[4], points[7],points[6], 0xcc6666); // bottom
triangles[6] = new Triangle(points[3], points[2],points[6], 0xcc66cc);
triangles[7] = new Triangle(points[3], points[6],points[7], 0xcc66cc); // right
triangles[8] = new Triangle(points[1], points[5],points[6], 0x66cccc);
triangles[9] = new Triangle(points[1], points[6],points[2], 0x66cccc); // left
triangles[10] =new Triangle(points[4], points[0],points[3], 0xcccc66);
triangles[11] =new Triangle(points[4], points[3],points[7], 0xcccc66);
addEventListener(Event.ENTER_FRAME, onEnterFrame); }
private function onEnterFrame(event:Event):void {
var angleX:Number = (mouseY - vpY) * .001;
var angleY:Number = (mouseX - vpX) * .001;
for (var i:int = 0; i < numPoints; i++) {
var point:Point3D = points[i];
point.rotateX(angleX);
point.rotateY(angleY); }
graphics.clear();
for (var k:int = 0; k < triangles.length; k++) {
triangles[k].draw(graphics);
} } } }
Point3D.as
package tom{
import flash.display.Sprite; i
mport flash.events.*;
public class Point3D {
public var fl:Number = 250;
private var vpX:Number=0;
private var vpY:Number=0;
private var cX:Number = 0;
private var cY:Number = 0;
private var cZ:Number = 0;
public var X:Number=0;
public var Y:Number=0;
public var Z:Number=0;
public function Point3D(X:Number,Y:Number,Z:Number)
{ this.X=X; this.Y=Y; this.Z=Z;
}
public function setCenter(cX:Number,cY:Number,cZ:Number=0):void {
this.cX = cX; this.cY = cY; this.cZ = cZ; }
public function setVanishingPoint(vpX:Number, vpY:Number):void {
this.vpX = vpX; this.vpY = vpY; }
public function get screenX():Number {
var scale:Number = fl / (fl + Z + cZ);
return vpX + cX + X * scale; }
public function get screenY():Number {
var scale:Number = fl / (fl + Z + cZ);
return vpY + cY + Y * scale;
} public function rotateX(angleX:Number):void {
var cosX:Number = Math.cos(angleX);
var sinX:Number = Math.sin(angleX);
var y1:Number = Y * cosX - Z * sinX;
var z1:Number = Z* cosX + Y * sinX;
Y = y1; Z= z1; }
public function rotateY(angleY:Number):void {
var cosY:Number = Math.cos(angleY);
var sinY:Number = Math.sin(angleY);
var x1:Number = X * cosY - Z * sinY;
var z1:Number = Z * cosY + X * sinY;
X = x1; Z = z1; }
public function rotateZ(angleZ:Number):void {
var cosZ:Number = Math.cos(angleZ);
var sinZ:Number = Math.sin(angleZ);
var x1:Number = X * cosZ - Y * sinZ;
var y1:Number = Y * cosZ + X * sinZ; X = x1; Y= y1; } } }
Triangle.as
package tom{
import flash.display.Graphics;
public class Triangle {
private var pointA:Point3D;
private var pointB:Point3D;
private var pointC:Point3D;
private var color:uint;
public function Triangle(a:Point3D,b:Point3D,c:Point3D, color:uint)
{ pointA = a; pointB = b; pointC = c; this.color = color; }
public function draw(g:Graphics):void {
g.beginFill(color);
g.moveTo(pointA.screenX, pointA.screenY);
g.lineTo(pointB.screenX, pointB.screenY);
g.lineTo(pointC.screenX, pointC.screenY);
g.lineTo(pointA.screenX, pointA.screenY);
g.endFill(); }
}
}
这个文件保存在一个tom 的文件夹里面,当我们运行的时候,我们只需要在你绑定文档
暂时写住那么多!!日后再慢慢补充