Archive for October 2008

ActionScript Code Snippets

I only started to learn ActionScript about a year and a half ago when I wanted to participate in Puzzle Pirates’ Grand Crafting Puzzle Project. I tried to learn Java at first, but I didn’t get far.  Flash with ActionScript was a lot easier for me since it was much more visual.

So from time to time, I’ll be posting ActionScript Code snippets that I think will be helpful to beginners as well. This is the first entry.

EXAMPLE: Flipping a Card

When the green button is clicked, the card is flipped:
If the movie doesn’t show, click here instead. (Wordpress gets kind of annoyed with Flash on the front page.)

FILE: Click here to download the FLA file.

EXPLANATION:
The card is a MovieClip that has two frames - the front (Two of Spades) and the back. When the green button is clicked the “flipClip” function is called.  This starts flipping the card by calling the “flipFront” function which sets up the timer and two listeners.

The first listener (TimerEvent.TIMER, changeXScale) is called on every Timer tick and causes the xScale of the card to decrease by 0.1. There are only 10 ticks so the timer stops when the xScale reaches 0.

The second listener (TimerEvent.TIMER_COMPLETE, flipBack) is called once the Timer has finished its 10 ticks.  At this point the card has shrinked horizontally to an invisible line (since the xScale is 0) and “flipBack” function is called.

The “flipBack” function changes the frame of the card clip. If it’s showing Frame 1, it changes to show Frame 2 and vice versa. It also multiplies the “flipTracker”  by -1. This number determines whether the card is shrinking or expanding.

Similar to the “flipFront” function, the “flipBack” function creates a new Timer and adds two Listeners to it.

The first Listener is called on every tick and since the flipTracker is now positive, the xScale is increased by 0.1 each tick. This continues until 10 ticks have passed and the xScale is 1.0

The second Listener is called at the end of the Timer and calls the “cleanUp” function. This just removes the listeners so we don’t cause a memory leak.

CODE:

var flipTimer:Timer = new Timer(30, 10);
var flipTracker:int = 1; //Determines whether the card is “shrinking” or “expanding”

flipper_mc.stop();

flip_btn.addEventListener(MouseEvent.CLICK, flipClip); //This it the green button

function flipClip(e:MouseEvent) {

	flipFront()
}

function flipFront() {

	flipTracker *= -1;

	flipTimer = new Timer (30,10)
	flipTimer.start()
	flipTimer.addEventListener(TimerEvent.TIMER, changeXScale)
	flipTimer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack)
}

function flipBack(e:TimerEvent) {

	flipTracker *= -1;

	if (flipper_mc.currentFrame == 1) {

		flipper_mc.gotoAndStop(2);

	} else {

		flipper_mc.gotoAndStop(1);

	}

	flipTimer = new Timer(30,10)
	flipTimer.start()
	flipTimer.addEventListener(TimerEvent.TIMER, changeXScale)
	flipTimer.addEventListener(TimerEvent.TIMER_COMPLETE, cleanUp)

	}

function changeXScale(e:TimerEvent) {

	flipper_mc.scaleX += 0.10 * flipTracker;

}

function cleanUp(e:TimerEvent) {

	flipTimer.removeEventListener(TimerEvent.TIMER, changeXScale)
	flipTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, cleanUp)

}

Bar Ratio going up!

I noticed yesterday that the Coins to Bar ratio went up by 100 Coins and I’m not sure why. There wasn’t a significant release yesterday.  It also looks like the ratio is going up a bit faster than before the bump. It makes me wonder if someone cashed out a bunch of Bars.

Making them Multiplayer

Well, I’ve started working on Blackjack Square and I still think this will be a pretty easy game to code. The game will definitely be uploaded sometime in early to mid-November.

However, the other big thing I want to work on is making my current games multi-player. My very first experience with multi-player games was Paint Factory Challenge and that turned out to be a total bust. It’s barely played at all - only about 1200 games so far. It was also an unbelievable pain in the butt to code and test since I use Flash CS3 to develop my games. Basically I had to log on to Whirled every time to test the multi-player aspects of the game.

