Archive for the 'ActionScript' Category

Page 2 of 4

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:

Simple terrain generator experiment

Just great how easy it is to make a simple terrain generator :D

This evening I started an experiment to try and create a terrain generator (like the one in my all-time favourite SimCity2000). So far I don’t really have use for it but it might come in handy in the future.

I managed to work out the basics in just 3 to 4 hours. Now I need to find a way to get smoother edges and get rid of all the really small islands. I recon that’s gonna take a bit more work.

 I used an enterFrame event to get the animated effect. Just to see what happens.

Check the first results here:
http://www.powerupmedia.nl/#/exp070702

PS: this is the first time I used actual flash components since I don’t have a slider or spinner ready in as3; boy do those things SUCK!!! :roll:

UPDATE (2007-07-09):
I managed to remove the majority of the small (one or two px) islands by tweaking the algorithm a little. The result is now a bit more realistic.

Also I increased the max world size to 960×960 (to illustrate: SimCity2000 has a mapsize of 128×128).

Finally I added the world wrap option (because it took me just 2 minutes to add :D ).

UPDATE (2008-03-09):
Here is a short explanation of how the code works:

The general trick is to place a first pixel somewhere on the map and than check each of its neighbours to see if they need to be filled with the same “terrain-type”. You can do this by checking a random value against a fixed limit value (in this case represented by the “spread”).
When a certain pixel on the map is filled you place it in an array so you can always check if a pixel is already occupied and you can also use it to check if the maximum number of a certain type is reached (in this case represented by the “pct”).
After this whole sequence you repeat the same process for the newly generated pixels (there is your recursion). If there are no more pixels left to check and the maximum is not reached yet, you just place a new pixel somewhere on the map and start again.

I hope this will help you along.

Flash experiments

justGREAT! I organized some of my flash experiments for you to play with…

ribbon #1 (2003?)
soap (2005)
particles #1 (2005)
particles #2 (2006)
particles #3 (2006)
mouse #1 (2006)

Let me know what you think of them…

I hope to convert some older experiments in to as3 in the near future. I’ll keep you posted!

Update (2007-12-05)
I moved my experiments to a new domain and I renamed them because their numbers became too overwhelming :P
You can find these and other experiments on http://labs.powerupmedia.nl