Tuesday, October 19, 2010

Making a Custom Mouse Cursor in Flash with Actionscript 3

In most games, you will see a custom mouse cursor that the developers created to make the mouse seem like part of the game world.  For Stack-O-Lantern, I didn't want to have the standard mouse pointer while playing the game because I wanted it to feel like you were actually grabbing the characters and picking them up.  Luckily, as I found out, this is very easy to do in Flash and Actionscript.


Since I decided that I wanted my mouse cursor to look like a hand, I went into Flash and created a MovieClip with two frames:  one where the hand has all fingers and its thumb stretched out, the second with a clinched fist.


Open Hand (Frame 1)

Clinched Fist (Frame 2)

I also added a little dark gray outline to both frames so that the hand would stand out against a white background.  Make sure when you create the MovieClip that you check the option Export for Actionscript; this will ensure that you can use it in your code.  If you've already created your MovieClip and didn't set a class name, just right click the MovieClip in your Library and click Properties.  Then click the Advanced toggle to drop down the options you see below.  Click Export for Actionscript and put in your Class name.  I named my Hand object as seen below:


Hand MovieClip properties

We are now ready to use this thing in our code.  First, in your main document class, create a class member that will represent your mouse cursor.  I made the object at the class level so that I could reference the cursor wherever I was in the code.


private var _mouseCursor:MovieClip;

In Stack-O-Lantern, I wanted the user to have a normal mouse cursor while they were navigating the menus, so I didn't display the hand until I got into the actual game.  Here is the code to make the switch between the normal cursor and your custom cursor:

// This is a general function that might be used when your
// game is loaded and the user is presented with the actual
// screen.
private function startGame():void
{
    // Instantiate the mouse cursor object
    _mouseCursor = new Hand();

    // This will keep the cursor from going between
    // frames at a rapid rate :)
    _mouseCursor.gotoAndStop(1);

    // Add the mouse cursor MovieClip to the stage
    // so that it is visible
    stage.addChild(_mouseCursor);
    
    // Hide the generic mouse cursor
    Mouse.hide();
}

You also need to create an event listener that is called every frame; a good one for this Event.ENTER_FRAME.  In your document class' constructor, add this line of code:


addEventListener(Event.ENTER_FRAME, onEnterFrame);


Create the actual method that handles this event:


private function onEnterFrame(e:Event):void
{
    ...

    // This will make the mouse cursor MovieClip follow the
    // mouse coordinates
    _mouseCursor.x = mouseX;
    _mouseCursor.y = mouseY;
}

To get the cursor to change frames when you click the mouse, you need to create two mouse event listeners:  one that checks when the mouse is clicked, and the other when the mouse button is released.

stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

Now, you need to put some simple code in each of these methods to change the frames of the MovieClip:

private function onMouseDown(e:MouseEvent):void 
{
    _mouseCursor.gotoAndStop(2);
}
private function onMouseUp(e:MouseEvent):void 
{
    _mouseCursor.gotoAndStop(1);
}

You now have a functioning custom mouse cursor!  That wasn't too bad was it?

If you want to turn the generic mouse cursor back on, you can use the following code:

Mouse.show();
stage.removeChild(_mouseCursor);

...or, if you don't want to dispose of your custom cursor, you can opt to just hide it: 


Mouse.show();
_mouseCursor.visible = false;

Let me know if you have any questions or comments...I'll be posting more tutorials real soon.




















Unity 3 Engine Released!

Not that this has anything to do with Flash or Actionscript, but if you are into game programming, you need to check out this engine.  I've been following Unity for quite a while, even when it didn't have a free version.  It is absolutely amazing, and with the 3rd installment of it, the graphics and ease-of-use are incredible.

Go give it a look and download the free version:  http://unity3d.com/

Monday, October 18, 2010

Reactions to Stack-O-Lantern on Kongregate

The good news:  I got over 500 plays in less than three days and I was on the front page of Kongregate for a small period of time.

