actionscript

Get hours, minutes and seconds from a number without maths operations

Today I’m gonna show you a secret ninja technique to extract hours, minutes and seconds from a number (representing an amount of seconds). In my example I will show an Actionscript code, but this can be implemented in JavaScript and maybe other languages too.
So, the scenario is the following: we have a number representing seconds and we want to know how many hours this amount of seconds contains, how many minutes and how many remaining seconds. We know that a minute is composed by 60 seconds and an hour by 60 minutes and we could write a series of maths operations in order to accomplish our objective, but there is a way far simple and fast: use the Date class!
Date class already implements all the methods we need:

  • getHours()
  • getMinutes()
  • getSeconds()

So, in order to take advantage of these useful methods, all we have to do is initialize a “fake” date using the amount of seconds we want to “split” into hours, minutes and seconds. The Date class has several OPTIONAL arguments that can be specified during its initializations, these are:

  • year
  • month
  • date (day number)
  • hours
  • minutes
  • seconds
  • milliseconds

Because theme all are optional, we can create a date object by specifying only the know arguments (in our case seconds) and by assigning null or zero to the others:

1
var date:Date = new Date(null, null, null, 0, 0, 9137);

Then by calling getHours, getMinutes and getSeconds we will obtain what we expect:

1
2
3
trace("hours: ", date.getHours()); // 2 hours
trace("minutes: ", date.getMinutes()); // 32 minutes
trace("seconds: ", date.getSeconds()); // 17 seconds

Not sure about the result? Let’s test it:

60 * 60 * 2 = 7200; (seconds contained in 2 hours)
32 * 60 = 1920; (seconds contained in 32 minutes)
7200 + 1920 + 17 = 9137; (original seconds!)

  • Stumbleupon
  • Delicious

Documenting Actionscript classes which make use of AIR framework

This is just a quick post that may (I hope) help who are going to use asdoc tool to generate Actionscript documentation for AS classes which use AIR framework.

Problem:

You want to generate documentation for your package(s), but when you type asdoc command in the terminal you get a lot of errors such:

Type was not found or was not a compile-time constant: File.
Type was not found or was not a compile-time constant: Vector

…and so on

Solution:

You have to use -external-library-path option to include AIR swc, because AIR classes are located into external swc apart from Flex framework.
These files are under /sdks/{yourSDKVersion}/frameworks/libs/air/ (where “{yourSDKVersion}” is the version of Flex SDK you are using to deploy your applications) and are the following five:

  • airframework.swc
  • airglobal.swc
  • applicationupdater_ui.swc
  • applicationupdater.swc
  • servicemonitor.swc

The first 2 should be imported with -external-library-path option anytime you use AIR classes, the others are necessary only if you are using the update framework and/or serivicemonitor.

The following is an example of how to use the asdoc command with the option discussed:


./asdoc -source-path=/Users/davidezanotti/Documents/workspace/myproject/src/ -external-library-path=/Applications/Adobe\ Flex\ Builder\ 3\ Plug-in/sdks/3.3.0.4589/frameworks/libs/air/airframework.swc,/Applications/Adobe\ Flex\ Builder\ 3\ Plug-in/sdks/3.3.0.4589/frameworks/libs/air/airglobal.swc -doc-sources=/Users/davidezanotti/Documents/workspace/myproject/src/ -output=/Users/davidezanotti/Desktop/doc

The code above will succesfull generate documentation for all classes under the folder “/Users/davidezanotti/Documents/workspace/myproject/src/” to the “doc” folder on the desktop.

  • Stumbleupon
  • Delicious

Actionscript Vector class initialization with a source Array

What you maybe already know:

Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array.

What you maybe don’t know:

It’s possible to initialize a Vector with an Array or another Vector as a source but you must use the Vector function not the class constructor. The class constructor in fact has the following signature:

1
Vector(length:uint = 0, fixed:Boolean = false)

Vector function instead has this:

1
Vector(sourceArray:Object):Vector.<T>

So, rather than using such approach:

1
2
3
4
5
var vector:Vector.<String> = new Vector.<String>();
vector[0] = "value1";
vector[1] = "value2";
vector[2] = "value3";
// ...and so on

We can do the following (Notice the absence of “this” keyword):

1
2
3
4
5
6
7
8
9
// create a vector with an inline Array as source
var vector:Vector.<String> = Vector.<String>(["v1", "v2", "v3"]);

// create a vector with an inline Vector as source
var vector2:Vector.<String> = Vector.<String>(vector);

// Testing (both trace will print the same string)
trace(vector[1]);
trace(vector2[1]);
  • Stumbleupon
  • Delicious

Learning design patterns with Actionscript 3: episode 2 – Decorator pattern

The objective of the decorator pattern is to provide a way to add extra features and responsibilities to a class at runtime without to subclass it.

The classes diagram of this pattern is composed by an interface or an abstract class which will define the supertype of the class that will be extended (decorated) by one or more classes implementing the same interface and extending an abstract class which identify these as decorator classes.

Read the rest of this entry »

  • Stumbleupon
  • Delicious

parseInt(): difference between Javascript and Actionscript

Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the radix by analyzing the string received as first argument, so if you want to be sure of the result returned you have to explicitly pass the radix in the second argument.

Example:

var myString = "000123";
alert(parseInt(myString));

The code above will alert 83 instead of 123 (because the default base guessed by Javascript isn’t 10 like in Actionscript), to get 123 the code must be:

var myString = "000123";
alert(parseInt(myString, 10));
  • Stumbleupon
  • Delicious

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

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 :)

  • Stumbleupon
  • Delicious

Using bitwise operators with Actionscript to create arguments flags

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:

Read the rest of this entry »

  • Stumbleupon
  • Delicious

Create custom reusable Flex’s components with Actionscript

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.

Read the rest of this entry »

  • Stumbleupon
  • Delicious

Implementing Array.shuffle() in Actionscript

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):

Read the rest of this entry »

  • Stumbleupon
  • Delicious

We should always use Actionscript’s "this" keyword

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)
  • Stumbleupon
  • Delicious