...

View Full Version : Orb[a] is undefined? (prototype script)



Sleeping_Troll
05-24-2009, 03:28 AM
I apologise for posting so much code, but I have highlighted the "villian" in red, I would rather post too much than not enough!



////////////////////////////////////////////////
// Thanks to the inspiration of thwacks! //
// evolve 14/06/2004 //
// http://www.mgifos.demon.co.uk //
// last evolved 15/06/2004 //
////////////////////////////////////////////////
var numOrbs=36; //number of Orbs
var iv=1; //initial Orb speed
var Zeye; //perspective:distance of eye from box centre
var Zscreen; //perspective:distance of screen from box centre
var OrbInitialSize,OrbProximityFactor;
var Xmin,Ymin,Xmax,Ymax,Zmin,Zmax;
var OrbRadius;
var Perspective;
var Left;
var Top;
var Height;
var Width;
var Zindex;
var VelocityX=[];
var VelocityY=[];
var VelocityZ=[];
var PositionX=[];
var PositionY=[];
var PositionZ=[];
var Dimensions=[];
var Orb=[];
function init(){
resize();
setTimeout("Main()",1000);
}

function Main(){
var i,im,theta;
OrbInitialSize=25;
OrbProximityFactor=4*OrbInitialSize*OrbInitialSize;
var container=new Element("div",{id:"container",style:"display:'none',left:'0px',top:'0px',width:'0px',height:'0px',zIndex: '0'"});
for(i=0;i<numOrbs;i++){
var phi,theta;
PositionX[i]=Dimensions.width/2;
PositionY[i]=Dimensions.height/2;
PositionZ[i]=-Dimensions.width*2;
theta=Math.PI*2*Math.random();
phi=Math.asin(Math.random());
if(Math.random()>0.5)phi=-phi;
VelocityX[i]=iv*Math.cos(theta)*Math.cos(phi);
VelocityY[i]=iv*Math.sin(theta)*Math.cos(phi);
VelocityZ[i]=iv*Math.sin(phi);
var Orb[i]=new Object;
Orb[i].image=new Element("img",{src:"./images/Orb.png",style:"position:'absolute', display:'none',left:'0px',top:'0px',width:'100px',height:'100px',zIndex: '0'"});This line throws error Orb[i] is undefined, note in my var declarations "Orb=[];" $("container").insert(Orb[i]);
setInterval("updateOrbs()",1000);
}
}