The bad news:  I don't think anybody "got" the game - in fact, the few people that rated it, hated it. Do I understand why?  Not really, but I am biased.  Here are my thoughts as to why it did not succeed on Kongregate...

  1. It demanded that you think about what you are doing.  A lot of games out there are mindless point-and-click games that reward you heavily for not doing much.  Yes, I'll admit, this game is a little tricky, but after playing it a couple of times, you should be able to get it.  There seems to be an alarming trend these days of people "wanting all and giving nothing" - this mook behavior seems to bleed into the Flash game arena and it's a little disappointing.
  2. They just didn't get the concept.  One person said that they absolutely didn't understand the concept of the game - I didn't think it was that in-depth.  You are taking characters out of a stack and placing them on a floating icon.  The instructions on the Kongregate page state how to play it; I even took the time to create a little how-to page inside of the game. I am very aware that the game is not your ordinary puzzle game, but the concept of dealing with a stack of things is very basic and understandable to me. Can anyone say Jenga?
  3. They thought that the physics were off.  I can promise you that the physics aren't off - the Box2D engine is very good and I used a lot of settings to make it behave the way I thought it should.  When you stack a lot of objects in real life, the stack tends to become more unstable the higher it gets - the game behaves this way as well.  Also, when you try to pull a box that is halfway down the stack, the other boxes are going to be affected; whether they fall or get knocked, they are going to move.  I went back and tweaked some settings to see if it was me being biased about the game or if the user's were too sensitive - the more I tweaked the settings the less realistic it seemed to me, and I decided to leave it alone.  So, I really don't understand this complaint, though everyone complained about it - I could go into the math behind it and try and justify my opinion, but what's the point? They wouldn't get it anyways.
  4. They hated the scoring model and believed it was buggy.  Again, this is something that baffled me.  One person commented saying that because his score was low the game was buggy.  I clearly stated in the instructions that dropping characters on the ground will hurt your score.  If you drop characters, that is something that can't be rewarded; and to make it more challenging to get some of the achievements, I thought that you should take great care not to drop anything.  I wanted an extra challenge and the players did not respond well to it, oh well. But without the challenge of losing points for dropping characters the game is way too simple. It's kinda like playing bowling with the gutter bumpers. It's kinda fun but gets boring real fast. Within the first few hours I had a few people blowing up the high scores ranks. So I know that my scoring model was effective and it was possible to do well.
I got a lot of "fail" comments but that is somewhat expected when you put content on the web. Sometimes there are people out there that just want to put everyone and everything down. I looked at these people's comment histories and it basically was the same comment for every other game. The funny thing about people like that is that they are incapable of doing anything themselves, and that to me, makes their opinion invalid.  I welcome constructive or valid criticism and I wish that I had received more of that instead of the hateful comments.

Overall, I couldn't be happier with my little game. My wife and I put a lot of thought into the character and level design, and to be honest, I thought it was a cute game that would at least allow people to have a good time for 5 minutes out of their day.  Is it a perfect game?  Of course not, and I will be the first to admit to that.  But it taught me a lot and it has just motivated me even more to make games. I have so many ideas for other games that I don't know what to start on next. Whatever it is, I know I will have fun with it. And I hope that some people can get enjoyment out of it.


Released Stack-O-Lantern on to Kongregate

I am happy to say that I released my first game on Kongregate, called "Stack-O-Lantern."  As I've said in previous posts, it is a game where Halloween characters are stacked on top of each other and it is up to you to get them out of the stack without dropping them.  Here are some screen shots of the finished product:


Title Menu

Scene Selection Screen
Note: Every title you roll over changes the look of this scene

 Cemetery Scene

 Fall Time Scene

There are a total of five scenes covering all types of Halloween or Fall-themed places.  Each level, except for the first one, has a particular challenge about it that makes it a little harder to get a high score or low time.  For instance, the Cemetery scene pictured above, has all of the characters sitting on top of a teeter-totter that will go out of balance if you are too reckless.  The Fall Time scene pictured above has little acorns that fall out of the trees and hit your pile of characters.

