Ted's Pack of Cards in SWX

This demo requires Flash.

I was looking for a distraction from the PayPal integration stuff I'm working on today for my Super Secret New Project (tm) when I ran across Ted Patrick's Cards API post from yesterday and decided to quickly implement a SWX API for it.

Ted has created a very simple REST API for a deck of cards. It currently has one method which returns a shuffled deck (or decks) of cards.

The SWX API for it was a cinch to write. Here's the PHP code for it:

I wasn't sure if the timestamp was in there just to stop caching or whether it would be useful so I left it in :)

You can play with it online using the SWX Service Explorer on the Public SWX Gateway.

The demo SWF you see at the top of the post calls the service using the following code:

import org.swxformat.SWX;

var swx:SWX = new SWX();
swx.gateway = "http://swxformat.org/php/swx.php";
swx.encoding = "GET";
swx.debug = true;

var callDetails:Object =
{
    serviceClass: "Cards",
    method: "shuffle",
    result: [this, resultHandler]
}

// ... other stuff (UI, etc.)

swx.call(callDetails);

And, since the data is returned in a native SWF array, the result handler simply iterates over the array to display the cards:

function resultHandler(event:Object)
{
    var deck:Array = event.result.deck;

    for (var i:Number = 0; i < 52; i++)
    {
        var cardInit:Object = {_x: x += 50, _y: y, _width: 50, _height: 75};
        if (i%12 == 0)
        {
            cardInit._y = y += 75;
            cardInit._x = x = 0;
        }
        _root.attachMovie(deck[i], "card"+i, i, cardInit);
    }
}

Don't forget, like all the other SWX APIs, you can call this API from Flash 6+ (including FlashLite 2.0 and 2.1) using ActionScript 1 or 2 (not 3 yet).

Download the demo (zipped; 2.6MB).

The demo includes the SWX ActionScript Library which comes with other SWX sample apps that you can play with.

Comments