Blog

  • Flash Player 9 Unacceptable JPEG’s from phpThumb

    Using phpThumb at work. You upload a video file, and the server generates a thumbnail from the video via phpThumb. Here’s the bad news: The thumbnail doesn’t work in IE6 and IE7 on Windows in Flash Player 9. Everything else works (Firefox, Windows Safari (sometimes), Opera, and everything works on Mac). Flash Player 8 works, but AS1/AS2 Flash Player 9 (compiled by Flash CS3) does not.

    So, something changed in Flash Player 9 that affects the JPEG rendering because the JPEG’s work fine in everything else. It’s only a big deal because the biggest feature of Flash Player is backwards compatibility; aka, Flash Player 1 content should work as intended in Flash Player 9. Naturally everyone and their mom will blame phpThumb doing something different with headers, etc. “Enterprise company with a 10 year-old rendering engine vs. an open source image generator… um… yeah.”

    As soon as we get our code pushed to production, I’ll make a few test cases for the Adobe engineers, but in the meantime, anyone else experienced this? If so, and you are reading this blog from a Google search, hopefully we’ve posted a solution in the comments, hehe. As of today, the server guys got it working somehow (aka modifying phpThumb’s base code, something about headers :: shrugs ::). Really really frikin’ hard to test with caching, etc. so it’s been long and bloody.

  • 10 Tips For Working With Cairngorm

    These apply to Flex 2.0.1 with any version of Cairngorm. Since people’s programming background varies in Flex, these are not facts, just my opinions based on my experiences using ARP & Cairngorm for over 2 years. Furthermore, there are alternatives to Cairngorm, I just cannot try them as much as I like. Now that I’m doing product work, I can’t just “try this framework on this new project”. I live in the same code base longer supporting existing clients, and can’t do dramatic re-factoring without getting fired.

    1. If Cairngorm looks complicated, don’t worry, it is, and you’re not alone

    “Bunch of code on _root vs. all of these classes and conventions? This’ll take forever!”. It takes getting used to. I felt “comfortable with Cairngorm” on my 4th project with it. To this day, I still mod it though, and try different things. As more people come into Flex, there are more cool ideas and techniques floating around.

    2. If it feels like it’s taking a long time just to do something in Cairngorm, it does.

    “OMFG… 3 classes just to execute what would take me 1 line in Flash? Lamesauce!”. Code gen can help here, at least at the beginning of the project.

    3. Only Commands set data on the Model; you can break this, just don’t if you can help it.

    In Model View Controller, only the Controller sets data on the Model. In this case, the Commands are the Controller (usually), and as such, setting ModelLocator data is their job, and their job alone. If data is getting f’d up, you immediately know it’s in the Command. You never have to question “who’s setting my data, where, and when?”.

    4. Delegates make the server call and parse the data (sometimes in a Factory)

    This has only happened once in my career: I was hitting PHP for the back-end, parsing XML, and in the middle of the project, we switched to OpenAMF and Java where I had to parse objects. Using Delegates means you only have to re-code your Delegates, not the rest of your app. The Commands still get the same data.

    The only reason I use Factories is because A) parsing can be a lot of code and you don’t want to make your Delegates look more complicated than they are and B) you can share parsing routines easily in the same Factory class.

    5. If you see a Command parsing data, it’s coded wrong.

    Parsing is differently from filtering, modifying, and assembling. IE, making Value Objects from XML should be done in the Delegate, not the Command. Making associations between ArrayCollections, injecting default values are both ok. Remember, the key here is if the Command is touching raw server data, you’re either not using Delegates correctly, or have AMFPHP/FDS/WebOrb working correctly, hehe.

    6. There are 3 ways to use Commands & Delegates. I prefer A because it’s consistent, leads to short class files, and is very explicit.

    A) For every use case, you make 1 Command and 1 Event. This can sometimes also mean 1 Delegate. (ie, LoginEvent, LoginCommand, LoginDelegate)

    B) For every use case that’s related, you can consolidate them into a package path. So, Login, ChangePassword, and ForgotPassword would all be in the same class. You’d then use constants to determine which one to run. (ie, LoginEvent has LOGIN, CHANGE_PASSWORD, and FORGOT_PASSWORD as String constants. Your LoginCommand has a switch statement which determines which one to run. Your LoginDelegate has 3 methods; login, changePassword, and forgotPassword that your Command can use.

    C) Variances on B. Maybe 1 Event and 1 Command, but multiple Delegates. This is not a cop-out bullet item, rather, I’ve seem many derivatives that can all be traced back to “B with mods”.

    7. ViewLocators are considered bad practice.

    That said, there are developers who still love and use them. The use case, however, is valid: Having a View “know” when something in a Command has occurred. I use callbacks, some set data on the Model to trigger callbacks on the Views (maybe via a setter function), and some use addEventListener in tandem with CairngormEventDispatcher.

    8. ViewHelpers are considered bad practice.

    That said, there are developers who love the idea of “code behind”. I’ve yet to see a consistent theme on the blogs and mailing lists. There are 2 reasons that are common:

    A) They don’t like mixing ActionScript & MXML.

    B) They want to separate their View’s from their “View controller code”

    Some use the script tag to point to an external file (lame in my opinion; you have to define your controls as member variables and you end up with twice as many View classes). Some use inheritance where you extend your GUI MXML. Others take the reverse, and have the GUI MXML extend the ActionScript ViewHelper. Some will even take the Composition over extending MovieClip approach I’ve seen a lot of coders do in Flash. Instead of their ViewHelper extending a view class, it’ll instead take a UIComponent as a parameter, say in an init method, and use that UIComponent via composition.

    To me, you’ll get over it as you get more comfortable with Flex. Code your components in MXML with code up top in a script tag. If you really need efficiency, or start doing some really low-level, abstract type classes, then you can start coding your components in ActionScript. If you want to get stuff done today, use MXML.

    9. Don’t use flash.net.Responder, use mx.rpc.Responder instead.

    Yes, it’s frustrating because Flex 2.0.1 doesn’t give you the source, but in my opinion, Flex Builder doesn’t handle same-named classes very well. Just go with what Flex uses, and call it a day. If someone requires flash.net.Responder, code a wrapper to bring him into Flex land. “Sir… you need a tie to get into this restaurant. We have one in the back, one moment.”

    10.Try not to have View’s use CairngormEventDispatcher (or Events that extend CairngormEvent use event.disaptch()).

    Instead, have those deeply nested views dispatch events. Either bubble them up so a master controller View can then fire off Cairngorm events, or bucket-brigade them up. The most common scenario is itemRenderers that are a custom class in DataGrids.

    You:

    – make the itemRenderer class. If you have something in it that is clickable, dispatch a custom click event. Make it bubble.

    – extend the DataGrid that uses your custom itemRenderer, and put the custom event in the metadata up top. Otherwise, the class that houses the DataGrid will only be allowed to subscribe to the event via MXML, only ActionScript. If you use MXML, it’ll fail to compile because the DataGrid doesn’t have that event in it’s source code.

    It may feel cool at first to have a LoginForm for example dispatch the LoginEvent, but if you need to use it for a different purpose elsewhere, you’re screwed; it’s hard-coded to use a specific CairngormEvent. This applies to all components. Encapsulate your components and do your best to do what BT does: “bubble it up”.

  • Overriding valueOf in ActionScript

    This is a response to Maploop’s weblog, where he has an entry talking about addition and the $ symbol. The comments there seem to be broken, so I’m re-posting on my blog here.

    Why not just override valueOf, and then you can use them as custom addition datatypes? Haven’t tried myself, so don’t know if it works or not…

    :: tries ::

    Hrm, works in AS1:

    function Cow()
    {
    }
    Cow.prototype.valueOf = function()
    {
            return 2;
    };
    
    var a = new Cow();
    var b = new Cow();
    trace(a + b); // 4
    

    Now AS3…

    :: tries ::

    Yep, works in AS3 as well:

    package
    {
            public class Cow
            {
                    public function Cow()
                    {
                    }
    
                    public function valueOf():Number
                    {
                            return 2;
                    }
            }
    }
    
    var a:Cow = new Cow();
    var b:Cow = new Cow();
    trace(a + b); // 4
    
  • There is Nothing Wrong with Learning AS3

    I’m writing this post out of guilt. I’m working with a contractor today, Charles Schulze, helping him get a working version of one of our API’s. He’s been really patient with us while we get a working build to him that he can build around. We’re all in this walled off area of desks and tables called “The Pit”. It makes it easier to collaborate compared to f’ing cubes. A discussion was going on about AS2 and AS3 differences. Charlie again mentions he wants to get more projects doing AS3. He doesn’t get as many as AS2. I told him if he is doing pure ActionScript 3 all the time, he should be doing Flex work instead of Flash. He laughed.

    “Dude, you’re using classes in ActionScript 2… you’re building a mini-app. If you know ActionScript 3, you’re practically an application developer.”

    He then looked upset. He didn’t show it, but I could see it. He brought up various scenarios, so I did my pitch on what I think Flex is for and what Flash is for (typically). I threw in the caveat it’s not always that black and white, especially with hybrids like him who can design and code equally well. If you are in a particular industry, it makes it easier to focus on a skill set. For example, if you keep hanging around the agency world doing contract, you’re more app to get bling using your Flash skills. While I’m sure they’d ask for AS3, a lot of OOP and design patterns are useless when you have a week to get stuff done that would take me 2 months. Vice-versa in the software world in using Flex. A lot of the design appreciation just isn’t there, and you cannot use a lot of the skillsets that don’t apply to coding.

    He brings up more postulate scenarios, such as doing interactive, design-driven websites. As if on cue, Nick throws in the “doing 3D interface work”. Yeah, I was owned… but I still didn’t think that was justification for putting AS3 in a product meant to encourage upgrades, which then encourages stock holders to see post-merger success, and thusly purchase more stock in Adobe. I look at Flash 8 and go, “Damn, they got that one soooo right. Phat design additions and re-implementation of Script Asisst. Macromedia for the win!”.

    He then mentions quietly with a resolute sadness, head down working on his computer, that I made him not as motivated to learn more AS3 than he already knows. I immediately felt bad, like I had erred in communicating and then spent the next 5 minutes un-doing… or rather, re-directing my rallying towards learning AS3 in Flash CS3, not Flex Builder. Sapping passion from another is a major sin.

    I explained the immediate benefit of runtime errors. Yes, that dialogue box in your face can belt your self-esteem at times, but nothings more frustrating than an error that just stealthily never appears. Now, you see them when your SWF is running, AND can see the path leading up to their cause of failure. Furthermore, you’ll never have that nagging feeling of “maybe I should optimize my code…”. AS3, when used even with an once of effort towards using strong typing gives you mad speed increases. This won’t necessarily improve your animation speed, but certainly won’t detract from it, especially in games, or complicated menu systems. The days of seeing “Why does ‘parseXML’ freeze my animation?” on mailing lists should be a thing of the past. E4X is slower than DOM (aka XML.childNodes ) … but typically uses less code. Finally, the increased cleanliness of the strong-typing implementation will ensure your code probably works right when you compile, better than AS2. This confidence makes you a better Flash Developer.

    Let’s not forget the most important… file size. Flex 2 SWF with a Button component, 126k. Flash CS3 SWF with a Button component, 15k. That assumes your even using the new CS3 components; one you could could be smaller for example. Both Flash CS3 & Flex 2 produced Flash Player 9 AS3 SWF’s run the same speed: fast as nuts.

    So, I hope I got Charlie psyched again about getting more AS3 projects. I felt like a dick stealing his joy on wanting to get more into it, and felt I made a valid, applicable case to him to get him back in the mood for it. Hopefully my frustrations at feature choices won’t detract you either. You can learn AS3, reap the rewards, and steer clear of all the OOP & design patterns if you so choose. AS3 alone is still pimptastic in Flash CS3. There is nothing wrong with learning AS3. Just remember AS1 and AS2 are still powerful and a valid part of your arsenal.