I also added sounds, a custom mouse cursor, achievements integrated with the Kongregate API, and some neat physics effects.  Overall, it was a simple  game that taught me way more than I thought it would.  I will be posting some tutorials based off what I learned and incorporated in the game.  I will also post what the reactions to the game are.

Monday, September 20, 2010

Update on Halloween Game

Amidst work and life, I have managed to get quite a bit further in the development of my Halloween stack game.  My wife and I have drawn almost all of the Halloween-related characters and I am starting to experiment with how the user will interact with the stack of the characters. 


I think tweaking the mouse grab and the way the characters lay on each other will be the biggest challenge coming up.  Below is the latest screenshot of the game:




I should have all of the characters done this week and I will let you know how I solve the mouse grab issues.  See ya soon!

Friday, September 10, 2010

Halloween Game in the Making!

In the upcoming spirit of Halloween, I have decided to make a little casual Flash game that involves a pile of characters stacked together in the center of the screen.  The goal of the game is to get all of the characters from the pile into a box that you drop them into.  

Think of a Jenga-type game, except that the blocks are actual little faces of Halloween characters.  A little indicator box will appear in the top left part of the screen that tells you which character you need to pick out of the pile and put into a box.
  
Since the pile is made up of several different characters, the one you need to pick may be buried in the middle of the pile, or even on the edge.  Do you re-stack some of the characters to try and reach the one you want, or do you risk the whole pile falling over by trying to drag the character out?  If the pile falls over, you lose the game.  I think there will be a point system or a timer that can keep the tension up.  I might try and include achievements for not letting any characters fall to the ground, or something along those lines.

I am using the QuickBox2D library for my physics engine, and a little routine that makes the mouse cursor turn into a hand that can grab and drag the characters.  My wife has been helping with the character design and scene concepts, and I think it is coming along real well so far.  Any time you can involve your wife in making a game, life sure does start to feel good :)

Below is a very, very early version of a creepy Halloween level that I am making:


The pile is not constructed yet, but I have put a few test objects in there to see how it would look once I get more characters drawn.  I will keep posting as I get further along!

If there are any of you out there reading this, let me know what you think so far.  I would appreciate any feedback.

Thursday, August 26, 2010

The Stupidity of IT

This is an off-topic post about something that I witnessed not too long ago at my job.  

I happened to be walking by one of our conference rooms and noticed something really peculiar. There were about 20 overdressed guys in suits, all sitting around two tables that they had joined together.  The projector was displaying something on the screen, yet all of the conference room lights were on (insert "here's your sign").  Everyone was shaking their heads in what seemed like a synchronized bow of approval, gawking at what was on the screen in front of them.


Given this mass reaction, I half expected to see something that would be mind-blowing, or a cure for some disease...I even would've been OK to see some new software that a company was soullessly trying to sell to us for thousands of dollars.  But, alas, I saw one of the dumbest things I have ever seen in my entire life - please look below:


No, I didn't leave out any additional text that might make this grid make any more sense.  It was really this stupid.  I don't know what they could've been talking about, but this, regardless of the conversation, is just plain asinine.  

Do you ever question your job?  

I did that day, and I have been laughing (and crying) about this ever since.

Wednesday, August 11, 2010

Level Loader Tutorial in AS3 using Function Pointers

In a game you often come across the need to change levels.  In my case, while making a mini-golf game, I obviously have the need to go from hole to hole as the player makes their shots.  Since I load my level content through a method call, such as loadLevelOne(), I needed a way to be able to make those calls without explicitly knowing what order to call them during runtime.


AS3 has a very easy way to do this that I think is worthy of a quick tutorial.  Since a function that you declare in a class is of type function, you can pass them around like objects or, as in my case, store them in an array that you can cycle through.  You only need a couple of variables and a few utility methods to do this.


Let's say you have two levels to load for your game and you have defined them somewhere in your code, as follows:


private function loadLevelOne():void
{
...
}


private function loadLevelTwo():void
{
...
}


As members of your class, you can make the following variables:


// Levels array that stores Functions
private var _levels:Array;

