4 Replies Latest reply on Jul 9, 2011 4:35 AM by Ned Murphy

    Not a number??


      if (this.hitTest(_root.enemy))


      When this happens, instead of incrementing health by 1, it says NaN. I've had this problem for a really long time and tried to find out what's wrong, but I can't. If anyone else can help, that would be appreciated a lot. Thanks.

      P.S: I'm using CS4.

        • 1. Re: Not a number??
          Ned Murphy Adobe Community Professional & MVP

          What says NaN?  How is health defined in the _root?

          • 2. Re: Not a number??
            0slimjimmy0 Level 1

            Health says NaN.

            code in frame: text.Life = "Health: " + health

            the health variable is in the Life text properties.

            • 3. Re: Not a number??
              kglad Adobe Community Professional & MVP

              a textfield's text property is always a string (even if it looks like a number to you).  you can cast it as a number, if that's reasonable:



              // this will also cause an NaN error if sometextfield.text="hi", for example.

              // but if the text property looks like a number to you, flash will handle the conversion without problem.

              // and you can then treat _root.health as a number.



              but, if you use:




              and try something like:




              you will always get an NaN error.

              1 person found this helpful
              • 4. Re: Not a number??
                Ned Murphy Adobe Community Professional & MVP

                Your explanation is not very clear.  You should use a variable for the health and a textfield for displaying it as a separate entity.  In the _root timeline declare the variable...


                var health = 100;    // or whatever value it starts at



                And if you plan to display the health value in a TextField, then create a TextField and assign it an instance name... let' say you name it "healthTxt" and it also lives in the _root timeline.  Then your code should look like...


                if (this.hitTest(_root.enemy))

                    _root.healthTxt.text  = String(_root.health);


                and if all of that code happens to be on the _root timeline, get rid of all the unnecessary _root references.  You should not make a habit of throwing _root in front of everything in sight.  You should only use it if you are not on the _root timeline and have no other means of targeting it.