📄 rotatepics.as
字号:
// the following code is based on "rotateXY.as" taken from
// "Foundation Actionscript 3.0 Animation: Making Things Move!"
// chapter 15, page 412-414
// where the code has been changed or added to has been commented
// the code which has been added in this class was added by Leo Walsh
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.*;
import mx.controls.TextArea;
import mx.core.UIComponent; // changed from importing sprite to importing
// UIComponent as it makes it easier to add
// to the stage
public class RotatePics extends UIComponent // changed sprite to UIComponent
{
//var pic:Picture = new Picture();
private var picInd:Number = 0;
private var picText:TextArea= new TextArea;
private var offset:Number;
private var imagePath:String = "../Images/"; // added to code, to access picture filepath
public var imgSize:Number = 0.1; // added to code, to define size of pictures in 3D model
private var nImg:int; // changed numBalls to nImg, stores number of pictures
private var pics:Array; // changed balls to pics
private var fl:Number = 400; // changed the focal length, this is down to preference
private var xBound:Number; // added, this defines the size of the stage in the x-direction
private var yBound:Number; // added, this defines the size of the stage in the y-direction
private var vpX:Number; // the following two were changed, this is due to being unable
private var vpY:Number; // to access flash.display.stage as this script is being used in
// an mxml document
include "zoom.as";
public function RotatePics(width:Number, height:Number, noPics:int) // added width and height, to pass stage dimensions
{
nImg=noPics; // from mxml to class
xBound = width; // added set xBound to stage width
yBound = height; // added set yBound to stage height
vpX = xBound/2; // set x-component of vanishing point to midpoint of screen on x-axis
vpY = yBound/2; // set y-component of vanishing point to midpoint of screen on y-axis
fl = width;
offset = height/20;
imgSize = height/5000;
//trace(xBound); // used in debugging to make sure correct value was set
//trace(yBound); // as above
init();
}
public function picPos ():void // entire function added by Leo Walsh
{
var pic:Picture = pics[0]; // variable dealing with current picture, in this case the first element
var yInc:int = 0; // number of discreet y positions
var numPics:int = 1; // number of pics per line
var incNum:int = 3; // increment to increase numPics
var totalPics:int = nImg; // total number of pics
while(totalPics > numPics)
{
totalPics -= (2*numPics); // take away two rows worth of pics from total
numPics += incNum; // set numPics to the number of pics on the next line
incNum++; // increase increment by 1
yInc += 2; // increase number of rows by 2
}
yInc++ ; // take account of middle row
var yCoord:Number = yBound/3; // value of y
var A:Number = yCoord; // A stands for the maximum amplitude, or the radius thinking about the sphere conceptually
numPics = 1; // reset numPics to 1;
incNum = 3; // reset incNum to 3;
var angleY:Number = 0; // angle for calculating y
var angleYInc:Number = (Math.PI/(yInc - 1)); // angle between y values
var picBegin:int = 0; // first element of array
var picEnd:int = nImg -1; // last element of array
while(yCoord > 1)
{
var angleInc:Number = 2*(Math.PI/(numPics)); // set increment angle for x & z values
var angleX:Number = 0; // set angle x to 0
var angleZ:Number = 0; // set angle z to 0
for (var i:int = 0 ; i != numPics ; i++) // iterate through all pictures
{
pic = pics[picBegin];
pic.xpos = (Math.sqrt(Math.pow(A,2) - Math.pow(yCoord,2)))*Math.cos(angleX); //set x position in 3D space of pic in upper hemisphere
pic.ypos = yCoord; //set y position of pic
pic.zpos = (Math.sqrt(Math.pow(A,2) - Math.pow(yCoord,2)))*Math.sin(angleZ); //set z position of pic
pic = pics[picEnd];
pic.xpos = (Math.sqrt(Math.pow(A,2) - Math.pow(yCoord,2)))*Math.cos(angleX); //set x position of corresponding picture in lower hemisphere
pic.ypos = -yCoord; //set y position of pic
pic.zpos = (Math.sqrt(Math.pow(A,2) - Math.pow(yCoord,2)))*Math.sin(angleZ); //set z position of pic
picBegin++; //move to next pic to place in upper hemisphere
picEnd--; //move to next pic in lower hemiplace
angleX += angleInc; //change x angle used for x position for next pictures
angleZ += angleInc; //change z angle used for x position for next pictures
//trace("picBegin="+picBegin); used for debug
//trace("picEnd=" + picEnd); used for debug
}
numPics += incNum; //set number for next row
incNum++; //set increment for the row after the next row
angleY += angleYInc; //find angle used for next y coordinate
yCoord = A*Math.cos(angleY); //calculate new y position
//trace ("yCoord="+yCoord);
}
yCoord = 0; // place final pictures at ypos = 0
angleX = angleZ = 0; //reset angles
angleInc = 2*(Math.PI/(numPics)); //calculate angles between pictures
//trace("angleInc" + angleInc);
//trace("picEnd" +picEnd);
//trace("picBegin" +picBegin);
for(; picEnd >= picBegin; picBegin++) //iterate through final pictures
{
pic = pics[picBegin];
pic.xpos = A*Math.cos(angleX); //set x coordinate of pic
pic.ypos = yCoord; //set y coordinate of pic
pic.zpos = A*Math.sin(angleZ); //set z coordinate of pic
//trace("picBegin"+picBegin);
angleX += angleInc; //move to next position
angleZ += angleInc; //move to next position
}
}
private function init():void
{
pics = new Array();
for(var i:uint = 0; i < nImg; i++)
{
var pic:Picture = new Picture(); // create new instance of Picture
pic.index = i;
var detailLoader:URLLoader = new URLLoader(); //
//detailLoader.addEventListener(Event.COMPLETE, detailLoaded); // store text data
var detailURL:String = "../Details/Detail"+(i+1).toString()+".txt" // in picture class
var detailURLReq:URLRequest = new URLRequest(detailURL); //
detailLoader.load(detailURLReq); //
//trace("picText" + picText.text);
//pic.detail.text = detailLoader.data;
trace("detailLoader"+detailLoader.data);
var pictURL:String = imagePath+"Image"+(i+1).toString()+".jpg" // this code was modified with code from album2
var pictURLReq:URLRequest = new URLRequest(pictURL); // from Mike Chantler. Loads images into pic class
pic.load(pictURLReq); // and stores them in array
pics.push(pic); //add pic to array
pic.addEventListener(MouseEvent.CLICK , onImageMouseClick ); //adds listener to aliaa's function to zoom
addChild(pics[i]); // add pics to stage/application
}
picPos(); //set up pic positions
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
//private function detailLoaded(e:Event):void {
//picText.text = new String(e.target.data);
//trace("picText"+ e.currentTarget.valueOf());
//}
//the following functions are unchanged except where noted
public function onEnterFrame(event:Event):void
{
var angleX:Number = (mouseY - vpY) * .0001; //lowered sensitivity
var angleY:Number = (mouseX - vpX) * .0001; //lowered sensitivity
for(var i:uint = 0; i < nImg; i++)
{
var pic:Picture = pics[i]; //use pics instead of balls
rotateX(pic, angleX); //note: variable names have changed, but the function is identical though
rotateY(pic, angleY);
doPerspective(pic);
}
sortZ();
}
private function rotateX(pic:Picture, angleX:Number):void
{
var cosX:Number = Math.cos(angleX);
var sinX:Number = Math.sin(angleX);
var y1:Number = pic.ypos * cosX - pic.zpos * sinX;
var z1:Number = pic.zpos * cosX + pic.ypos * sinX;
pic.ypos = y1;
pic.zpos = z1;
}
private function rotateY(pic:Picture, angleY:Number):void
{
var cosY:Number = Math.cos(angleY);
var sinY:Number = Math.sin(angleY);
var x1:Number = pic.xpos * cosY - pic.zpos * sinY;
var z1:Number = pic.zpos * cosY + pic.xpos * sinY;
pic.xpos = x1;
pic.zpos = z1;
}
private function doPerspective(pic:Picture):void
{
if(pic.zpos > -fl)
{
var scale:Number = fl / (fl + pic.zpos);
pic.scaleX = pic.scaleY = scale * imgSize;
pic.x = vpX + pic.xpos * scale - offset; //offset added to center pictures
pic.y = vpY + pic.ypos * scale - offset;
pic.visible = true;
}
else
{
pic.visible = false;
}
}
public function sortZ():void
{
pics.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < nImg; i++)
{
var pic:Picture = pics[i];
setChildIndex(pic, i);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -