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.