Tag Archive for 'as2'

Page 2 of 2

I want actual sizes!

Just great! yet another weird Flash “feature”. When I tried to get the width of a rotating MovieClip I got wildly variating values. This surprised me because the MovieClip only contained exact round shape. The circle was 25 px in diameter, but when I started to trace the width in an interval I got values ranging from 25 to 35.4px.

As it turned out, the width and height of the MovieClip only represent the bounding box of the original shape (when it still had a rotation of 0). The only way to get the actual width and height of the MovieClip was to use the getBoundsmethod. This method calculates the boundingbox according to the current shape and orientation of the MovieClip giving the trrue width and height.

To test this create a movieclip width a circle in it and place the code below on the first frame (I’m no fan of timeline-code but it’s ideal for testing purpose :D ).

1
2
3
4
this.onEnterFrame = function(){ 
	this._rotation += 15; 
	trace("boundingbox-width: "+(this.getBounds().xMax-this.getBounds().xMin)+"\t\twidth: "+this._width); 
}

The sample is as2 but the problem and solution are the same in as3.

I hope this helps some of you who were wondering why a 25px circel suddenly had a 35.4px width :| .

allowFulSceen vs. transparent wmode

Just great when you waste half an hour figuring out why you can’t get your flash to run in fullscreen mode :|

To save you the time and frustration, here is why it didn’t work:
You can’t use the allowFullScreen = true and wmode = transparent together :?

Somehow the flashplayer is not smart enough to simply make the background opaque when you set the player to fullscreen mode.

Lets hope they fix this in the next player update…

Code speed-up tips!

Isn’t it just great how a few simple tricks can speed up your code? Here are some…
Al examples below are based on the fact that calling a method is way slower than just calculating the answer on the fly.
Here are some fast ways to replace common Math methods. In most cases this save half the time or more :!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//slow:
z = Math.abs(x);
//faster:
z = (x<0) ? -x : x;
 
//slow:
z = Math.min(x,y);
//faster:
z = (x<y) ? x : y; 
 
//slow:
z = Math.max(x,y);
//faster:
z = (x>y) ? x : y; 
 
//slow:
z = Math.floor(x);
//faster:
z = (x >>> 0); 
 
//slow:
z = Math.ceil(x);
//faster:
z = (x >>> 0) + 1; //mind the brackets! 
 
//slow:
z = Math.round(x);
//faster:
z = int(x); 
 
//slow:
z = Math.pow(x,2);
//faster:
z = x*x; //this one only works faster if the power is a fixed value; for-loops in case of a variable power are most of the time slower than the Math.pow!

Of course most of the above method replacements make your code more sloppy, so I suggest you only use these replacements when the speed of the application really matters (for example in huge for-loops) or in libraries or components which rarely need adjustment.

:idea: If you have any good additions to this list please let me know…

Use “this”…

Because “this” is just great!

Unfortunately many people omit them in their code :cry: . Not sure why though, but my guess is that they are ignorant or just lazy (don’t be offended if you don’t use the this keyword for another reason, but please let me know why).
Even Adobe (and Macromedia before them) neglects the this keyword. None of their examples uses this and it’s nowhere to be found in their own code either!
Shame on them!

At the company where I work we didn’t use the keyword up until a year or two/three ago. Some of us found the keyword to be very handy and convinced the rest (not sure which group I belonged to at that time).

First of all, using the this instance variables supplies you the option to declare a local variable with the same name:

1
2
3
4
this.myVar = "my instance var"; 
var myVar:String = "my local var"; 
trace(this.myVar); //my instance var 
trace(myVar); //my local var

(Note that the above code only works in a class and not in the timeline!)

This can come in really handy when you need to store some temporarily data, during a for loop for example.

The second and most important reason to use this, is that it keeps your code clean and neat. It will help you to visually distinguish the local variables from the instance variables. This is vital if you work in a team on the same project or if other people need to work on your project later on (bug fixing, implementing new feature, etc.)
In all these cases it’s important to scan through someone’s code quickly and see at a glance where a certain variable comes from or is declared. If you omit the this keyword you would first have to scan the entire method to see if, maybe, the variable is declared local in this method. While if you do use the this keyword you know for sure it is declared in the Class definition and you know you can use it throughout the entire class (and in case of a public var, throughout the entire code).

So using the this keyword gives you more control over your code and it lets you (and your colleagues) programme in old or other peoples code much faster!

If you don’t use the this keyword at the moment, I hope this post convinced you to try out the this keyword for a while (even if it’s only for one single project) and I’m sure you never want to programme with out it, ever again.

If you already did use the this keyword, maybe this post wil help you to convince some ignorant fools in your vicinity :D .