Friday, September 11, 2009

Update!


It's been a while, thought it was time to post an update. Life's been exciting lately. I'm about to find out whether my wife is pregnant with a boy or a girl... I got laid off from my job... worked two new jobs (though only kept the second one...), and received two other offers (it's nice to feel wanted!)... and found out how to make a killer blended coffee drink! Ok, that last was the least exciting, but I'm going to fill you in on the details anyways...

What you'll need...


  • A blender (we have a 'Magic Bullet')

  • A pot of coffee (we used Wal-Mart brand)

  • Ice cube trays (these came with the apartment, believe it or not)

  • Sugar (sweeten to your taste, I like a little, my sweetie loves it sweeter!)

  • Milk (We've made it with both 1% and evaporated milk - both good!)

  • Some dark chocolate (optional)

  • First, make a pot of coffee... After it's cooled down, pour the brewed coffee into ice cube trays and freeze overnight.
    The next morning, put some coffee cubes, sugar, milk, and chocolate into the blender and blend until smooth. ...That's it! Try it!


    Ok, on to other stuff... I've been working on Tonypa's tile based game flash tutorials aaaand the jury is still out on whether I like them or not. I'll finish them, and then maybe I'll feel more qualified to give my opinions...
    You'll just have to come back!

    Tuesday, August 11, 2009

    Getting rid of duplicate lines of code



    I just finished one of MJW's sh'm'up tutorials and by the end I had multiple lines that were duplicates of each other. For example:
    1. {

    2. removeChild( bullet );

    3. bullets.splice( j, 1 );

    4. }


    While this used to be okay - I just wanted working code! - Now, after these tutorials I'm spoiled. Some would say a better (though very beginner!) programmer. I feel like I need to compact it and clean it up as much as possible.
    So I created a public function called removeClip (first it was removeEnemy, then I figured I could use it for the bullets too, and maybe other things later!) and passed it three variables:
  • instance of the clip I wanted to remove.
  • the position it was in the array
  • and, the array name.

    Tada!
    1. public function removeClip( movieClip:MovieClip, i:int, array:Array ):void

    2. {

    3.     removeChild( movieClip );

    4.     array.splice( i, 1 );

    5. }



    Then I just went back through AvoiderGame.as and replaced all those lines that removed the bullet / enemy with:
    removeClip( enemy, i, army );
    or
    removeClip( bullet, j, bullets );

    Michael, thanks for teaching us how to code.

    EDIT: MJW posted a comment on trimming down my code even more that I'd like to share! (Especially in case you don't read the comments...)

    He wrote: "I'm not sure if you know this, but all arrays have a method, .indexOf( item ), that returns the index of the item specified (or -1 if the item is not in the array).

    So you can write "array.splice( array.indexOf( movieClip ), 1 )", for example. This way you don't have to pass the index of the item through to the function each time!
    "

    I've used his suggestion, and changed my removeClip function to this:
    1. public function removeClip( movieClip:MovieClip, array:Array ):void

    2. {

    3.     removeChild( movieClip );

    4.     array.splice( array.indexOf( movieClip ), 1 );

    5. }


    I guess if you wanted, you could also make a check to make sure it was part of that array before trying to splice, like this:
    1. public function removeClip( movieClip:MovieClip, array:Array ):void

    2. {

    3.     removeChild( movieClip );

    4.     if( array.indexOf( movieClip ) != -1  )

    5.     {

    6.         array.splice( array.indexOf( movieClip ), 1 );

    7.     }

    8. }


    Anyways, it might not seem like a lot at first. You're only slimming it down by one small variable. But that's one less variable for every bullet that leaves the screen, or hits an enemy, plus every enemy that gets hit, or leaves the screen, and in a much bigger game it would really start adding up.

    A little more information about it here from Senocular
  • 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.

    Wednesday, August 5, 2009

    Master! Master! Where's the dreams that I've been after...

    I'm an Experts-Exchange fan. It's a great place to get quick help on technology problems. Well, how kewl is this? I got the rank of "Master" on there, and now they're sending me a free t-shirt. Not often that places actually do give away something free, they didn't even ask for s/h... :)

    Bigfoot
    Master50,000 Expert Points
    Windows XP Operating System


    Wow, and Blogger.com totally screwed up the formatting on that signature...

    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()

    Monday, August 3, 2009

    Michael asked for it...



    MJW left a comment asking if I was going to post my game anytime soon. So by, umm... popular demand (Hey, 50% of my blogs' commenters asked for it!!) - here it is!

    The game gets difficult REALLY quick. But I don't really expect you to play it long. I'm just trying to get the basics down right now.



    http://bigfoot-avoidergame.googlecode.com/files/Avoider.swf

    Comments, banter, criticism? Close your browser...
    Want to lavish praise? Leave it in the comment section!
    Kidding... I want to hear it all - leave me a comment if you play. We'll call it "Comment-Ware"

    Wednesday, July 29, 2009

    MJW Avoider Game ep8ch1



    ...That'd be episode 8, challenge 1

    So now that he's shown us the magic of adding sound, he wants us to figure out how to create a Mute button on our own... sheesh. :) This was the easiest challenge, so far. But, so that I can solidify this in my own head, and in case you need help, here goes my thought process:

    The first step I wanted to complete, had nothing (directly) to do with sound. I wanted Flash to recognize another button was being pressed. Instead of being original, I picked "m" as my mute button... Feel free to pick L, meaning "Let the music stop", or ß (ALT+0223) for "This music was picked for the beta. If you don't like it you can turn it off."

    Going back and referencing my old code, I made these additions:

    All of them are in the AvoiderGame.as file.

    Create a publicly accessible variable (outside contructor)...
    1. public var mKeyIsBeingPressed:Boolean;)



    Initialize it in the contructor...
    1. mKeyIsBeingPressed = false;



    Inside the onKeyPress function
    1. if( keyboardEvent.keyCode == 77 )

    2. {

    3.     mKeyIsBeingPressed = true;

    4. }



    Michael let you figure out the onKeyRelease function, and a student is not above his teacher, so I'll let you figure that out too.... :P

    Then in the onTick function, I used a trace() command as a placeholder
    1. if( mKeyIsBeingPressed )

    2. {

    3.     trace( "You're pressing M!!!" );

    4. }



    Don't forget to "Disable Keyboard Shortcuts"

    ...and then watch your Output window. When you press m, it should fill up with "You're pressing M!!!" over and over...



    Ok, so it's working, I have a stream of useless output to prove it. Now I want it to actually do something useful! I created some new global variables:
    1. public var musicPosition:int;

    2. public var musicIsMuted:Boolean;


    musicPosition is an integer. It's going to be equal to where the music was muted, so that when it's unmuted it'll play from the same spot. I guess that makes it a pause button... so, sue me.
    musicIsMuted is straight forward. It's a true/false value of whether the music is muted.

    ...and then I initialized them, one in the constructor (musicIsMuted), and one in the onTick function
    1. musicIsMuted = false;


    and
    1. if( mKeyIsBeingPressed )

    2. {

    3.     if( musicIsMuted )

    4.     {

    5.  

    6.     }

    7.     else

    8.     {

    9.         musicPosition = Math.floor(bgmSoundChannel.position);

    10.     }

    11. }



    Ok, ok.... I did a little more then initialize it. I put together the basic if...else structure I was going to use. If the m key is being pressed, then if the music is already muted, do nothing right now, else check the position (in milliseconds) it was muted at, and put that into our variable, as a whole number (that's what Math.floor() does, if you don't remember)

    Now, let's flesh it out some. If the music wasn't muted already, what do we want to do? We want to stop the background music from playing, and we want to change our variable (musicIsMuted) which tells us if the music is playing to true.
    1. else

    2. {

    3.     musicPosition = Math.floor(bgmSoundChannel.position);

    4.     bgmSoundChannel.stop();

    5.     musicIsMuted = true;

    6. }



    If the player already muted the music, and now wants it to play again (cause they miss those jazzy synth beats!) then we would want it to play from where it stopped (we can do that by passing the play() function an argument), and change the musicIsMuted variable back to false.
    1. if( musicIsMuted )

    2. {

    3.     bgmSoundChannel = backgroundMusic.play( musicPosition );

    4.     musicIsMuted = false;

    5. }



    Now save everything and test your game... Easy, huh?
    Oh, wait, you say it keeps rapidly turning off and on? Crud! Well, we can fix that using some tidbits from Michael's sh'm'up tutorial.

    Create another global variable:
    1. public var ticksSinceMutePressed:int;



    Initialize it in the constructor:
    1. ticksSinceMutePressed = 0;



    and change your function to:
    1. if( mKeyIsBeingPressed )

    2. {

    3.     if( ticksSinceMutePressed > 10 )

    4.     {

    5.         if( musicIsMuted )

    6.         {

    7.             bgmSoundChannel = backgroundMusic.play( musicPosition );

    8.             musicIsMuted = false;

    9.         }

    10.         else

    11.         {

    12.             musicPosition = Math.floor(bgmSoundChannel.position);

    13.             bgmSoundChannel.stop();

    14.             musicIsMuted = true;

    15.         }

    16.     ticksSinceMutePressed = 0;

    17.     }

    18. }

    19. ticksSinceMutePressed += 1;



    If you haven't read that tutorial already, and can't piece together what I did, head over and check it out. No use in me re-inventing the wheel.

    Avoider Game Shmup

    That should do it. Now you can hear the 'thump, thump, thump' of the enemys coming on screen without that pesky synth-music playing in the background!

    Monday, July 27, 2009

    Disappearing Flash Window?

    I've had a bizarre problem happen with Flash CS3 twice now since I've been using it. I don't know what causes this but Flash disappears when I "restore" the window (when it's not maximized) It's really bizarre. I mean, I can work with it maximized just fine, but I don't want it to HAVE to be maximized.
    I tried:
    Right-clicking it's button in the taskbar and selecting "Move", and then using the arrow keys and/or the mouse. Didn't do anything.
    Right-clicking it's button in the taskbar and selecting "Size"... Didn't do anything.
    I think I tried reinstalling Flash the first time. (I'm in IT - it was like a personal challenge!) - Didn't do anything.
    Reset settings in Flash. Doing this caused my preferences to reset, unfortunately it didn't fix my problem. - Didn't do anything useful.
    This time I searched online and found the solution on Experts-Exchange.

    "I found this config file and renamed it:
    C:\Documents and Settings\%username%\Local Settings\Application Data\Macromedia\Flash 8\en\Configuration\panelset.xml

    Flash started again and all windows are visible again. The above file was recreated..."


    So I tried the same thing, since I'm using CS3 they changed the name of the file on us, it's now called "workspacelayout.xml" - when I renamed it (to .old) and restarted Flash, I was able to restore my window and it actually showed up!
    "Happy! Happy! Joy! Joy! Happy! Happy! Joy! Joy! Happy....."

    Friday, July 24, 2009

    Bee Stings

    Wow, I forgot how much bee stings can hurt! I haven't been stung in years, but was driving back from the gym earlier with my window open (I don't have A/C) A bee flew in, landed on me, stung me, and flew back out the window!
    Stupid bee...
    Yowch!

    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!

    Reclaiming my zeal!



    I've found an awesome AS3 tutorial! It's on Michael James William's blog and it's all about creating your own 'Avoider' game using Action Script 3... speaking of AS3 - holy cow it's different from AS2, this isn't some minor revision! It's like learning a whole new language. Kinda reminds me of when I was learning C++ because it's more OOP.
    Anyways, Mike's (can I call you Mike?) tutorial - which is using FrozenHaddock's Avoider Game tutorial and upgrading it to AS3 - is a great resource for learning AS3, and if you're just starting to play with Flash that's where you want to begin. So go there, check it out! Participate in the comments if you have a problem... I know I will be :)

    Included with the later 'chapters' of his tutorial are some 'challenges' - I love this part! The purpose of his tutorial isn't just to walk you through doing everything. Like a good teacher, he sets out some goals for you to try to accomplish on your own, and then provides support if (WHEN) you need it by means of the comments section.

    If you haven't gone through the tutorial yet, go do it! I'm going to post what I did on some of the challenges here.

    Chapter 7 Challenges
    One of the challenges is creating a second button on the menu screen, so that one would start the game with mouse control and the other with keyboard control. I mulled over this for three days!

    My first step was to duplicate the startButton in AvoiderGame.fla, I named the instance of the copy - kbStartButton. I also edited the button so that when I hovered my mouse over it it said "Keyboard" and the original startButton says "Mouse"

    My next steps were in creating an event listener for my 'keyboard control' button (I named the instance kbStartButton (Line 4 in the code below)

    1. public function MenuScreen()

    2. {

    3.     startButton.addEventListener( MouseEvent.CLICK, onClickMouse );

    4.     kbStartButton.addEventListener( MouseEvent.CLICK, onClickKeyboard );

    5.     Mouse.show();

    6. }



    You notice, I also changed onClickStart to onClickMouse.
    And also in the function...
    1. public function onClickMouse( event:MouseEvent ):void

    2. {

    3.     dispatchEvent( new NavigationEvent( NavigationEvent.MOUSE ) );

    4. }



    Plus a new function that we referenced in the event listener (onClickKeyboard)


    1. public function onClickKeyboard( event:MouseEvent ):void

    2. {

    3.     dispatchEvent( new NavigationEvent( NavigationEvent.KEYBOARD ) );

    4. }



    We referenced a .MOUSE and a .KEYBOARD navigation event, so go to our NavigationEvent.as file and add:

    1. public static const RESTART:String = "restart";

    2. public static const START:String = "start";

    3. public static const MENU:String = "menu";

    4. public static const KEYBOARD:String = "keyboard";

    5. public static const MOUSE:String = "mouse";




    Now, going back to the DocumentClass.as file, change your constructor to:

    1. public function DocumentClass()

    2. {

    3.     menuScreen = new MenuScreen();

    4.     menuScreen.addEventListener( NavigationEvent.KEYBOARD, onRequestStart );

    5.     menuScreen.addEventListener( NavigationEvent.MOUSE, onRequestStart );

    6.     menuScreen.x = 0;

    7.     menuScreen.y = 0;

    8.     addChild( menuScreen );

    9. }



    Go down to the onRequestStart function, and add this to the top:

    1. if(navigationEvent.type == "keyboard")

    2. {

    3.     useMouseControl = false;

    4. }

    5. if(navigationEvent.type == "mouse")

    6. {

    7.     useMouseControl = true;

    8. }



    Now, go to AvoiderGame.as - don't worry, we'll be back here in a minute!

    Create a variable for choosing the mouse, I called it iChooseMouse

    1. public var iChooseMouse:Boolean;



    and change the constructor for AvoiderGame to accept this new variable. While you're at it, add a line that makes useMouseControl equal to iChooseMouse.

    1. public function AvoiderGame( iChooseMouse )

    2. {

    3.     useMouseControl = iChooseMouse;

    4. ...



    Now, stroll back to DocumentClass.as and change every instance of

    1. playScreen = new AvoiderGame();



    to...

    1. playScreen = new AvoiderGame( useMouseControl );



    Save all your .AS files and give it a test run. Should work!