Archive for the 'Flash' Category

Page 3 of 4

with() Problem/Bug

This is yet another bug I found while doing some Flash-dev, it has to do with the with() statement.
If you use with(this){…} it will give you erratic/unpredicted behavior, for instance take this code:

ob = {};
ob.test = function() {
	with(this) {
		__x = 1;
		trace("test: inside: "+__x);
	}

	trace("test: outside: "+this.__x);
}

ob.test();

You would think that output would look like this:

test: inside: 1
test: outside: 1

But, instead it comes out as:

test: inside: 1
test: outside: undefined

Seems as though Flash does not allow you to set variables when using this in a with() statement. You can get the .fla i used to make the test cases for this bug here.
Thanks Macromedia for wasting another hour of my time! :P

Determining When A Movie Clip Is Initialized

This is a common problem in Flash, determining when a MC is fully initialized. This problem only really arises when you are using attachMovie() to dynamically make/attach MC’s to the stage. An example of this problem is if you have a MC with timeline animation that you are going to attach to the stage. The timeline animation has stop() on its first frame and its last frame. You use this code to attach it to the stage:

this.attachMovie("boxClip", "box", this.getNextHighestDepth());
box.play();

The attached “box” MC wont play (Here’s an example of what I’m explaining). Other common problems like this are trying to grab the height of a dynamic textfield with autoSize = true contained in a MC that you have just attached to the stage and assigned alot of text to the internal textfield. Heres an example:

this.attachMovie("textContainer", "textHolder", this.getNextHighestDepth());
this.textHolder.textField.text = lotsOfText;
trace(this.textHolder.textField._height);

The textfield will not trace its height after the text has been inserted and the textfield has auto-sized, it was report whatever size you originally assigned the textfield (Here’s an example of what I’m explaining).

Your probably wondering how the heck you fix this problem, well, actually its really simple. By using EventDispatcher we can easily solve this problem. I discovered that after a MC has run through the code on its first frame everything in the MC is initialized including all child MC’s. Using this information I added this line of code to the end of the first frame’s actions:

dispatchEvent({type:"inited"});

Then for the attach code for the first example with the animated MC would look like this:

this.attachMovie("boxClip", "box", this.getNextHighestDepth());
box.initBroadcaster();
box.addEventListener("inited", function(){this.play();});

The initBroadcaster() method is from my Object Prototypes and simple initialized the object that the method is applied on as a EventDispatcher. If you dont know anything about EventDispatcher or listeners, I suggest you learn about them; they are one of the most helpful things in AS, this tutorial should give you what you need to know about EventDispatcher if you haven’t used it already. So after initializing the box MC as a EventDispatcher I added a function listener to the “inited” event that will be broadcasted by the box once it is fully initialized. The function is executed in the scope of the box, because of this the ‘this’ variable equals the box MC, thus this.play() will cause the box MC to start playing. If you didn’t want that function to exact in the boxes scope you can use the Delegate class to execute a function is different scope (Flash 7.2 also comes with a Delegate class but Steve’s Delegate class that I linked to is better). If you want to learn more about Delegates and how to use them read this tutorial. You can get both the updated animation initialization and the textfield initialization files here.

Class Variable Weirdness

This is a weird bug/feature I found in Flash a week or two ago. I like to declare my variables in classes like this:

class electric {
	var voltage = 100;

	function electric() {}
}

Well, I was working on a project a few weeks ago and I had a class that looked something like this:

class data {
	var info = [];

	function data() {}

}

Throughout the SWF I code like this:

dataOb.info.push("info 1", "info2");
dataOb2.info.push("info 3", "info 4");

When I traced dataOb, I got this output:

info 1, info2, info 3, info 4

But I only added “info 1″ and “info 2″ to the dataOb.info array? Why would “info 3″ and “info 4″ be in their too? It seems as though if you declare a variable as an array in the variable declaration and dont re-initalizie it in the constructor (IE, info = []) then the array will be shared across instances of the class. This effect doesn’t seem to happen with number declaration like the one in the example electric class I posted. Here’s an example showing what I just explained.

App Stop V.7 & Updated Source Code

App Stop V.7 has been released, it includes the following new features:

  • Added optional numeric priority display
  • Added ‘Check For Updates…’ mechanism
  • Added Application CPU usage display support (Thanks to the AGProcess class!)
  • Reorganized the preference panel

The main feature is obviously the CPU display, although internally the foundation has been laid for some new features. The updated source code for App Stop has also been up-loaded.

I’ve also updated some of the Actionscript source code that was posted on the source section of this site. The easingScrollBar class has been updated to fix a bug where the scrolling would be a few pixels off when pixelFontSafe was set to true. The debug class has been updated to allow setting the color of the text in the debug field. Also, object.as has been added which contains simple prototypes that are used in all my projects, you might see references to some of the prototypes contained in that file in my other source files.

Bitmaps & Flash

I was working on a flash-based project recently which consisted of alot of bitmaps. I always export my bitmaps from Photoshop as png-24, since I always find they yield the best results and have the best support for transparency. The problem I was having in the project I was working on was that when I imported the image, it work look fine- just as how I outputted it from Photoshop. But when I exported the movie, the bitmap lost some of details and replaced jagged grungy edges with straight ones, this was not what I wanted since the site was a ruff & grungy style. I finally discovered that Flash was exporting the image as a jpg, changing it to exporting as a png fixed the problem giving the me the jagged grungy lines I was looking for.

Flash Quirk Of The Day!

Flash has so many weird odd bugs it’s sickening.
I was working with a simple timeline animation in Flash that looked like this:

On the timeline was a shape tween that was acting as a mask for two MC’s. I had some initialization code that I only wanted to fire once contained in one of the MC’s, but for some reason it was called twice because the MC was being instantiated twice. I couldn’t figure out why the MC was being instantiated twice, so eventually I took a shot in the dark and converted the shape in the shape tween to a symbol. It worked and the code only fired once. So in the end my timeline looked like this: