I’ve been having some strange problems with the collision code of a game I’m making, and I’ve been able to trace it down to this: the getters of my properties are returning NaN, instead of an actual number.
Here are the declarations of the properties, at the top of the class:
private var _top:Number;
private var _bottom:Number;
private var _left:Number;
private var _right:Number;
And here are the getters:
public function get top():Number
{
return _top;
}
public function get bottom():Number
{
return _bottom;
}
public function get left():Number
{
return _left;
}
public function get right():Number
{
return _right;
}
The values become generated by calling the public function setSides(tileSize:Number) of a class instance (the values depending on the tileSize parameter). After I’ve run that, I run a trace which gets the values using the getters, but all of them return NaN.
The thing is, if I run a similar trace within setSides(), without using the getters – i.e. using the variables directly – they return the correct values.
And to make sure, I also did it within the function using the getters, so I’m sure they’re the problem.
Anyone know what’s going on?
EDIT: Here is my setSides() function:
public function setSides(tileSize:Number):void
{
var _top:Number = Math.floor(_yPos / tileSize);
var _bottom:Number = Math.floor((_yPos + 45) / tileSize);
var _left:Number = Math.floor(_xPos / tileSize);
var _right:Number = Math.floor((_xPos + 20) / tileSize);
trace("top: " + top + " bottom: " + top + " left: " + left + " right: " + right); //This gives the correct values, not NaN
}
But even if there was something wrong with my values I’m assigning (which would show in my trace), I’ve assigned a value of 0 to each of the variables in my constructor, so they shouldn’t give NaN from the moment my class is instantiated.
EDIT: Oh man, I just noticed this now. Due to some testing and re-editing I did, I was still using my variables as if I was defining them (with the var and :Number). Also, the “assigning 0 to them” was also before this testing and re-editing I mentioned, so that wasn’t present.
Remove the “var” and the type “Number” from your “setSides()” method. Those are local variables. You’re not actually setting anything. Should be: