Archive

Archive for April, 2009

Youtube AS3 Wrapper – Changelog

April 27th, 2009

Youtube AS3 Wrapper v0.8.0m

This new version introduces support for multiple videos playback in the same time, on the same stage!
Some changes in the api: All the methods have been added a new parameter, “playerid”, which will represent the id of the player to which the function will apply.
Example:

function loadVideo(id : String, startSeconds : Number = 0, playerid : uint = 0) : void;

The default for this parameter is 0, meaning that by default the function applies to the player with id=0. Which is the first player that is created.
If you use only one player (you don’t create more), you can forget about this parameter.

New methods added:

                 /**
         * Creates a new player
         * @return int the new player's id
         */

        function createPlayer() : int;
       
        /**
         * Sets the alpha [0,1] for the specified player
         */

        function setAlpha(alpha : Number, playerid : uint = 0) : void;
       
        /**
         * Sets the visibility for the specified player
         */

        function setVisible(visible : Boolean, playerid : uint = 0) : void;
       
        /**
         * Sets the position for the specified player
         */

        function setPosition(x : Number, y : Number, playerid : uint = 0) : void;
       
        /**
         * Swaps the depths of the 2 specified players
         */

        function swapDepths(playeridToBeSwapped : uint, playeridToSwapWith : uint) : void;
       
        /**
         * Brings the specified player to front
         */

        function bringToFront(playerid : uint) : void;
       
        /**
         * Sends the specified player to back
         */

        function sendToBack(playerid : uint) : void;
       
        /**
         * Sends the specified the specified player to it's original depth
         * Note that when usign swapDepths the player that is swapped has it's original depth set to the new depth
         */

        function sendToOriginalDepth(playerid : uint) : void;

The createPlayer method creates New player.
Always wait for the previous player the be ready before calling this method!
The method return the new player’s id. The first time the method is called, it will return 1. Because the player with id=0 is created without calling this method.
After calling this method, the PLAYER_READY event will be dispatched when the new player is ready, and event object will have the playerid parameter set to the new player’s id.

The other new methods speak for themselves through their names.

The YoutubeEvent has now a new parameter:

public var playerid : uint;

This parameter says now which player dispatched the event.

Youtube AS3 Wrapper v0.7.2

This new version fixes a bug where adding and removing the player to and from stage repeatedly causes it to crash.

Youtube AS3 Wrapper v0.7.1

Adds Adobe AIR support. The api remains unchanged.

Youtube AS3 Wrapper v0.7

Fixes the volume bug in IE and deprecates the reload():void and destroyAndReload():void methods because the volume works without reloading the player.

Youtube AS3 Wrapper v0.6

The first released version of the Youtube AS3 Wrapper.
Has problems with volume in Internet Explorer. In order to bypass this problem, the ytPlayer needs to be reloaded for every new clip that plays, using reload():void or destroyAndReload():void methods.

Youtube, Youtube Wrapper , , , ,

Youtube AS3 Wrapper v0.7.1

April 21st, 2009

A new version of Youtube AS3 Wrapper is available: v0.7.1

This new version adds Adobe AIR support.

The new version is available for download Youtube AS3 Wrapper – Downloads

Also, a new test harness player sources have been added, which makes use of the new wrapper and it’s AIR capabillities.
You can download the sources for the new Test Harness Player at the same link above.
Enjoy!

Actionscript 3.0, Youtube, Youtube Wrapper , ,

AS3 – Object Oriented Programming – Part1

April 16th, 2009

We talked a lot about classes and objects in the introductory part, now let’s look how we can create a class.

In AS3, a class must be created in a separate .as file, with the name of the class being the name of the file.
Also, a class must be contained in a package. We’ll discuss in the next part more about packages.

To define a class in actionscript we have the keyword class.

To write our first class, named Apple, create a file named Apple.as, open it in a text editor and paste in the next code (make sure you edit with a text editor and not a word processor like MS Word):

package {
  public class Apple {
  }
}

There it our first class, however is quite empty; it has no functionality.
Notice that i’ve used the keyword package. That way I defined the package for the Apple class. But more on packages in the next part of this tutorial.
Also notice another keyword introduced: public. This is an access specifier.
An access specifier sets the visibility for a class, method, property or namespace.

A method is a function defined inside a class.
A property or member is a variable defined inside a class.
A namespace is like a package, controls the visibility of definitions, but it’s not directly related to classes, so it won’t be discussed in this tutorial.

In our case, the public specifier sets the class to be visible and accessible anywhere. Without specifying public, the class would be visible only inside the package, because the default access is internal.
A class can only be defined public or internal, however there are 2 other access specifiers: private and protected.
Private means that the property or method is only visible and accesible inside the class. About protected we’ll talk a bit later.