When I originally thought up of Auction Poker, I intended it to be multi-player but I really did not want to go through the hassle of creating another multi-player game and have it end up unplayed. There are a few good multi-player games on Whirled that end up minimally played. Games such as Zyraxxus, Spades and Gomoku. I’m speculating here that part of the reason for that is that these games require another person to play. Games that have single player modes, in general, seem to get a lot more play.

However, I’ve learned a lot since I coded Paint Factory Challenge several months ago and I’m writing up a few classes and tools that will help make multi-player games a lot easier for me. I also think that many of my games can benefit from playing with other people - it’d be awesome to have 6 people playing Poker Square at a time in a challenge mode.

So basically, once Blackjack Square is done. I’ll be going around my other games and making them multi-player. The trickiest game to make multi-player will be Auction Poker and I’ll be leaving that one for last. I still haven’t quite figured out what to do with a player that’s elimnated in the first round. Will they just sit there, waiting for the next round? Or will I need to re-work the elimination of multi-player to make it optional? I suppose I’ll figure it out.

Making the AI, Part 2

Four months ago I wrote this post regarding the AI for Auction Poker. A lot has happened since then but I do plan to continue the discussion. Anyways, here’s Part 2.

In the last post I mentioned that the AI selects cards by their “affinity” to each other. I also mentioned that this doesn’t always result in an optimal hand. Sometimes, selecting the card with the highest affinity is detrimental to the hand - for example, selecting a second Queen after having a suited Queen, Jack and King. What was a possible Royal Flush is now at most a two-pair.

To remedy this situation, additional information was needed when calculating the affinity of the hand. Remember that affinity is based on the value of all the hands two cards can make. Once a hand is not possible to make anymore it is removed from the affinity calculation.

For example, if the AI holds a Pair of Fours, it cannot make a Royal Flush, a Straight or a Flush and those hands are not included in the calculation. Thus, the affinity between the Jack of Clubs and the Queen of Clubs drastically decreases since a lot of their value comes from a Royal Flush and Straight Flush.

Since calculating the affinity between two cards is a pretty heavy calculation to do every single time an AI player makes a bid, I pre-calculated the affinity for each pair of cards and placed them in tables for easy lookup. There are five tables for the following conditions:

1. All hands are possible.
2. Royal Flushes are not possible.
3. Straights are not possible.
4. Flushes are not possible.
5. No flushes or straights are possible. (Focus on pairs, trips, etc.)

This image, which I also used in previous post, shows the affinity between suited cards when all hands are possible. (The brighter the green, the higher the affinity.)

