# Z and Depth (3D)

I was following the 3D tuts here and made my little game with the race cars and tires. I tried added adding trees to the outside but all my depths got screwed up… The trees are in front of the tires, the tires are in front of the car…:(. Plaese Help!
Source:

``````  this.createEmptyMovieClip("theScene", 1);
theScene._x = 150;
theScene._y = 150;
theScene.depth = 1;
focalLength = 300;
cameraView = new Object();
cameraView.x = 0;
cameraView.y = -100;
cameraView.z = 0;
cameraView.velocity = 0;
cameraView.maxVelocity = 150;
objectsInScene = new Array();
placeObjectIn3D = function(obj, x, y, z){
var scaleRatio = focalLength/(focalLength + z);
obj._x = x * scaleRatio;
obj._y = y * scaleRatio;
obj._xscale = obj._yscale = 100 * scaleRatio;
obj.swapDepths(Math.round(-z));
};
// CARS
displayCar = function(){
var x = this.x - cameraView.x;
var y = this.y - cameraView.y;
var z = this.z - cameraView.z;
if (z < 4000){
if (z < 0){
this.z += 3000;
this.x = 200-Math.random()*400;
x = this.x - cameraView.x;
}
this.z += this.velocity;
z = this.z - cameraView.z;
}
placeObjectIn3D(this, x, y, z);
};
// TIRES
displayTire = function(){
var x = this.x - cameraView.x;
var y = this.y - cameraView.y;
var z = this.z - cameraView.z;
if (z < 0){
this.z += 3000;
z = this.z - cameraView.z;
}
placeObjectIn3D(this, x, y, z);
};
// TREES
displayTree = function(){
var x = this.x - cameraView.x;
var y = this.y - cameraView.y;
var z = this.z - cameraView.z;
if (z < 0){
this.z += 3000;
z = this.z - cameraView.z;
}
placeObjectIn3D(this, x, y, z);
};
// CARS
for (i=0; i<3; i++){
attachedObj = theScene.attachMovie("car", "car"+i, theScene.depth++);
attachedObj.x = 200 - Math.random()*400;
attachedObj.y = 0;
attachedObj.z = 500+Math.random()*1000;
attachedObj.velocity = 20 + i*5;
attachedObj.display = displayCar;
objectsInScene.push(attachedObj);
}
// TIRES
for (i=0; i<20; i++){
attachedObj = theScene.attachMovie("cone", "cone"+i, theScene.depth++);
if (i < 10){
attachedObj.x = -250;
attachedObj.z = i*300;
}else{
attachedObj.x = 250;
attachedObj.z = 150+(i-10)*300;
}
attachedObj.y = 0;
attachedObj.display = displayTire;
objectsInScene.push(attachedObj);
}
// TREES
for (i=0; i<30; i++){
attachedObj = theScene.attachMovie("tree", "tree"+i, theScene.depth++);
if (i < 15){
attachedObj.x = -300;
attachedObj.z = i*300;
}else{
attachedObj.x = 300;
attachedObj.z = 150+(i-15)*300;
}
attachedObj.y = 0;
attachedObj.display = displayTree;
objectsInScene.push(attachedObj);
}

drive = function(){
if (Key.isDown(Key.UP)){ cameraView.velocity *= 1.1;
cameraView.velocity += 1;
}else{ cameraView.velocity *= .95;}
if (Key.isDown(Key.DOWN)) cameraView.velocity -= 2;
if (cameraView.velocity < 0) cameraView.velocity = 0;
else if (cameraView.velocity > cameraView.maxVelocity)
cameraView.velocity = cameraView.maxVelocity;
cameraView.z += cameraView.velocity;
if (Key.isDown(Key.LEFT)) cameraView.x -= cameraView.velocity/5;
if (Key.isDown(Key.RIGHT)) cameraView.x += cameraView.velocity/5;
if (cameraView.x < -200) cameraView.x = -200;
else if (cameraView.x > 200) cameraView.x = 200;

for (var i = 0; i < objectsInScene.length; i++){
objectsInScene*.display();
}
};
theScene.onEnterFrame = drive;
``````

Can you find the error?
Thanks, sorry no fla. My server can’t handle it…

First theScene.depth = 1 shouldn’t be working, and even if it does its pointless… You have to use theScene.swapDepths(1), but theScene is already in depth 1 because you created it there.

The only thing I can see is all the spots where you have theScene.depth++. There is definatley a problem there.

What version of flash are you using?