function updateOrbs(){
/////////////////////////////////////////////////////////////
// Collision math
// Based on the fluid simulation by Peter Birtles //
// http://astronomy.swin.edu.au/pbourke/modelling/fluid/ //
// //
// Javascript conversion evolved January 2002 //
// 3D conversion and "energy leak" plugged 10/06/2004 //
/////////////////////////////////////////////////////////////
var a,b,d,VelocityXa,VelocityYa,VelocityZa,VelocityXb,VelocityYb,VelocityZb,nx,ny,nz,cdx,cdy,cdz,dd,m,co sam,cosamx,cosamy,cosamz,ddx,ddy,ddz;
for(a=0; a<numOrbs; a++){
for(b=(a+1); b<numOrbs; b++){ //Note:The original iterated both a and b from 1 to numOrbs, skipping the a=b case
d = (Orb[a].PositionX-Orb[b].PositionX)*(Orb[a].PositionX-Orb[b].PositionX)+(Orb[a].PositionY-Orb[b].PositionY)*(Orb[a].PositionY-Orb[b].PositionY)+(Orb[a].PositionZ-Orb[a].PositionZ)*(Orb[a].PositionZ-Orb[b].PositionZ);
if (d < OrbProximityFactor){
d = Math.sqrt(d);
VelocityXa= Orb[a].VelocityX;VelocityYa=Orb[a].VelocityY;VelocityZa=Orb[a].VelocityZ;VelocityXb=Orb[b].VelocityX;VelocityYb=Orb[b].VelocityY;VelocityZb=Orb[b].VelocityZ;
nx=Orb[b].PositionX-Orb[a].PositionX;
ny=Orb[b].PositionY-Orb[a].PositionY;
nz=Orb[b].PositionZ-Orb[a].PositionZ;
if (Math.abs(d)>0.0001){
cdx=nx/d;cdy=ny/d;cdz=nz/d;
m = Math.sqrt(VelocityXa*VelocityXa+VelocityYa*VelocityYa+VelocityZa*VelocityZa);
if (Math.abs(m)>0.0001){
cosam = (cdx*VelocityXa+cdy*VelocityYa+cdz*VelocityZa);
cosamx=cosam*cdx;
cosamy=cosam*cdy;
cosamz=cosam*cdz;
VelocityXa -= cosamx;
VelocityYa -= cosamy;
VelocityZa -= cosamz;
VelocityXb += cosamx;
VelocityYb += cosamy;
VelocityZb += cosamz;
}
m = Math.sqrt(Orb[b].VelocityX*Orb[b].VelocityX+Orb[b].VelocityY*Orb[b].VelocityY+Orb[b].VelocityZ*Orb[b].VelocityZ);
if (Math.abs(m) >0.0001){
cosam = (cdx*Orb[b].VelocityX+cdy*Orb[b].VelocityY+cdz*Orb[b].VelocityZ);
cosamx=cosam*cdx;
cosamy=cosam*cdy;
cosamz=cosam*cdz;
VelocityXa += cosamx;
VelocityYa += cosamy;
VelocityZa += cosamz;
VelocityXb -= cosamx;
VelocityYb -= cosamy;
VelocityZb -= cosamz;
}
Orb[a].VelocityX=VelocityXa;Orb[a].VelocityY=VelocityYa;Orb[a].VelocityZ=VelocityZa;Orb[b].VelocityX=VelocityXb;Orb[b].VelocityY=VelocityYb;Orb[b].VelocityZ=VelocityZb;
}
}
}
}
alert(Orb[a]);
//apply boundaries
PositionX[a] += Orb[a].VelocityX;
PositionY[a] += Orb[a].VelocityY;
PositionZ[a] += Orb[a].VelocityZ;
Perspective=(Zeye-Zscreen)/(Zeye-PositionZ[a]);
OrbRadius=OrbInitialSize*Perspective;
if (PositionX[a]<Xmin-OrbRadius*2){
PositionX[a]=Xmax+OrbRadius;//wrap
if(Orb[a].renew>0){
Orb[a].renew=2;
}
}
if (PositionX[a]>Xmax+OrbRadius){
PositionX[a]=Xmin-OrbRadius*2;//wrap
if(Orb[a].renew>0){
Orb[a].renew=2;
}
}
if (PositionY[a]<Ymin-OrbRadius*2){
PositionY[a]=Ymax+OrbRadius;//wrap
if(Orb[a].renew>0){
Orb[a].renew=2;
}
}
if (PositionY[a]>Ymax+OrbRadius*2){
PositionY[a]=Ymin-OrbRadius;//wrap
if(Orb[a].renew>0){
Orb[a].renew=2;
}
}
if (PositionZ[a]<Zmin){
Orb[a].VelocityZ=-Orb[a].VelocityZ;//rebound
}
if (PositionZ[a]>Zmax){
Orb[a].VelocityZ=-Orb[a].VelocityZ;//rebound
Orb[a].renew=1;
}
var styles={
left :PositionX[a],
top :PositionY[a],
height :OrbRadius,
width :OrbRadius,
zIndex :PositionZ[a]
}
Orb[a].image.setStyle(styles);
}

function resize(){
if (window.innerWidth){ //if browser supports window.innerWidth
Dimensions.width=window.innerWidth;
Dimensions.height=window.innerHeight;
}
else if (document.all){ //else if browser supports document.all (IE 4+)
Dimensions.width=document.body.clientWidth;
Dimensions.height=document.body.clientHeight;
}
$("container").setStyle({
width:Dimensions.width,
height:Dimensions.height,
top: 0,
left: 0
});
Zscreen=Dimensions.height;//Must be < Zeye
Zeye=Dimensions.width;
Perspective=(Zeye-Zscreen)/(Zeye);
OrbRadius=(OrbInitialSize*Perspective);
Xmax=Dimensions.width;
Ymax=Dimensions.height;
Zmax=0;
Xmin=OrbRadius;
Ymin=OrbRadius;
Zmin=-(Dimensions.width*2);
}

Anybody know what I am missing?

Philip M
05-24-2009, 09:27 AM
var Orb=[]; // global variable

var Orb[i]=new Object; // a variable local to the function Main()

It is possible in JavaScript to have two different variables with the same name, one global and the other local.

Delete the line:-

var Orb[i]=new Object;


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum