coder and technology lover
Posts tagged as3
Using bitwise operators with Actionscript to create arguments flags
Feb 8th
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:
Create custom reusable Flex’s components with Actionscript
Jan 28th
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.
Populate a Flex’s ArrayCollection… a consideration about addItem() performance
Jan 12th
I’m playing with Flex data Providers and actually I’m testing ArrayCollection. There are 3 ways to populate an Array collection:
- Initialize an ArrayCollection and then use its addItem() method (which accepts any Object type)
- Initialize an ArrayCollection, access its “embedded” Array by using the source property (ie: myArrayCollection.source) an then push objects inside this one
- Initialize an Array, populate it and then initialize an ArrayCollection by passing the Array to its constructor (ie: myArrayCollection:ArrayCollection = new ArrayCollection(myPreviousDefinedArray))
Last technique is (as far I saw) absolutely the fastest , I did a test in which I populated a collection with 300,000 objects and it taken about 9 seconds instead of 32 (350% slower) of first one! The second technique is also faster than the first, but the third is better.
So, what I learned is that addItem() should used only to add few items, instead for huge collection should be avoided in favor of the third technique described in order to obtain better performances.
Implementing Array.shuffle() in Actionscript
Jan 8th
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):
We should always use Actionscript’s "this" keyword
Jan 7th
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:
- 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)
- When invoking methods is immediately evident which is the class owner/target of the method itself
- 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)
MicroBookReview #1: Essential Actionscript 3.0 by Colin Moock (O’REILLY)
Jan 5th
Despite the word “essential”, in this book Colin Moock shows almost everything about Actionscript 3 and he does it in a clear and exhaustive manner.
The book covers several topics from the scratch and teaches not only the foundations but also some best practices and things to avoid, the topics are the following (generalized):
- Packages, classes, interfaces, functions and methods
- Language’s operators, variables, array and loops
- Animations
- Vector and Bitmap graphic
- Events and event handling
- XML (read, create, manipulate)
- Flash security
- OOP concepts (Inheritance, polymorphism and more)
- Garbage collection
- a Flex hint
Vote: 5/5
Learning design patterns with Actionscript 3: episode 1 – Singleton pattern
Dec 28th
In these Christmas holidays I bought some new books, among them I purchased “Actionscript 3 design patterns” (written by William Sanders and Chandima Cumaranatunge) and I’m actually studying this one. I never faced before the world of design patterns (which is, for those that never heard about, a way to write code and solve problems by implementing tested strategies that will guarantee flexibility and extensibility), or better I knew what a design pattern is, but I’m trying to learn to use them for the first time.
Unfortunately the first thing I learned about implementing design patterns in Actionscript 3 is that for some stuff we have to use workarounds, because actually as3 doesn’t provides 2 little useful things: abstract classes (on which most of the patterns are based) and not public constructor (in Java we can declare a class constructor as private, in as3 we must declare it as public), anyway with some little efforts, we can do everything :-)
Recent comments