// A variable to keep track of where you are in the array
private var _levelIndex:int; 


In the constructor of your class, or in a separate method, make sure to initialize your variables, and then "load" the functions in to the array:


// Initialize the levels array
_levels = [];


// Set the level index
_levelIndex = 0;


// Load the method calls into the array
_levels.push(loadLevelOne);
_levels.push(loadLevelTwo);


You could also do this more explicitly if you wanted by creating a variable of type Function, setting it to the name of the function, and pushing that into the array, like this:


// Function pointer variable
var functionPointer:Function;


// Load levels 
functionPointer = loadLevelOne;
_levels.push(functionPointer);


If you didn't know the exact order you wanted to load your levels, or if your situation for loading levels occurred in a more dynamic nature than in my mini-golf game, then the preceding example may be a better fit for you.  In order to get through the levels and actually call them, I created the following utility methods:



// Runs the current level
private function runLevel():void
{
   // Check to make sure that your levels array has data
   if (_levels.length > 0)
   {
      // Get the function pointer out of the array
  var currentLevel:Function = _levels[_levelCounter];
  
      // Call the method
  currentLevel.call();
  }
}

// Switches to the next level
private function switchLevel():void
{
// Check that you are still in bounds if you switch levels
  if (++_levelCounter < _levels.length)
  {
      // Get the function pointer
  var currentLevel:Function = _levels[_levelCounter];


      // Call the method
  currentLevel.call();
  }
}

// Retry the current level
private function retryLevel():void
{
// Get the current level
  var currentLevel:Function = _levels[_levelCounter];


   // Recall the method
  currentLevel.call();
}


So, hopefully this is of some use to you out there if you ever needed to use a structure like this. There are a lot of other uses for this as well, for instance, if you had an RPG like Diablo 2 or World of Warcraft.  In those games, your character often has spells (good or bad) that affect the player over time. You could set this up as an array of function pointers that get cycled through per frame, with each function handling the different spells that are cast on you.  Of course, that may be a horrible way of handling it, but at the very least, it is some food for thought.

See you next time!

Tuesday, July 20, 2010

Todd Kerpelman's Box2D tutorials and FlashDevelop

I've been getting into Erin Catto's Box2D physics engine recently (www.box2d.org) and I have found some great tutorials for the Flash version from Todd Kerpelman.  He has put together 22 really awesome videos on the basics of how Box2D works and how to integrate the engine into your games.  Even cooler is the additional 50 videos he has to create your own version of a PopCap game Peggle Nights (cleverly titled Puggle) using Box2D.  I am in the process of going through these myself and I amazed at the quality and the amount of time he put into them.  Please go check them out at:  http://www.kerp.net/box2d/


Another thing that Todd mentions is the use of FlashDevelop in tandem with the Flash IDE.  I think FlashDevelop is awesome because it provides a lot of shortcuts to increase your coding time and has a form of Intellisense when you are coding.  Anyone who has coded within Flash would appreciate this because the Flash IDE leaves a lot to be desired for developer who is used to Microsoft's Visual Studio or something like that.  If you watch the videos, you can see how Todd uses both environments.  You can get FlashDevelop from the following link:   
http://www.flashdevelop.org/wikidocs/index.php?title=Main_Page

My recommendation would be to let the installer follow its basic installation routine - I had no problems with this and have been using for a while now.

Anyways, I just wanted to give some props to Todd Kerpelman for providing a great developer resource.  We need more people like him out there.

Sunday, July 18, 2010

A Start

Hello!  This blog is just a fun way for me to express to you my interests in game programming - with a little bit of humor and other ramblings thrown in.  I am going to post some tutorials and code that you can use, as well as, link to people's websites that are way better at it than I am.

I believe that a great way to learn game programming is through example, and it is how I have learned what I know so far.  That being said, I will attempt to do all of my tutorials that way.  I am by no means an expert, and I don't claim to know the best way to do these things.  But, at the very least, I hope to give some inspiration to those out there who have a passion for creating games.

Please leave comments and feedback.

Enjoy!