Categories
Flex

Passing State with a Timer

Early on in my Flex work I found many cases where I needed the function after a timer event to have some more information. In other words, I really needed to pass state of variables to the next function. You could do this by using global variables, but a cleaner way is to create a subclass of the Timer class and pass along an object.

To do this, you can create this actionscript class in your main mxml directory (or get fancy and name it within subdirectories) as ” ObjTimer.as” and the contents are simply:

// ActionScript file
// ObjTimer is just a timer that can hold an object of variables to pass along
package {
    import flash.utils.Timer;
    public class ObjTimer extends Timer {
        public var obj:Object = new Object;
        public function ObjTimer(delay:Number, repeatCount:int = 0) {
             super(delay, repeatCount);
        }
    }
}

and then you can access it from within your mxml like so for example:

var newTimer: ObjTimer = new ObjTimer(900,1);
newTimer.obj["myvar1"] = "test";
newTimer.obj["firstname"] = "joe";
newTimer.obj["count"] = 10;
newTimer.addEventListener(TimerEvent.TIMER, handleFunction);
newTimer.start();

private function handleFunction(e:Event):void{
    var value:String = e.target.obj["myvar1"];
    var fn:String = e.target.obj["firstname"];
    var count:int = parseInt(e.target.obj["count"]);
}

This example is really quick-n-dirty, you can make it nicer by adding setters and getters in the new class, but for an easy implementation this should get you going.

Categories
Fun Stuff

San Francisco Ready for Olympic Torch Protesters

Everyone knows how much of a ruckus the Olympic Torch is causing this year as it traipses its way across the world. Well here in San Francisco the officials did not want the same silliness that ensued in Paris so they are doing a variety of things, which apparently include helicopters, motorcycle police, police running, police on bicycles, buses full of police that pour out fresh policemen when the running ones get tired, police on horseback, and a big yellow car shaped like a boat.

Just over my house alone I spotted 10 helicopters, I assume a bunch of them are the media. But who knows? They could be packed with SWAT police ready to rappel down and save the torch. Photos below, click for larger versions.

Helicopters protecting the torch Helicoptors protecting the torch

The crowd was excited and peaceful, I did not see anyone charging the torch with a fire extinguisher or any other fire-intimidating device. Which is great, because, really, why protest the torch for crying out loud! The torch didn’t hurt anyone. And certainly the 80 year-old man carrying the torch in one hand, and his cane in the other, doesn’t need anyone yelling at him for the block or two he gets to participate. You want to protest China? Here’s an idea — go to the embassy. Trust me, you don’t want to cross the San Francisco police.

Police protecting the torch Police protecting the torch

Police protecting the torch A yellow boat-car protecting the torch

Categories
FaceBook

FaceBook Applications

I’ve been developing some applications on FaceBook and like what they’ve built as their API framework. It’s a work in progress, but is growing and getting better. My FaceBook profile is here, and I’ve built the following applications so far:


Ujogo Poker
: This is a more condensed version of the official Ujogo.com website, and allows the FB avatars to be shown, adding friends, seeing user’s profiles inside the app, and various alerts to users based on performance, etc. The backend code is done in J2EE using the Java API.


Confessions:
This is a game to let users create confessions that are put in front of their group of friends for judgement. The backend is done in PHP using the FaceBook PHP API.


The Driving Test
: A test-taking game where you can add your own questions into the database. The backend is in PHP. It is a good thing I have an approval process function for when people submit their own questions, some of the stuff people submit is crazy! Perhaps I’ll post some of it someday.

All the applications are Flash (SWF) developed in Flex, communicating with standard backend services.

Categories
Flex

DataGrid with Multiple Pulldowns

Here is an example that illustrates how you need to add some additional logic when you add ItemRenderers into a DataGrid. The way I came across this problem is I noticed that when I had ItemRenderers doing things like checkboxes or pulldowns, the choices that they were set to on a row basis would CHANGE whenever I scrolled or sorted a column.

I struggled with finding a way, as I talk about in another posting, and found that using the dataChange event does the trick, and I always create the ItemRenderer as a separate component instead of trying to put all the logic in the main mxml. The component gets the initial setting of the value for that cell via “.data” and then you need to maintain and check it within the component. This can end up with the coder doing some hacking, and my example is only that — an example. I’m sure there are more elegant ways to achieve this. But that’s one of the things I think Flex shares with Perl — there are always multiple ways to get something done!

