Feb 12 2009

Converting Java to Actionscript… maybe is not so hard (I hope)

Category: actionscriptDavide Zanotti @ 5:06 am

After valuate my own realization of a certain library, I opted to try the porting of an already existent one… which is made in Java. This library has a lot of classes and interfaces and the manually translation would be very time-expensive, considering also that it uses a lot of classes and functions which are unavailable in Actionscript 3. Fortunately I found a small and useful AIR application which automatically converts .java files to .as files (the application is here: http://thunderhead.esri.com/readonlyurl/J2AS3.air), this application anyway helps a lot but many extra-work is required to properly convert the classes… for example Actionscript has not the abstract key, variable can’t be market as final and so on, so we have to manually adjust these issues. Another useful stuff I found is an Actionscript library which aim is to provide to AS3 developers the same (more or less) features and power of Java Collection framework, which provide classes like HashMap, ArrayList, LinkedList and so on (the library can be found here: http://code.google.com/p/addicted2flash/). Despite these helps I’m still facing some incompatibility issues… classes/methods AS3 is missing, but implementing them by myself is quite simple. For example I implemented the Java’s System.arraycopy() in 2 minutes, by following the JavaDoc ).

In conclusion Actionscript and Java have a  lot of stuff in common, the syntax and structure is quite the same, except for some issues like abstract classes and methods, multiple methods and constructors declaration (override). The main difference is that Java provides an huge number of Classes that Actionscript doesn’t, however it seems not so hard to translate Java code and implement Java functionality in Actionscript.

I hope I won’t have to deny my words :)

Tags: , , , , ,


Feb 08 2009

Using bitwise operators with Actionscript to create arguments flags

Category: actionscriptDavide Zanotti @ 10:10 am

Among all books I bought (including Java books), I haven’t one which explain in depth and in a clear manner what bitwise operators are and how to use them. Almost all books has at least a page about the argument but none is able to give an exhaustive explanation. So, after searching the net, I found an extreme useful article by Joseph Farrell on gamedev.net (direct link: http://www.gamedev.net/reference/articles/article1563.asp), which explains bitwise operators in C starting from an introduction to Numbers and number system. The article is valid to Actionscript, Java and other languages too. I started to try to understand bitwise operators, after looked to Flex’s Alert class, which can accept several options (different buttons) as an unique argument (flags). This can be accomplished by using the bitwise OR operator |  (a pipe), which “joins” together different constants:

Continue reading “Using bitwise operators with Actionscript to create arguments flags”

Tags: , , , ,


Jan 28 2009

Create custom reusable Flex’s components with Actionscript

Category: actionscript,flexDavide Zanotti @ 2:51 pm

I’ve played for a while with Flex’s library and now I’m experimenting my own custom components and I would like to share them and the knowledge necessary to build personal, reusable cool components.

Fundamentally there are two ways to realize custom Flex’s components: one (and the easiest) is to create an MXML file, the second is to create an Actionscript class.

Continue reading “Create custom reusable Flex’s components with Actionscript”

Tags: , , , , , ,


Jan 08 2009

Implementing Array.shuffle() in Actionscript

Category: actionscriptDavide Zanotti @ 1:20 pm

One useful thing that Actionscript doesn’t offers is the ability to shuffle an array, however this can be accomplished in a blink of an eye and with very few lines of code. Before to implement my own solution, I looked on the web, but solutions provided by others developers looks too complex and too long to type. What I found was based on a for loop and several line of codes to fill a secondary array with the elements in a new random order. My solution instead is based on a while loop (3 lines of code only):

1
2
3
4
var arr2:Array = [];
while (arr.length > 0) {
    arr2.push(arr.splice(Math.round(Math.random() * (arr.length - 1)), 1)[0]);
}

How it works?

First we have to create a new array (arr2) which will contains the elements from the base ordered array (arr), then we use a while loop, which can be translated into english as: “do this until base array is not empty”. Inside the loop, we push into the second array a random element from the first array and, at the same time, we remove that element from its array. Array’s method splice() in fact, removes one or more elements from an array and returns an array containing the deleted elements, that in our case is only one (as specified in the second argument). The first argument of the method represent the starting index from which to start to delete, this is dynamically calculated by using the Math.random() method (which returns a number between 0 and 1), then multiplied by the length of the base array minus 1, because array length is always 1 greater than elements array contains (cause array indexing starts from zero), finally this number is rounded to the nearest integer by Math.round(), in order to access an exact index.

I think this is the best and shorter way to implement a shuffle functionality :)

Tags: , , ,


Jan 07 2009

We should always use Actionscript’s "this" keyword

Category: actionscriptDavide Zanotti @ 4:52 pm

I was wondering if to use or not the “new” keyword for classes variables and methods, because it’s not mandatory and in as3 examples is rarely used and when it’s used, is only to avoid name collisions (typically when setting a class property using a given argument), such:

package {

    public class MyClass {

        private var myVar:uint;

        public function MyClass(myVar:uint) {

            this.myVar = myVar;

        }

    }

}

However there are 3 valid reasons to (always) use  the “this” keyword:

  1. Is an excellent way to visually differentiate between static and dynamic variable (static variables can’t use the “this” keyword, otherwise you get a compile error)
  2. When invoking methods is immediately evident which is the class owner/target of the method itself
  3. Faster typing and developing thanks to Flex Builder hints (after typing “this.”, I can select all the applicable methods, which is faster and error free than type the entire method name)

Tags: ,


« Previous PageNext Page »