Tag Archive for 'code'

0.1*3 = 0.30000000000000004

Isn’t this just great? I stumbled upon this problem in actionscript a while ago, but at that time I just worked around it and never bothered again… until today. A colleague came to me with the same problem and I decided to investigate a little more.

Apparently it is some weird bug when it comes to multiplying decimals values by 3. Take this little script for example:

1
2
3
4
5
var j:Number; 
for(var i:int=0; i<10; i++){ 
	j = i/10; 
	trace(j+"*3 =\t"+(j*3)); 
}

It outputs:
0*3 = 0
0.1*3 = 0.30000000000000004
0.2*3 = 0.6000000000000001
0.3*3 = 0.8999999999999999
0.4*3 = 1.2000000000000002
0.5*3 = 1.5
0.6*3 = 1.7999999999999998
0.7*3 = 2.0999999999999996
0.8*3 = 2.4000000000000004
0.9*3 = 2.7

And it doesn’t stop just there. The same problems occur when adding a decimal value to the double of it’s value:

1
2
3
4
trace(.1+.2) //0.30000000000000004 
trace(.2+.4) //0.6000000000000001 
trace(.3+.6) //0.8999999999999999 
trace(.4+.8) //1.2000000000000002

The problem occurres in Javascript as well as in Actionscript (and probably some other languages too) so it’s probably caused by the way decimal values are treated at byte-level. Because it’s not likely there will be a fix for this soon, here is a little workaround:
i = int(i *10)/10; //use int() in favour of Math.round() to speed up the code

Joy with the script timeout period

Isn’t it just great when you finally find a useful new feature in Flash 9.
I’m talking about the script-timeout period.
Remember how you sometimes had to run a script for more than 15 seconds and always got that annoying timeout popup? From now on (using Flash 9 / player 7 and up) you can set the timeout as you wish. Isn’t that just great?!
It gets even better when you use AS3; the player doesn’t just show the script-timeout popup but it actually throws an Error (“Error #1502: A script has executed for longer than the default timeout period of 15 seconds.”). This way you can make a certain part of your script (which needs more time than the supplied timeout period) run over and over again until the calculation is complete.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package { 
	import flash.events.Event; 
	import flash.display.MovieClip; 
	public class TestMC extends MovieClip{ 
		private var i:int = 0; 
		private var loopCount:int = 0; 
		public function TestMC() { 
			this.addEventListener(Event.ENTER_FRAME, this.calculate); 
		} 
		private function calculate(event:Event):void { 
			try{ 
				while(true){ this.i++; } 
			} 
			catch(e:Error){ 
				this.loopCount++; 
				trace("____________n'i' after "+loopCount+" frames = " + this.i); 
				if(i<0){ //i will be negative when it becomes to big, normally you would check here if your calculation is done... 
					this.removeEventListener(Event.ENTER_FRAME, this.calculate); 
				} 
			} 
		} 
	} 
}

This is just an example and I’m sure you can think of much more useful ways to use this feature.
Hope it heps you along…

Update (1 December 2007):
Just great; I forgot to mention HOW to set the timeout . Well here is how:
You can’t set the timeoutperiod by code (neither in as3 nor in as2). You have to set the timeout manually in the publish settings of your fla: Ctrl+Shift+F12 > flash-tab > “Script time limit”.
Let me know if you still have trouble.

Where is my as2 remoting in Flash 9?

Just great! I installed Flash 9 and was happy I could finally throw out the old Flash 8 version only to find out that I couldn’t  compile my old as2 remoting projects in Flash 9. At Adobe they were smart enough to include remoting in as3 but they totally forgot to add it for as2 as well. Checking the (Flash 9) help led me to the site where I can only download the Flash 8 remoting installer which can’t be installed for Flash 9…

Very smart Adobe! :|

Fortunately I thought of an easy way to get this problem solved; simply copy the remoting classes from Flash 8 to some other directory and set this directory as one of your default Flash 9 – as2 – classpaths. (Copying it into the default classpath with all other default classes didn’t work for me somehow.)

To make it easy for you I extracted the remoting classes and put them in a zip: as2RemotingPackage.zip

PS: I coudn’t find any info about this on the internet so either nobody ever needed it, nobody ever bothered to try and find a solution or this solution is so simple nobody ever bothered to write about it.
Anyway I bother to write about it for those of you who couldn’t find a solution or just didn’t bother to find one.

Hope this will help you… :idea:

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…