-
How to avoid the NaN value? Help!
i have this code:
function mover (e:Event):void
{
if (e.currentTarget.scaleX > 1.39)
{
e.currentTarget.auxScl = -1;
}
if (e.currentTarget.scaleX < 0.71)
{
e.currentTarget.auxScl = 1;
}
e.currentTarget.scaleX += e.currentTarget.auxScl / 1000;
e.currentTarget.scaleY=e.currentTarget.scaleX;
e.currentTarget.z=e.currentTarget.scaleX;
SimpleZSorter.sortClips (container);
distX=e.currentTarget.dx-e.currentTarget.x;
distY=e.currentTarget.dy-e.currentTarget.y;
e.currentTarget.x+=distX/Math.abs(distX);
e.currentTarget.y+=distY/Math.abs(distY);
posX=Math.round(e.currentTarget.x);
posY=Math.round(e.currentTarget.y);
if (posX==e.currentTarget.dx||posY==e.currentTarget.d y)
{
e.currentTarget.dx = Math.round((Math.random() * 60) + 150);
e.currentTarget.dy = Math.round((Math.random() * 150) + 245);
}
}
it supposed that the e.currentTarget be continuously moving, when the e.currentTarget.x reaches the e.currentTarget.dx (or y to dy) position, gets a new position to move on. The code seems to work and does for a while, but suddenly the e.currentTarget disappears from the scene, this is because the .x value of the e.currentTarget gets NaN. Somebody can tell me how to avoid that the x value gets NaN?
Please I really need your help!!!!
Thanks for reading.
-
I'd guess you're running into a division by zero at some point. Try checking your values before you apply them to things:
PHP Code:
if(isNaN(n)) n = 0;
-
a few things I see: Depending on the speeds your objects are traveling at, they may pass right through the posX or posY.
If e.currentTarget.dx == e.currentTarget.x, then distX will be 0, and dividing by it will get you NaN. Same with ys.
-
Thanks!
Yes, as you said when the x gets 0 was divided by 0 getting NaN.
Solved...
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|