Archive for category Classes

Flash on iPhone – Automatically convert dynamic text to high performance bitmaps (with filters)

During the recent optimisation and performance experiments I have been building for running Flash/Air on iOS, it became important to develop a way to have dynamic text display that wasn’t going to slow down performance on the device.

For instance if you are building a game and need to display the players score on screen you’ll require a dynamic field that can display and update this value when required. Currently any form of vectors (text included) are generally not going to help your playback performance on iOS and mobile devices, I’ve pretty much made a rule to avoid using them all together on the final display list. Another surefire way to start slowing things down is to use dynamic filters – so in keeping with these concerns I built a simple class that will automatically convert your TextField into a dynamic bitmap based alternative. The best thing about it is that it’s very simple to use and can automatically replace the orignal TextField with itself.

bitmap_text
(click to see the demo)

Simple and effective, but far from perfect

Although the original text and the bitmapped versions look very similar in the above example, this BitmapText class is far from a complete solution. The aim isn’t to create a complete bitmapping solution – for anyone that has worked dynamically with text in Flash will know there are a lot of fruity areas where measurements and formatting results aren’t completely reliable. For instance while it is set up to deal with basic alignments (left, right and centred) the class is only currently built to work with single lines, so multiline dynamic text is not currently possible. The focus has definately been on what was the quickest most pragmatic solution for my needs. Large dynamic areas of text could just as easily be services by manually making a bitmap copy.

Usage and Quick Overview

The constructor for the class looks like this:

public function BitmapText(thisField:TextField,thisAlphabet:Object =null, thisCharList:String=null, thisProps:Object = null) 

You can see that the only required argument is the TextField object that you require BitmapText to replace, but you can also add the following:

  • thisAlphabet- a storage object of already created letter bitmaps, will be created by this instance if not supplied
  • thisCharList – a string containing the characters you wish to include and be stored as bitmaps for use with the BitmapText object, an internal default list contains all upper and lower case characters as well as numerals and basic keyboard symbols
  • thisProps – an object you can use to feed further property values in to the class

The simplest usage would be something like this:

var myBitmapText:BitmapText = new BitmapText(myTextField) 

Which will make a BitmapText copy of your existing on screen TextField named myTextField (and remove the orignal from the stage).
To update the text, you can simply use the following:

myBitmapText.text="New text to display"

Tweak properties

The class itself is quite simple and has a couple of properties you can tweak, the mor eimportant ones are:

  • margin – specifies the space between characters (bitmap text spacing is very simple – similar to monotype fonts)
  • edge_buffer – specifies an edge buffer in pixels to add when making bitmap character copies. This is particularly handy if you have drop shadow or glow filters that won’t be picked up or cropped otherwise

Thre are a couple more properties you can set within the class which shuld be self evident if you look at the code.

Download

Feel free to take, use or modify the class for your own projects, if you extend it or fill out some of the gaps, please ping it back and I can add the additions. The only request I make is that you keep the credit to me/codeandvisual.com in the code.
Bitmap Text for Flash on iPhone

Tags: , ,

2 Comments

Parallax class for Flash iPhone games

What is Parallax?

For those of you who don’t know what parallax is, the best way to describe it is the way objects in the background tend to move less than objects closer to the viewer. Here’s a wiki link for something more indepth. Side scrolling 2D games provide the perfect opportunity to use the effect, giving the environments a great sense of three dimensional depth. This sort of trickery is often known as 2.5D as it sits somewhere between the second and third dimensions.

Demo

The best way to explain it is to see it in action, click on the image below to view a quick demo.

parallax_image

You can see in the demo that the environment is made of three layers with the closet layer (layer 1) moving much faster than the layers behind it. The furthest back layer (layer 3) in this case doesn’t move at all. The overall effect is quite an engaging illusion of depth.

60 FPS on the iPhone

Before going in to a description about the class I should probably mention that it doesn’t only relate just to iPhone games and of course you can use the class in any Flash project. Having said that, one of the great things about this class (and this demo) is that they run extremely well on the iPhone (on iOS4, compiling with Air 3.0). This demo is running beautifully at 60fps and you couldn’t ask for anything better (quite literally). That sounds great, and it really is, but if you’ve done any game programming in Flash before, and especially for mobile, you’ll realise that you can hit a bottle neck at any point, especially if you start working with high volumes of animated sprites or collision detection. But it’s a good starting point at least.

The Class

For the sake of demonstration I’ve bult the class here as a little stand alone item that you can easily declare and add to the stage. If you’d like to edit, rip it apart or use as is, go ahead that’s up to you. I actually use a different version for my production work that fits better into my programming structure, but it’s a good base. The usage is simple:

var myParallax:EnvironmentParallax =  new EnvironmentParallax(480, 320,[bitmap_1, bitmap_2,bitmap_3])
addChild(myParallax)

The three arguments for the constructor specify the view width, view height and an array of bitmaps to use as the background layers. The order of bitmaps in the array specifies the stacking order of the layers in the movieclip with the 0 position being closer to the viewer with each additional layer being further into the background.

Notes

  • The key to making the layer graphics is to make the bitmap sizes smaller as they go further into the background. If you think about the fact that the background image moves less than the foreground it’s easy to see that there needs to be less of an image area the further back the layer is.
  • The difference in scrolling speed between the layers is automatically determined by the size of each layer, so in this way you have control over the stength of the parallax effect by choosing what size you make each layer.
  • The engine is set up to allow unlimited layers, meaning you don’t have to stick to just 3 layers as seenin the demo – of course each new layer is going to add to the resource requirements of your scene, but by all means – push it as far as you can.

Download

You can download the Class along with an example FLA here.

Tags: , ,

No Comments

Changing the behaviour of a class without recompiling.

This is a feature in AS3 that I hadn’t come accross before. If you ever load swf files into one another you might not know it, but you’ll be dealing with application domains. I’ve loaded in SWF’s before, but never had to pay any specific attention to the applicationDomain.

Basically they allow you to specify where the class definitions for an SWF are sourced from. By default if two SWF files use a class with the same name, then only the definition found in the parent SWF will be used. If you want the child SWF to use it’s own version of that class, then you need to give it it’s own application domain.

From the Adobe docs:

1
2
3
4
        request = new URLRequest("http://www.url.com/myflashfile.swf");
        var context:LoaderContext = new LoaderContext();
        context.applicationDomain= new applicationDomain()
        loader.load(request,context);

The current project I’m working on loads in a variety of SWFs that extend a common base class. It ended up that I needed to make a change to the base class, so at first I thought I was going to have to go and recompile all of the external swf files in order to incorporate the changes I’d made. But actually, this wasn’t the case due to the default behaviour of the applicationDomain. It turned out that as long as that base class was used in the parent swf, then all the external swfs that were loaded in would use the defintion found in it and not their own. Way to go! It meant that I could tweak and make changes to those external swfs, without ever open or recompile them. It’s turned out to be really useful, and painless!

Tags: , , , ,

No Comments