Take a look at the first flash example, where I DON’T use the dataChange — then see how it works nicely on the 2nd example.

First example (Try sorting columns and see it randomizes each time):

This movie requires Adobe Flash Player — Download at www.adobe.com

Second example (Works!):

This movie requires Adobe Flash Player — Download at www.adobe.com

Download my code sample here.

Categories
Flex

Flex2 -> Flex3 font size issue

I switched to Flex3 the other day and found that the font sizing for the default font has changed! So on most of my applications where I
painstakingly sized buttons so that they exactly fit the text label, I now see the text does NOT FIT anymore and has “…” at the end!

The Flex folks have some good solutions, see more on Flexcoders.

Categories
Flex

Doesn’t seem like normal behaviour for DataGrid!

I ran into a problem the other day trying to create a DataGrid that had voting buttons as an itemRenderer. When an action was taken in that cell, the buttons would change, i.e., gray out or light up to show a click occurred. The problem that would happen is that, when I scrolled the list up or down, that column would randomize!

I found posts that talked about solutions for very simple things like a checkbox in a column that did not have any elaborate actionscript behind it, but not to address a component that sat behind the itemRenderer. I figured it out though, and the answer is pretty simple.

I had to add a dataChange function call in the component’s main tag so it would force the cell to rethink it’s status when the grid would scroll.

The main mxml:

<mx:DataGrid id="userGrid" dataProvider="{userData}" sortableColumns="true"
    draggableColumns="false" width="100%" height="100%">
  <mx:columns>
    <mx:DataGridColumn dataField="Topic" width="100" />
    <mx:DataGridColumn id="votecol" dataField="status" headerText="Vote" sortable="true"
        itemRenderer="castVote" width="120" />
    </mx:columns>
</mx:DataGrid>

The castVote component:

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" dataChange="gridMoved()"
    width="100%" height="100%">
<mx:Script>
        <![CDATA[
        import mx.core.Application;
        import mx.events.ListEvent;

        private var myStatus:int = 0;

        private function gridMoved():void{
                if (data != null) {
                        myStatus=data.status;
                }
                doShow(myStatus);
        }

        private function doShow(n:int):void {
                // 0 means already voted, 1 is for, 2 is against
                if (n==0) {
                        buttons.visible=true;
                        v1.visible = v2.visible = false;
                        return;
                }else if (n==1) {
                        v1.visible=true; v2.visible=false;
                        myStatus = n;
                        buttons.visible=false;
                }else if (n==2) {
                        v2.visible=true; v1.visible=false;
                        myStatus = n;
                        buttons.visible=false;
                }
        }

        private function doClick(n:int):void {
                data.status = myStatus = n;
                doShow(n);
        }

        ]]>
</mx:Script>

        <mx:Label id="v1" text="You voted FOR this resolution" visible="false" />
        <mx:Label id="v2" text="You voted AGAINST this resolution!" visible="false" />
        <mx:HBox id="buttons">
                <mx:Button label="YES" click="doClick(1);" />
                <mx:Button label="NO" click="doClick(2);" />
        </mx:HBox>

</mx:Canvas>
Categories
Fun Stuff

My iPhone

I love my iPhone! Don’t you love yours? I was what you might call an early adopter however, since I got mine in 1996. That’s right, the original iPhone. Luxurious large touch screen, full keyboard, speakerphone, and … 14.4k modem!

Yep, this thing was around a while back, spawned from the labs at National Semiconductor, launched by a startup I worked for, and then sold to Cisco Systems before they kindly gave up the domain name for an undisclosed sum. That domain name used to be registered in my name — I rue the day I assigned it over to Cisco 🙂

Categories
Fun Stuff

WTF Voicemail

My friend Steve threw a party a few weeks back and one of his uninvited guests left him this voicemail the day after. Wait for the image below and click it to hear:

[SWF]/wp-content/uploads/flash/lowe.swf, 100, 100[/SWF]

Think you know what the hell this guy is saying? Feel free to comment.

Categories
Musings

Blog is up finally!

I’ve had what you might call a blog since 1998 when back then we just called it a “web page”. I’ve been planning to create an official blog for the past few years but always had better things to do. Since I hadn’t updated my old “blog” for a few years I figured it could wait!

 But wait no longer, here it is!