Once Royal Flushes and Straights are not possible (Chart #3), the affinity chart for suited cards looks like this:

At this point, the rank of suited cards is irrelevant - Straights and Straight Flushes aren’t possible anymore. Any suited card can be used for a Flush, that’s why the chart is so flat.

With these adjustments, we can update the basic procedure for selecting cards by the AI:

1. If the AI has no cards in hand, the AI checks the deck for the cards with the highest affinity.
2. The AI then determines which affinity table to use based on those two cards.
3. Using that table, the AI calculates the affinity for the remaining cards and picks the best one.
4. This process continues until the AI has filled out its hand.

However, with this process the AI will still select “bad cards” on occasion. For example, in the case below, the AI selects the King of Clubs and Ace of Clubs as its initial cards but then goes on to select the King of Hearts, the Seven of Clubs and the Seven of Hearts. These last three “bad cards” are not a result of bad calculation, there just aren’t any better cards on the board.

So where do we draw the line? The first two cards give us a nice chance for a Royal Flush, but having two Kings and a Ace isn’t bad either - we could end up with a Full House.

To figure this out,  I tried to think of how I would play it. If I had the same set of cards, I would only select the King of Clubs and the Ace of Clubs and ignore the rest.  Basically,  I had to tell the AI which cards to ignore. This turned out to be a lot more subjective than I realized and which I’ll discuss in Part 3.

Tracking Coins/Bar Ratio

I’ve been making a few updates to this new page here that tracks the Coins/Bar Ratio in Whirled. It’s mainly for my own curiosity. I went back and forth on figuring out the best way to track it easily and I finally settled on a combination of Google Docs and a Google Gadget. The best part is that whenever I enter the information in my spreadsheet, it’ll automatically update the graph!

A couple of disclaimers so far, though-
*For the first few days, I had to extrapolate since I didn’t write down the data.
*For the first week or so, I only checked the ratio once a day, at around 6:00 p.m. Pacific.

However, since it’ll be remarkably easy to track this information now, I’ll be trying to update the data more than once a day, if I have time. The full set of data is available on a spreadsheet here.

More Bling!

I noticed that as of yesterday, game creators on Whirled are receiving Bling! Below is a shot of my first Game Bling.

One game in particular is bringing in much more bling than the others - 16 points. I don’t know for certain which one it is, but I’m pretty sure it’s Paint Factory since it is my most popular game.

Poker Square is also quite popular, much more than Cribbage square so I’m guessing that’s where the 5 points of bling came from.  Auction Poker and Cribbage Square bring up the rest. I also have Paint Factory Challenge listed but it’s not really played so I don’t really expect any real bling from it.

I’m currently working on fixing up some stuff on Auction Poker and I’ve already started writing the next two entries on the Auction Poker AI, but I just need to do cleanup before publishing.

A warm little hotspot.

So I walking in downtown Oakland and I came across this rather bright hotspot on the ground. You can see it on the sidewalk in the picture below. The building adjacent to it is pretty new and has a curved facade which is focusing the sunlight into that small area.

I stepped into it just to see how warm it was and I was surprised by how uncomfortably hot it was. It felt like it was at least 100F there.  I should’ve gone back with a thermometer to see how hot it actually was. I took this next picture looking up from the hotspot to the building - you can see all the reflections of the sun. That’s a hell of a lot of reflected sunlight.

I’ll have to remember never to design a curved window facade with a southern orientation.

Bling!

So Whirled came out with Bling earlier in the week and it has me quite excited! I can start making a little bit of cash while making fun stuff. I also noticed that game creators will also receive bling in the future! I’m definitely going to be spending more time creating stuff on Whirled - especially games. I have quite a lot of notes about possible games. I just have to hunker down and do them.

By this weekend, though, I’ll be releasing the flower toy along with some “seed packs” that will allow the player to pick some standard flowers instead of relying on random flowers.

In the next week or so,  I’ll also be tying up some loose ends in Auction Poker and start making Blackjack Square.

I have been thinking of Blackjack Square for a while, after all it seems like a logical extension of Poker Square and Cribbage Square. However, the problem with Blackjack is that there is no predetermined hand size - a 21 can be made with 2 cards, 5 cards or even more. Fitting them to a standard square is a bit tough.  However, a really obvious solution on how to fix this problem came to me when I was on Whirled. It was so obvious I was really surprised that it didn’t come to me earlier.

So Blackjack Square will definitely be coming before Gravitation. It’ll be a simple matter of reusing a lot of the code and graphics from Poker Square- just a weekend project. Hopefully I can have it done around the beginning of November or the end of October.

Also, here’s a screen cap of my very first bling - hopefully the first of many.  It’s worth about 2 cents!

Working on a new toy.

Here’s a quick glance at the new toy I’m working on. It’s basically a flower with lots of different variations. I’ve already coded in 11 different species, 10 different leaf types and 32 different color patterns! Some of the color patterns are slight variations of each other and some look random until you have the right number of petals.

This image below shows 16 brand new flowers:

However, the funnest part is cross-breeding the plants! With a few clicks you’ll be able to cross-breed the plants to get more interesting color combinations! For example cross breeding the two plants on the top leads to tons of different variations:

The cross-breeding is important since some color combinations and patterns will not be available from a new flower. When a new flower is made, it’s colors are selected from one of three groups - the yellow-orange-red group, the blue-purple group or on occasion the black-green group. The reason for this, is that each color in a flower’s petal is made of four genes. Each gene is assigned a specific color and the final color is combination of all four. If I had randomly selected the color for each gene, many times I ended up with brownish or sickly looking colors. However, by selecting colors from each group I can be pretty sure that the colors will work better together.

Anyways, I’m finishing up some of the Whirled signaling/memory code so I’m expecting to release this toy sometime during the weekend.

Last panoramas of Hawaii

Laie Point facing south.

Laie Point facing east.

As you can notice, I’ve also added the Flickr photostream on the sidebar. Most of the newer panoramas are going to be placed in there instead of here on the blog.

I have a lot more pictures there! Check them out.