PDA

View Full Version : 3D with AS3 : Help on re-coding a Kirupa example



humbucker
June 7th, 2009, 12:51 PM
Hello everyone,

Can some please help me recoding a great 3D example found on kirupa (originally written in AS2) in AS3 ?

Here is the linl : http://www.kirupa.com/developer/actionscript/shape_camera.htm (http://www.kirupa.com/forum/../developer/actionscript/shape_camera.htm)
It's the example with stars at bottom of page.

Here is the code I've changed so far :



// create a scene movieclip to contain all 3D elements
// and center it on the screen.
var theScene:MovieClip=new MovieClip();



theScene.x = 150;
theScene.y = 150;
theScene.depth = 1; // variable to control depth placement

// define s function to set the target of the cameraView
// when you select the star (onPress)
selectStar = function(e:MouseEvent):void {
cameraView.target.x = this.x;
cameraView.target.y = this.y;
cameraView.target.z = this.z;
}

// this is the function for displaying the star onscreen
displayStar = function(cameraView, focalLength){
var x = this.x - cameraView.x;
var y = this.y - cameraView.y;
var z = this.z - cameraView.z;
if (z < 0){
this.z += 5000;
this.x = 500-Math.random()*1000;
this.y = 500-Math.random()*1000;
x = this.x - cameraView.x;
y = this.y - cameraView.y;
z = this.z - cameraView.z;
}
var scaleRatio = focalLength/(focalLength + z);
this.x = x * scaleRatio;
this.y = y * scaleRatio;
this.scaleX = this.scaleY = 100 * scaleRatio;

swapChildren(Math.round(-z));


};

// define array to hold the objects in the scene
objectsInScene = new Array();

// loop through and create 10 stars randomly in the scene
for (i=1; i<=10; i++){



//attachedObj = theScene.attachMovie("star", "star"+i, theScene.depth++);
addChild(theScene);
attachedObj = theScene.addChild(new star());
//attachedObj = theScene.addChild(star);
//star.depth = depth++;

attachedObj.x = 500 - Math.random()*1000;
attachedObj.y = 500 - Math.random()*1000;
attachedObj.z = i*500;
attachedObj.buttonMode = true;


attachedObj.addEventListener(MouseEvent.MOUSE_DOWN , selectStar);
attachedObj.display = displayStar;
objectsInScene.push(attachedObj);
}

// now make a camera object to serve as the users view or camera
// position within the cockpit of the car
cameraView = new Object();
cameraView.x = 0;
cameraView.y = 0;
cameraView.z = 0;
// set a target object in the cameraView to represent the positions
// the camera is easing to in its movement
cameraView.target = new Object();
cameraView.target.x = 0;
cameraView.target.y = 0;
cameraView.target.z = 0;

// define focal length
focalLength = 40;

// function controlling the camera ease and all displaying
// for objects in the objectsInScene array
easeToTarget = function(event:Event) {
// ease each camera point to its target point
cameraView.x += (cameraView.target.x - cameraView.x)/5;
cameraView.y += (cameraView.target.y - cameraView.y)/5;
cameraView.z += (cameraView.target.z - cameraView.z)/5;

// run the display function for each object in objectsInScene array
for (var i=0; i<objectsInScene.length; i++){
objectsInScene[i].display(cameraView, focalLength);
}
};

theScene.addEventListener(Event.ENTER_FRAME, easeToTarget);



Thank you !! :bounce: