Tagged with AS3

SnippetSaturday: Number to String

by

This is some code I wrote a long time ago back when AS3 was just starting to show up on the scene (I updated it some to work on its own and to use crazy nested Vectors). I’m sure it’s not terribly useful, it probably could be optimized quite a lot, but hey, it is what it is. I think it could be informative as one crazy possible solution to a kind of difficult problem. Comments away!

numberToString – wonderfl build flash online

Tagged , , ,

Snippet Saturday: quick random choice

by

Today’s Snippet Saturday (actually, Sunday) is a quick shortcut for choosing one of several strings, objects, etc. randomly.

Now, I wouldn’t really recommend using this code in a project. There are ways to do the same thing that are much more readable and less error prone. Instead, I thought it was an interesting experiment to show off some of how AS3′s syntax works for those of you who may not have seen something like this.

What’s happening here? Let’s break it down.

  • ["Alpha", "Bravo", "Charlie"] – Here we are instantiating a new array and populating it with three strings. This is essentially the same as:
    var a:Array = new Array();
    a[0] = "Alpha";
    a[1] = "Bravo";
    a[2] = "Charlie";
    
  • [...] – next is another pair of square brackets. This is an array access operator. In other words, everything between the two brackets will be evaluated as the index of the array to retrieve.
  • int(...) – This is an explicit type-cast to an int. That means that everything inside those parentheses is evaluated and flash attempts to convert from whatever data type it is to an integer. In the case of decimal numbers, they are rounded down so this is similar to using Math.floor().
  • Math.random() * 3 – random() of course produces a random floating point (decimal) number between 0.0 and (almost but not quite) 1.0. Multiplying that number by 3 (the length of the array) produces a number between 0.0 and 3.0 (technically, between 0 and 2.99999999etc).

The result, an array is created with three strings, a random number between 0.0 and (almost) 3.0 is generated, it is rounded down to an int between 0 and 2, that number is used as the index of the array to look up. The result will be randomly one of the three strings.

I hope you found this interesting!

Tagged ,

I finally got with git(hub)

by


It’s probably been more than a year that people have been telling me to start using github. While, I’ve certainly followed a few cool projects on there, and I’ve wrestled to get git working on Dreamhost, and I’ve even done some command line work with the repository, I haven’t yet set up a new public repository… until today. I just put up a mini-library for doing Abstract-like classes and methods in AS3 (previously written about on this blog) called simply enough, AbstractAS3.
So please feel free to take a look. This time, instead of commenting on how you would improve the code, you can just fork it and do it yourself! Enjoy.

PS. I’m hoping with the next version release of KitchenSync to move it from google code to github (UPDATE: This is done! KS on Github) to encourage more collaboration.

Tagged , , , ,

IDisplayObject? – Getting around the lack of an interface for the DisplayObject in Flash

by

Interfaces are amazing things. When I was young and green, I didn’t understand their purpose, but after working with them for a while, I will defend their use to the end. I try to create lots of interfaces early on in my projects and I find that by keeping things flexible, it saves more time despite the extra typing, pardon the double entendre.

But this is not an article about why interfaces are so great. No, this is an article about the mysterious gap in the Flash Player API with regards to an interface for DisplayObjects. If you have a class typed as something like IView, there’s no way to enure that IView can be a parameter of display list functions like addChild().

Continue reading

Tagged , , , , ,