[FONT=Arial]http://www.kirupa.com/developer/actionscript/more_panning.htm [/FONT]
have been trying to get the tutorial working in as3 …
the original code (as2 code) is as follows
this.createEmptyMovieClip("theScene", 1);
theScene._x = 150;
theScene._y = 150;
objectsInScene = new Array();
cameraView = new Object();
cameraView.x = 0;
cameraView.y = 0;
cameraView.z = 0;
cameraView.rotation = 0;
focalLength = 300;
displayFigure = function(){
var x = this.x - cameraView.x;
var z = this.z - cameraView.z;
var y = this.y;
var angle = Math.atan2(z,x);
var radius = Math.sqrt(x*x + z*z);
x = Math.cos(angle + cameraView.rotation) * radius;
z = Math.sin(angle + cameraView.rotation) * radius;
if (z > 0){
if (!this._visible) this._visible = true;
var scaleRatio = focalLength/(focalLength + z);
this._x = x * scaleRatio;
this._y = y * scaleRatio;
this._xscale = this._yscale = 100 * scaleRatio;
this.swapDepths(Math.round(-z));
}else{
this._visible = false;
}
};
for (i=0; i<8; i++){
attachedObj = theScene.attachMovie("figure", "figure"+i, i);
if (i < 4){
attachedObj.x = -200
attachedObj.z = 50 + i*150
attachedObj.y = 0;
}else{
attachedObj.x = 200
attachedObj.z = 50 + (i-4)*150
attachedObj.y = 0;
}
attachedObj.display = displayFigure;
objectsInScene.push(attachedObj);
}
walkAround = function(){
if (Key.isDown(Key.RIGHT)) cameraView.rotation += .05;
if (Key.isDown(Key.LEFT)) cameraView.rotation -= .05;
var movement = 0;
if (Key.isDown(Key.UP)) movement += 10;
if (Key.isDown(Key.DOWN)) movement -= 10;
cameraView.x += Math.sin(cameraView.rotation)*movement;
cameraView.z += Math.cos(cameraView.rotation)*movement;
for (var i=0; i < objectsInScene.length; i++){
objectsInScene*.display();
}
};
theScene.onEnterFrame = walkAround;
i came up with the following
var theScene:MovieClip = new MovieClip();
this.addChild(theScene);
theScene.x=150;
theScene.y=150;
var objectsInScene:Array = new Array();
var cameraView:Object = new Object();
cameraView.x=0;
cameraView.y = 0;
cameraView.z = 0;
cameraView.rotation = 0;
var focalLength:Number = 300;
//Im trying to just add one Object out the library which iv
var person:Person = new Person();
theScene.addChild(person);
person.x=200;
person.z=100;
person.y=0;
objectsInScene.push(person);
//HAD TO CHANGE x to x1, y to y1 because of conflicting variables... (said the compiler)
function displayFigure(person:Person) {
var x1 = person.x - cameraView.x;
var z1 = person.z - cameraView.z;
var y1 = person.y;
var angle = Math.atan2(z1,x1);
var radius = Math.sqrt(x1*x1+ z1*z1);
x1 = Math.cos(angle + cameraView.rotation) * radius;
z1 = Math.sin(angle + cameraView.rotation) * radius;
if (z1 > 0){
if (!person.visible) person.visible = true;
var scaleRatio = focalLength/(focalLength + z1);
person.x = x1 * scaleRatio;
person.y = y1 * scaleRatio;
person.scaleX = person.scaleY = 100 * scaleRatio;
//compiler didnt like the following line so had to comment it out
//theScene.setChildIndex(person,Math.round(z1));
}else{
person.visible = false;
}
}
//USED A KEY .as class i found on the internet which allows me to use Key.isDown like this
function walkAround (ev:Event){
if (Key.isDown(Keyboard.RIGHT)) cameraView.rotation += .05;
if (Key.isDown(Keyboard.LEFT)) cameraView.rotation -= .05;
var movement = 0;
if (Key.isDown(Keyboard.UP)) movement += 10;
if (Key.isDown(Keyboard.DOWN)) movement -= 10;
cameraView.x += Math.sin(cameraView.rotation)*movement;
cameraView.z += Math.cos(cameraView.rotation)*movement;
trace (" X " + cameraView.x + " Z "+cameraView.z + " ROT "+cameraView.rotation);
for (var i=0; i < objectsInScene.length; i++){
displayFigure(objectsInScene*);
}
};
theScene.addEventListener(Event.ENTER_FRAME, walkAround);
… it is being accepted as valid code , the coordinates for the objects seem to change without the camera view being changed .
if someone could tell me where im going wrong would appreciate it to no end … thanks . tom.