Thursday, August 6, 2009

MJW Avoider Game ep11ch1



Alrighty-- Another challenge! I already wrote below about my fun with isNaN during this challenge. But let me try to document what I did for the challenge itself. Won't be too long...

Staying in the GameOverScreen.as file, I added the following to the constructor. (I called my array bestScoreArray) I wanted to check if the array was undefined or empty, and if so, create it.
  1. if( isNaN(sharedObject.data.bestScoreArray) )

  2. {

  3.     sharedObject.data.bestScoreArray = new Array();

  4. }



Then, I figured I'd plug information when the final score was set. So down in the setFinalScre function:
  1. else if( scoreValue > sharedObject.data.bestScore )

  2. {

  3. //  sharedObject.data.bestScore = scoreValue;

  4.     sharedObject.data.bestScoreArray = [scoreValue, sharedObject.data.totalGames, "Test"];         

  5. }


I'm grabbing three pieces of information and sticking it in an array -- score, total number of games played when that score was reached, and an arbitrary string of text just to test.

Ok, now I need to make sure it's working, so I threw this down below in the same 'setFinalScore' function. It will loop through the array and 'trace' every item to the output window.
  1. for (var i:int = 0; i < sharedObject.data.bestScoreArray.length; i++ )

  2. {

  3.     trace( i, sharedObject.data.bestScoreArray[i] );

  4. }



Now run it.

D'oh! Didn't work... why not? Looking back through my code I realized this was wrong:
  1. if( isNaN( sharedObject.data.bestScoreArray ) )



Can you spot why? Because we're creating an array... so it's NEVER going to be a "number" (isNaN checks if something is 'not a number')

So, change that to this, and try again:
  1. if( sharedObject.data.bestScoreArray == null )

  2. {

  3.     sharedObject.data.bestScoreArray = new Array();

  4. }



Voila! Should work now. If you have any problems, let me know.

No comments:

Post a Comment