Subscribe to RSS
get email updates
home | about | pixDif AIR app | video tutorials
polyGeek.com

place your ad here

Web Premium



Get Qwest High Speed Internet



Add commas to a number for readability

February 18th, 2010 . by polygeek

Do you need to display a large number and want to add commas to it for readability. The following code should work. Actually, it works for numbers that aren’t too big. Not sure when it starts to break down but passing Number.MAX_VALUE definitely doesn’t work. :) But, for regular numbers smaller than the national debt you’re probably in luck.

public static function commatizeNumber( n:Number ):String {
      var a:Array = new Array();
      var commatized:String = '';
      var decimal:String = '';
      if( int( n ) != n ) {
        // if the number isn't an integer then get everything after the decimal
        decimal = '.' + n.toString().split( '.' )[1];
        n = int( n ); // make n an integer
      }
      var numString:String = String( n );

      var place:int = numString.length; // start at the end
      while( place > 0 ) {
        var s:String;
        // stop the while-loop
        if( place - 3 < 0 ) {
           place = 0;
           s = numString.substr( 0, numString.length % 3 );
         } else {
           place -= 3;
           s = numString.substr( place, 3 );
         }

         a.push( s );
       }
       var len:int = a.length
       for( var i:int = len - 1; i >= 0; i-- ) {
        // prevents the addition of a comma at the end of the String.
        commatized += ( i != 0 ) ? a[i] + ',' : a[i];
      }
      commatized += decimal;
      return commatized;
    }

This code seems a bit inelegant to me. Maybe it’s because I wrote it this morning before having coffee. :) Let me know if you see a way to make it better.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

Using the DrawingAPI to make wavy circles and stars

January 2nd, 2010 . by polygeek

Ingredients

  • A bunch of Sprites
  • 1 cup of graphics.lineStyle
  • A dash of Vector3D and Matrix3D – adds an extra dimension to the dish ( ha-ha )
  • And most importantly a pinch of sin and cosine.
view source

Directions

It is surprisingly easy to make this dish. The entire deal comes down to one line of code:

s.graphics.lineTo( Math.sin( i + rotation ) * r, Math.cos( i + rotation ) * r );

Once you have that the rest just falls into place. The entire WavyCircles class is only a few lines of code:

[cc lang="actionscript3"]package com.polyGeek.patterns {
	import flash.display.Sprite;
	public class WavyCircle extends Sprite {
		public function WavyCircle( s:Sprite, radius:int, amplitude:int, period:int, rotation:Number ) {
			super();
			var step:Number = 0.01;
			var len:Number = 2 * Math.PI + step;
			for( var i:Number = -0.01; i < len; i+= step ) {
var r:Number = radius + amplitude * ( Math.sin( period * i ) * Math.cos( period * i ) );	 				 				if( i >= 0 ) {
					s.graphics.lineTo( Math.sin( i + rotation ) * r, Math.cos( i + rotation ) * r );
				} else {
					s.graphics.moveTo( Math.sin( i + rotation ) * r, Math.cos( i + rotation ) * r );
				}
			}
		}
	}
}[/cc]

Ya see. That wasn’t so bad now was it? Most of the work on this came in the MXML and getting the auto-pilot and sliders going. Not hard. Just time consuming. I did all this coding back in the Spring – 9 months ago. I get a bit distracted over the summer and never published it. Of course I ported it over to Flex4 before putting it up. That was pretty easy. Most of the work was just replacing “<mx” with “<s” and that sort of thing. Not that I had to. Just wanted to.

I’d suggest turning off the auto-pilot and manually changing the radius, amplitude, period and rotation. Then take a look at the source code. It should be pretty easy to make since of it.

And yes, I should hook this up to an MP3 player and throw a few more WavyCircles in there that change based on the beat. Maybe later.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

Explorer for BitmapData.perlinNoise and BitmapData.noise

April 5th, 2009 . by polygeek

If you need to add some random noise to your BitmapData then you have two options:

  1. myBitmapData.perlinNoise which is sort of smooth gradiations of patterns. Sort of looks like a nebula to me, or
  2. myBitmapData.noise which is much like static, just a bunch of randomly colored dots

It is very easy to use each method but how changes in the parameters can change the output isn’t very obvious. So I created this Explorer because that’s what I do. I’m an Explorer of Actionscript. :)

view source

And now…
Now you can go out and make some noise of your own. Maybe something a little more creative than this. Be sure to drop a comment below if you have anything you would like to share.

Notes
The last parameter passed in the perlinNoise method is for an array of offsets. There’s no easy way to add user controls for editing that many possible arrays – one for each octave. Therefor I just left it out since the default is null. I also did a few experiments with adding an offset array behind the scenes without any user control but the change in visual appearance of the output was so small that I didn’t think it was worth the effort. It’s quite possible I wasn’t doing it right so you should play with it yourself.

Coding conventions
In this Explorer I experimented with changing my coding conventions for giving IDs to components. I usually just give components a descriptive ID and I don’t think about the format that much. This time I placed the type of component at the beginning of the ID string followed by an underscore and then a descriptive name. I often get to a point in an method where I need to access a component and I have to remember the name. The Outline panel is helpful but I don’t use it as much as I should. With this convention all I have to do is type in slid… to get the code hinting for all of the slider components and then just pick it out of the lineup.

What do you think of this convention?

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

localhost or not localhost, that is the question

March 22nd, 2009 . by polygeek

How often to you have apps that need to change certain settings depending on whether you are publishing locally or deploying to a server? If you’re like me every time you get ready to export for production you have to remember to change a few connection strings. But I forget all the time and end up exporting or worse forget altogether and the client emails me, “Hey, I can’t login anymore.”

Well, I’m tired as hell and I’m not going to take it anymore. I figured they had to be a better way so I started digging around in the Application.application properties and found the url property. I should have done this ages ago. In fact, I could swear that I saw a blog post about something like this months ago but I didn’t really pay attention and then couldn’t find it again. So if you blogged about this a few months ago thanks for planting the seed for me to get off my ass and start using this approach.

The very simple way to set this up is to simply ask:

if( Application.application.url.indexOf( 'localhost' != -1 ) {
// this is the localhost
} else {
// you are not on the localhost
}

With this you can set your RemoteObject connection properties depending on the location that the SWF is running from. Now you won’t have to change any settings when exporting.

If you’d like to take this a step further I wrote a static class to help. It will tell you if you’re on the localhost and much more. Look at the Flex app below to see all the properties.

view source

Event Dispatcher
The version above is all ready to go. The only thing that it lacks is an event dispatcher at the end of the init() method. I use my own custom event dispatcher – Zion – which works great for me but might not be your cup of tea. I’d suggest adding your own event dispatcher but if you want here’s the same source code as above with my Zion class dispatching the event for you.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

BitmapData.draw

March 17th, 2009 . by polygeek

The cornerstone to BitmapData is the draw method. That is one of the main ways to get pixels into your Bitmaps. Below is an explorer to allow you to play around with selecting pixels from an image and see them drawn onto the surface below.

Dragging the mouse inputs the values for the Rectangle parameter. The radio buttons to the right allow you to control the BlendingMode and ColorTransformation parameters.

At the very bottom you can see text output of the exact values that went into drawing the pixels into the BitmapData.

view source

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

« Previous Entries    



polyGeek.com

© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites
Local Directory for Los Angeles, CA

Better Tag Cloud