A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: How to avoid the NaN value? Help!

  1. #1
    Member
    Join Date
    Jan 2009
    Posts
    36

    Unhappy 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.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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)) 0

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  4. #4
    Member
    Join Date
    Jan 2009
    Posts
    36

    Smile 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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center