Archive for the ‘Programming’ Category

Side-by-side comparison shows blitting isn’t a huge speed bump

Sunday, March 21st, 2010

This isn’t a new article, but it touches on a topic I’ve heard a lot about recently. That is, pixel ‘blitting‘. Blitting is an old school process used in sprite-based games of essentially erasing a spot in a bitmap image based on a sprite’s alpha and using a bitwise operator to draw the sprite into that bitmap image. Most graphics systems do this transparently to the user in some fashion (pardon the pun). 8-Bit Rocket has a good introduction to these concepts with regards to Flash, but read on for the important part of this post.

Read the article on Photon Storm

The article shows that Flash Player is already fairly optimized for graphical composition operations when compared to blitting. Both showed similar framerates. However, the cacheAsBitmap operation caused an exponential increase in memory consumption when the number of sprites on stage increased. Ironically, the comparison was run by the creator of the Pixel Blitz library who, I’m sure, had a difficult time presenting the results.

There are some good details in the article so if you’re interested, read the whole thing before you decide if you agree.

Have you done any work with bit blitting or sprite sheet animation? Have you found it to be beneficial? Tell us your thoguhts.

IDisplayObject? – Getting around the lack of an interface for the DisplayObject in Flash

Sunday, March 14th, 2010

Interfaces are amazing things. When I was young and green, I didn’t understand their purpose, but after working with them for a while, I will defend their use to the end. I try to create lots of interfaces early on in my projects and I find that by keeping things flexible, it saves more time despite the extra typing, pardon the double entendre.

But this is not an article about why interfaces are so great. No, this is an article about the mysterious gap in the Flash Player API with regards to an interface for DisplayObjects. If you have a class typed as something like IView, there’s no way to enure that IView can be a parameter of display list functions like addChild().

(more…)

Using XML Schemas in Eclipse / FlexBuilder

Monday, April 20th, 2009

UPDATE: After reading the entry by Misprintt on the SchemaManager and XMLDecoder more thoroughly, I felt the need to tell you all to go check it out! This is a very powerful undocumented feature of Flex (why undocumented?) that could potentially save loads of time on a larger project. Along with the rest of this article, you could automate the parsing of your XML files into bindable model classes with ease.

XML-Schema (.XSD extension) are documents used to describe the format of XML files. They are similar to DTD (Document type declaration) files but much more powerful.

xsd editor

While working on some XML documents for a Flash site, I stumbled across the XML-Schema editor for Eclipse. The editor shows the XSD as a graphical representation of the model that you’re editing that looks similar to a UML class diagram.

Here’s a great article on how it works.

(more…)

MVC cheat sheet

Wednesday, March 18th, 2009

Sometimes I forget the details of whether my Model should know about my Controller and that sort of thing. I found this little MVC cheat sheet on the internet that got me straightened out in a jiffy with step-by-step instructions. The page is actually the lecture notes from one of Colin Moock’s presentations circa the Essential AS 2.0 days but it’s still very useful. Those of you struggling with the lingo, replace Observer with EventDispatcher and ignore the junk about attachMovie. God, how did we ever write AS2!?

Using Abstract Classes in the Flash CS3/4 Library

Wednesday, March 11th, 2009

Here’s a little trick that can help you save lots of time that might otherwise be spent creating multiple specialized classes in your Flash project. This trick makes use of the Base Class field in a symbol’s actionscript properties. (For info on creating pseudo-abstract classes in AS3, go here)

By the way, this tip may be common knowledge to Flash users. I use FlexBuilder almost exclusively these days but recently have had to dip back into the twisted world of the Flash IDE so please refrain from leaving any comments about how I shouldn’t use the timeline.

The Problem

Say you have several different buttons for a site that each look of behave slightly differently but all share the same underlying functionality. In this example, I’ll be using a crude set of video controls. I want each of these different controls to respond to rollovers by showing the label beneath the icon.

picture-1

Each  control has different icons and text so they’re going to need to be kept in different symbols. One solution might be to try to cram all the icons into one master symbol and change between the different icon frames using code. But that’s sloppy and unnecessary. Another solution might be to create 4 different classes that each implement the functionality or each extend a class with common functionality. This is a much better solution, but requires you to create 4 AS files for what functionally is just 1 type. You cannot use the same class for multiple symbols in your library though. Fortunately, Flash offers another solution that may be a bit less intuitive.

The Solution

If you needed to create the same sort of control components purely using code (using Flex for example), your first step would likely be to create some sort of base class that implements the common functionality of the 4 controls. Here’s an example of a base class for these 4 controls.

package {

	import flash.text.*;
	import flash.display.*;
	import flash.events.*;

	public class AbstractVideoControl extends MovieClip {
		// you'll need to create this text field on the timeline
		// and set the instance name to "label"
		public var label:TextField;

		public function AbstractVideoControl() {
			label.visible = false;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}

		protected function onMouseOver(event:MouseEvent):void {
			label.visible = true;
		}

		protected function onMouseOut(event:MouseEvent):void {
			label.visible = false;
		}
	}
}

Note: In many non-ActionScript languages, there is a construct called an “abstract type” which  is simply a base class that cannot be instantiated on its own, only extended. In Flash, proper abstract types don’t exist, but we can consider a class Abstract if it shouldn’t be extended. More on that at Wikipedia.

Next, let’s make our controls extend this class. For each symbol, check the Export for ActionScript box in the symbol properties. Since Flash can automatically generate classes for symbols when an .as file doesn’t exist, the only thing you’ll need to fill in is the Base Class field (later, you can create a class for PlayButton if you need one). Make sure you use the fully qualified name if you’re using packages.

picture-3

Moar

I found this technique was very useful for when I wanted to animate TextFields on the timeline. Timeline animations, as you probably are aware, only work with MovieClip symbols. That means that even simple animations on text require the text to be contained in a symbol. I created an AbstractTextFieldWrapper class so that I could easily work with animated type without having to create new classes every time the format changes.

package {
	import flash.display.MovieClip;
	import flash.text.TextField;

	/**
	 * This class allows you to create an animated text field on the
	 * timeline and still set the text of the field without creating a new
	 * class for each text field that does this.
	 *
	 * @use In the flash library, create a new movieclip symbol containing the
	 * 		textField. Make sure you set the instance name for the text field to
	 * 		"textField". Make the base class for the symbol
	 * 		"AbstractTextFieldWrapper".
	 * 		In another class, use the type AbstractTextFieldWrapper insatead of
	 * 		TextField for your text field variable.
	 */
	public class AbstractTextFieldWrapper extends MovieClip
	{
		public var textField:TextField;
		public function set text(text:String):void {
			textField.text = text;
		}
		public function get text():String {
			return textField.text;
		}
	}
}