Ryan
February 27, 2018, 5:37pm
1
[Something defined] being colliders. Can someone explain to me why this is returning an uncaught reference error?
// initialize code called once per entity
RotateRing.prototype.initialize = function()
{
var colliders = this.app.root.findByTag('collider');
for(i=0;i<colliders.length;i++)
{
colliders[i].rigidbody.enabled = false;
}
};
RotateRing.prototype.spin = function()
{
for(i=0;i<colliders.length;i++) //Error here
{
if(colliders[i].rigidbody)
{
colliders[i].rigidbody.enabled = true;
}
colliders[i].rigidbody.applyTorque(0,50,0);
}
this.entity.rotate(0,50,0);
};
vaios
February 27, 2018, 5:50pm
2
Because colliders
is not in the scope of function spin
. You should make it a member variable in initialize
.
Ryan
February 27, 2018, 5:56pm
3
Okay, but I’ve done this before in a previous project:
CheckRoll.prototype.initialize = function()
{
var dice = this.app.root.findByTag('Dice');
//More variables here
};
CheckRoll.prototype.check = function()
{
//Makes sure each dice has stopped by comparing its current position
//to its last position
for(i=0;i<dice.length;i++)
{
if(stopped[i] === true)
{
continue;
}
var diePos = dice[i].getLocalPosition();
if(lastPos[i] !== undefined)
{
if(this.toDecimal(diePos.x) === this.toDecimal(lastPos[i].x) && this.toDecimal(diePos.y) === this.toDecimal(lastPos[i].y) && this.toDecimal(diePos.z) === this.toDecimal(lastPos[i].z))
{
dice[i].stopped = true;
this.stop = true;
}
}
lastPos[i] = diePos.clone();
}
this.stopCheck();
};
There’s about three or four other places in this project where I call that dice variable outside of initialize without issue.
vaios
February 27, 2018, 5:57pm
4
You are probably declaring dice
somewhere globally elsewhere too. If you want to access variables in another function you need to declare them in a scope that contains the scope of the function or make them member variables e.g. this.colliders = ...
Ryan
February 27, 2018, 5:59pm
5
Here’s a link to the only script in the project I can find a declaration for dice.
https://playcanvas.com/editor/code/529780?tabs=10861152,10964296
I went through all other scripts and couldn’t find another pace I declare dice as something.
Ryan
February 27, 2018, 6:04pm
7