Multi Column Text in Flash 9: TextFlowLite

Ever wanted to be able to display multi-column text in Flash? What about text that spans multiple arbitrary text fields? Sure, Adobe’s Text Layout Framework will let you display multi column text, but it’s still in beta, not to mention that it’s a big, robust framework.

TextFlowLite is a simple, light-weight class that works in Flash Player 9 with standard text fields. It takes one line of code to link any number of text fields together so that the overflow of each textfield runs into the next. Likewise, it’s one line of code to change the text, or reflow it if the textfields are resized.

import com.gskinner.text.TextFlowLite;
// set up the TextFlow, by default it will use the text of the first field:
var tfl:TextFlowLite = new TextFlowLite([fld1,fld2,fl3]);
...
// change the text:
tfl.text = "my new text";
...
// change the size of a field, and reflow the text:
fld1.height += 20;
tfl.reflow();

I built this class about a year and a half ago while working on the Spelling Plus Library, and had meant to release it much earlier. It is part of a set including TextFlow (adds support for orphans and widows), and TextFlowPro (which includes support for selection, editing, copy/paste). I will release the other two for free when I have time to retest and clean them up (hopefully later this week).

Here’s a simple demo:
Continue reading →

GTween beta 4: Tweening Library

GTween beta 4 has been released! This is the final beta of the library, and I am only keeping it beta for this release because there is a chance I will toy with the API a little bit more, and I’d like to clean up the demos before final (right now they are an ugly mix of demo and test file).

The main new feature in this release is a more robust synchronization model which allows you to synchronize child tweens to the delay, beginning, end or duration of its parent. This release also includes the GTweenFilter class (<0.5kb) which a number of people have been patiently waiting for. I also patched a number of bugs, added a few more simple demos, and created a MultiTween class that allows you to tween multiple objects to either the same destination property values, or different values using a single tween instance. Both of the new classes should be useful, but they are also there to serve as templates for people interested in writing their own extensions to GTween. They are not required to work with GTween normally - still just a single class. Here is the full list of changes:

  • fixed a bug with autoHide (thanks to JTtheGeek)
  • fixed a bug with useSmartRotation and destination values over 360 (thanks to radicalFish)
  • added propertyTarget and getPropertyTarget(), to support GTweenFilter and other potential subclasses
  • renamed the static “ticker” property to “activeTicker” to prevent issues in some editors due to it being named the same as the instance property. (thanks to Thomas Brekelmans)
  • fixed a problem with progressPoints set to a position of 0 not firing (thanks to Toby Skinner)
  • moved event meta tags to before class declaration, so they show up properly in the API documentation (thanks to Jordi Boggiano)
  • updated .clone() to support .useSetSize and .useSnapping properties
  • modified .addChild() to accept GTween.TWEEN_PHASE, GTween.END, GTween.DURATION, GTween.BEGINNING as a second parameter, allow more complex synchronization options.
  • included GTweenFilter and MultiTween in package.

As always, you can view the latest documentation, and download the latest version of the library at gskinner.com/libraries/gtween/.

Eneloop = Best Batteries Ever?

I don’t usually blog about things that are unrelated to the Flash platform, but once in a while something comes up that I think is impressive, lacks the exposure it deserves, and might be useful to other geeks. Eneloop batteries met that set of criteria, and they’re good for the environment, which is always a bonus.

I heard about Eneloop about a year ago, but I never found them in stores, and thought maybe the technology was vapour-ware. I wasn’t surprised, because they sounded too good to be true: They are rechargeable batteries that outperform alkalines, retain a charge for over a year (so you can charge them, stick them in a drawer, and forget about them until you need them), charge fast, and are quite cheap (only slightly more than brand name alkalines, and nearly half the price of the Panasonic rechargeables I had been using).

Then about 4 months ago I found some in a local computer store, and figured I’d give them a shot. I was impressed enough by the first pack that I’ve replaced all of my batteries with them, which only cost about $100 for 35 batteries and a charger (they also charge fine with my old charger). So far I’m really happy with them. They last a long time (about 2 months of use in my XBox360 controller, versus about a month with fully charged Panasonics), and hold a charge well so far (the ones I’ve put in my tv remotes are still going strong). The only thing I noticed is that unlike the AAs, the AAAs did not come precharged. I’m guessing they discharge more quickly in storage than the AAs due to their smaller size.

I have a bit of a conspiracy theory about these batteries, which is part of the reason I wanted to give them some exposure. These are theoretically “batteries for life” – if you charge them every 2 weeks, and they last the stated 1000 charges, they will work for over 40 years. Obviously this isn’t going to happen, but it seems like they should last a good while. Sanyo is the largest rechargeable battery manufacturer in the world, but as far as I know they have never marketed them directly. I can imagine a scenario where Sanyo approached the companies that were rebranding their batteries with this new technology, and were shot down because it has the potential to dramatically reduce the demand for batteries (because you don’t need to replace them). Likewise, you can’t find these batteries at stores like Best Buy, possibly for the same reasons – batteries are a high margin business, with a good replacement cycle. It’s just a guess, but it sounds possible to me.