With this said, let’s add a method and a property to our class:

package {
  public class Apple {
    private var stones:uint = 4;

    public function Apple() {
    }
  }
}

Now our Apple class has a property of type uint, named stones, which is declared private (this means that only methods of the class Apple can access this property) and has a default value of 4.
It also has a method, declared public, named Apple. Notice the name of this method is exactly the name of the class. A method with the same name as its class is named constructor.
In actionscript 3.0 a constructor can only be declared public. Also a constructor cannot have a return type (cannot return a value).
It is named “constructor” because when an object is created this method will be called. So it is a good place to put here any initializing code.

With these being said we can construct our first object of the class Apple, but first some words about the import statement.
Before we can use our class, we must import it where we want to use it. For that, we have the “import” statement, so we can use it like this:

import Apple;

Ok. Now we’re ready.
Open the Flash IDE, create a new Flash Actionscript 3.0 File, save it to a name of your choice and copy the Apple.as file in the same directory as your .fla file.
On the timeline, select the first keyframe and open the Actions window (Windows->Actions or press F9), and write the next lines:

import Apple;
var myApple : Apple = new Apple();

That’s it! Your first object is created out of your first class! You can test your file now, but really there isn’t much to see…
Notice the new keyword: it is used to construct new objects.

So let’s add something so that we can see what’s going on.
Add a trace action inside our constructor.
Our class should look like this:

package {
  public class Apple {
    private var stones:uint = 4;

    public function Apple() {
      trace("A new apple is constructed!");
    }
  }
}

Now run again your movie and you should see “A new apple is constructed!” in the Output window. That’s because when we construct our object using the new statement, the constructor of our class is called, thus the trace method is called.
If you would construct more objects, you will see that the message is outputted once for every object.

Next, let’s say we would like to know how many stones has our apple object. But the stones property is private, which means we can’t access it from outside the class. So, to access it let’s add a new method, named “getStones”, that will return the number of stones, and make it public.

package {
  public class Apple {
    private var stones:uint = 4;

    public function Apple() {
      trace("A new apple is constructed!");
    }

    public function getStones() : uint {
      return this.stones;
    }
  }
}

Make note of this keyword. “this” refers to the “current object”.

Now we can call the new method on our object like this:

myApple.getStones()

In Flash, change the code with this one:

import Apple;
var myApple : Apple = new Apple();
trace("myApple has "+myApple.getStones()+" stones.");

When you run your movie you should see in the output 2 lines.

That’s about all on this first part of the tutorial.
In the next part we’ll discuss more about packages.

Actionscript 3.0, Object Oriented Programming , , , , , , ,

AS3 – Object Oriented Programming – Intro

April 16th, 2009

Programmers who are not-familiar with Object Oriented Programming, find it hard to understand and cope with the concepts of object oriented programming. And moving from function oriented programming to object oriented programming can be a real pain.
That’s because they don’t have the right thinking. They think, “hmm, so I have this instance of this class and i must call this function on this object; for that I must use the . (dot) operator…”.
Actually OOP tries to resemble nature. That’s right, nature proved us it’s way for millions of years, why not take after it?… or something like this :-)

Let’s look at a nature example:
We have this big, beautiful apple. We look at it and we think “all apples look roughly the same”. So we take our apple, we look even closer to it and realize that even though it looks like all the other apples, there’s something particular about it: it’s a “Golden Delicious” apple. Now we know 2 things about it: it’s an apple, and furthermore it’s a Golden Delicious apple. We realize now that we can separate all the “Golden Delicious” apples from other classes of apples.
So here we are now:
We have the Apples class, which contains the Golden Delicious class and all the other apple classes: like Ultra Red Gala, Cameo, etc.
Furthermore, we can say: “but all the apples are fruits!”. Which is true.
This creates the next hierarchy:

  • Fruit
    • Apple
      • Golden Delicious
      • Ultra Red Gala
      • Cameo

Ok we know something about classes, but what is our apple? It’s not a class, it can’t be a class because it’s an object. That’s right: the class of our apple is “Golden Delicious”, the base class is “Apple”, but our apple is an instance of the class “Golden Delicious”.

Ok, that’s a very simplistic way of looking at Object Oriented programming.
This is only an introduction, as OOP is very complex, OOP in AS3 is complex too, so i’ll try to explain everything that’s needed to find your way into OO programming world.

I think it’s worth mentioning here that OOP concepts are common on all OO programming languages, however the syntax and features may differ from language to language.

Follow on to the first actual part of this tutorial: AS3 – Object Oriented Programming – Part1

Object Oriented Programming , , , ,