Showing posts with label tools. Show all posts
Showing posts with label tools. Show all posts

Wednesday, August 5, 2009

What is isNaN()?



So earlier I worked through MJWs Avoider Game Tutorial Chapter 11 and got to the challenges. I was excited! I wanted to save some data to the local computer too! So my first addition was going to be adding how many times the player played my game. Sounds easy enough, it was using the same idea as saving the highest score, right?

Well, I figured GameOverScreen.as was as good a place as any -if not better!- to do this. So I added the following lines:

  1. sharedObject.data.totalGames += 1;

  2. trace( sharedObject.data.totalGames ); 



But my output didn't read "1" - it said NaN. Whaaaat?
So I threw in an IF statement:
  1. if( sharedObject.data.totalGames == null )

  2. {

  3.     sharedObject.data.totalGames = 0;

  4. }


Haha! MJW helped me to learn to code!
...only problem was: It didn't work. My output still read NaN.
Not one to just ask a question and wait for the answer (sorry, I told you to post a comment on MJW's site if you had a question... I'm a hypocrite) I browsed to Google and searched for an answer.

The answer to my problem was isNaN! Here's what I put in my code:
  1. if( isNaN(sharedObject.data.totalGames) )

  2. {

  3.     sharedObject.data.totalGames = 0;

  4. }

  5. sharedObject.data.totalGames += 1;

  6. trace( sharedObject.data.totalGames );     



Now that works! I didn't even have to 'import' anything... though I did search for that because I accidentally put isNan (note the lower case 'n') in my code and got an error.

So there you go... isNaN()

Thursday, July 23, 2009

Trace()


I cannot over. emphasize. my. love. of. the. Trace. Command (ok, maybe I can overemphasize it)

Seriously though, when it comes to debugging my AS3 code, trace() is invaluable!

For example, when trying to create different start buttons for mouse and keyboard controls I was trying to send navigation events, and changing variables, creating variables... sometimes, at first I wasn't sure if the code I implemented worked so I used trace to spit out some information into the 'output' tab. (It's down between 'Properties' and 'Compiler Errors')

Here's how you use it:

  1. trace( variableName ); // Outputs the value of that variable

  2. trace( "Your Text Here" ); // Outputs 'Your Text Here'

  3. trace( "AvoiderGame.as Line 37 xSpeed = ", xSpeed ); //Outputs 'AvoiderGame.as Line 37 xSpeed = 6' (or whatever the value of xSpeed is...)



Easy Peasy Lemon Squeezy!