I tracked mine down at a local computer shop, and then later at Costco. And no, I’m not getting paid by Sanyo to promote these, I just think they’re a smart product, and they aren’t getting any attention. And yes, they have a really crappy “skip intro” type thing on the Eneloop website. Good batteries, bad Flash. 🙂

Making dispatchEvent More Efficient

In most projects there are tons of events that are dispatched, but that have no listeners subscribed to them. This shouldn’t be an issue, but it turns out that the Flash Player deals with these events inefficiently. Luckily it’s pretty easy to patch this for specific situations.

The code below will run about 5X faster than a standard dispatchEvent call when there is no event listener for the event type.

override public function dispatchEvent(evt:Event):Boolean {
if (hasEventListener(evt.type) || evt.bubbles) {
return super.dispatchEvent(evt);
}
return true;
}

Note that the actual time difference is fairly minimal (80ms vs 450ms for 100,000 iterations in my tests), so I would only do this in classes that you are going to create a lot of instances of and which will generate a lot of events that may not have listeners.

For example, I used this in GTween, because you could have thousands of tweens active at the same time, each of them dispatch CHANGE events every frame while active, and it’s very common to create tweens without a listener for that event.

While I think this will work for all cases, I haven’t tested extensively with less common event scenarios. Bubbling events should work, but you won’t get a performance increase.

This change also reduces efficiency for events that do have listeners, but it is very minimal (<10%, 505ms vs 545ms for 100k iterations in my tests).

Always Export a Release Build

When preparing a Flex project for deployment, always make sure to export a release build. I think most people know about this feature of FlexBuilder, but I’m not sure everyone is aware of just how important it is. The SWF that is generated when you run or debug your project contains a lot of extra byte code used for debugging and profiling your content.

This extra byte code has a very obvious effect on the size of your SWF (adding over 35% in some cases). It also has a less immediately obvious (but no less significant) effect on the performance of your application. In one example here at gskinner.com, a 980kb SWF was reduced to 675kb, and some performance intensive code ran over 2.5x faster in the release build than in the debug version.

Never deploy a project without exporting a release build, and be sure to test a release build if you are experiencing problems with file size, performance, or memory utilization.

Upcoming Conferences You Should Check Out

Two of my favourite conferences are running events across the pond this year, and I’m totally stoked about it.

FitC Amsterdam

On Feb 22-24 FitC will be running a conference in Amsterdam. I spoke at Spark Europe a couple years ago, and I have to say that Amsterdam is an awesome city for a Flash conference. Pair that with FitC’s solid reputation to deliver a well-organized conference with a great vibe and a killer speaker line-up, and I think it will be an amazing event. Downing a magic muffin prior to some of the inspirational sessions (Josh Davis?) could be interesting too. FitC Amsterdam sold out last year, so you might want to grab tickets while you can.

Flash On The Beach Miami

I’ve been to Flash on the Beach in Brighton two years running now, and it just keeps getting better! This year, John (the organizer) is bringing FotB to North America with an event in Miami on April 6-8. This means you will get to experience all the awesomeness of FotB, and there will actually be a real beach! (sorry John, a bunch of pebbles on a cold, grey stretch of ocean doesn’t count as a beach for me) It’s hard to quantify, but there’s something special about FotB, my theory is that it’s a trickle down effect from John, whose boundless enthusiasm, good humor, and general niceness rub off on speakers and attendees alike. FotB also sells out every year, and “Double Early Bird Pricing” ends December 19th.

I’m going to be running a rewritten version of the “Things Every Flash Developer Should Know” talk that I did at FotB08, focused more on the technical specifics of architecture, optimization, and memory management. Basically distilling things I’ve been working out for myself over 10 years of working with Flash into a one hour session.

With the uncertain economy, now is a good time to upgrade your skills, and get in some networking (time and money permitting). Hope to see some of you there!

Adobe Company Wide Layoffs, Pulls out of Macworld Expo

Breaking news: Adobe has announced company wide layoffs of about 600 full time positions, and has pulled out of Macworld Expo. Among the cuts, Mike Downey, who was a Flash evangelist and well known by the general community.

I know a lot of really great people at Adobe, and my thoughts are with them all. Hopefully everything works out for the best for everyone involved.

Hold on to your hats folks, it could be a bumpy ride.

News here:

TUAW

Mike Downey’s twitter

Adobe Provides Preliminary Q4 Fiscal 2008 Results