Archive

Posts Tagged ‘as3’

Youtube releases AS3 api

November 9th, 2009

Since youtube has released the as3 api the wrapper will become obsolete.

In order to keep things compatible, a new as3 lib will be available for download soon that will be compatible with the wrapper but will make use of the new as3 api.

Actionscript 3.0, Object Oriented Programming, Youtube, Youtube Wrapper , , ,

Youtube AS3 Wrapper v0.8.0m

July 12th, 2009

I would like to announce the availability of a new version of the Youtube AS3 Wrapper: v0.8.0m – m is from Multiple

This new version adds support for multiple players on the same stage.

That is, you can now play multiple youtube clips in the same time, on the same stage!

The new version is available for download on the downloads section.

The documentation has changed for the new wrapper version, however with not much. More details about that in the changelog section.

The old Test Harness Players will work ok with this new version of the wrapper, although a recompilation is needed, however a new very simple Test Player, in Flash this time, has been created to demonstrate the new features. The sources for this player are available in the downloads section too.

A sample has been added on the multiple instances post: Youtube AS3 Wrapper – Multiple instance sample

Hope you will enjoy this new version!

Youtube, Youtube Wrapper , , ,

Youtube AS3 Wrapper v0.7.2

June 3rd, 2009

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

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

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

Enjoy!

Youtube, Youtube Wrapper , , ,

scale9Grid – When it works and when it does not

May 19th, 2009

I think everyone with some actionscript knowledge tried at some point to use this feature, however ended up dissappointed. Looking on Adobe help won’t help you, because there’s nothing there.

So you kept on receiving the infamous #2004 ArgumentError: “One of the parameters is invalid.”.
You double checked the parameters of the Rectangle object you passed in and the values are within the bounding box of the symbol, but still the error is there.

In this post, I’ll try to bring some light on why sometimes scale9Grid works, and why it fails sometimes.
This article is on AS3 but the principles also apply on AS2 also, only the syntax might deffer.

First of all we should say that scale9Grid property of the flash.display.DisplayObject class receives a flash.geom.Rectangle object. The values of this rectangle must fall within the bounding box of the display object you’re trying to set the property on. The coordinates of the rectangle object are relative to the coordinate system of the display object, and not it’s parent!

Let’s see an example:
We have this Sprite object, mySprite, situated at x=100, y=100, being 200px in width and 200px in height.
We would set the scale9Grid property to a Rectangle object that is situated at x>=0 and y>=0, and let it have it’s width <= 200 and height <= 200. But we must make sure that x+width<=200 and y+height<200, this way we make sure or Rectangle falls within the bounds of the display object.

What we said so far is available on Adobe’s help also. But read forward, because from now on we’ll look at concrete examples of when it works and when it fails.

When it works?!

The scale9Grid will always work correctly when the DisplayObject doesn’t have any other symbols inside it, only graphics. That is, it doesn’t have any children.

Example:

var mySprite : Sprite = new Sprite();
mySprite.graphics.beginFill(0xff0000);
mySprite.graphics.lineStyle(2, 0x00ff00);
mySprite.graphics.drawRoundRect(0, 0, 200, 200, 50);
mySprite.graphics.endFill();

mySprite.x = 100;
mySprite.y = 100;

this.addChild(mySprite);
// Set the x and y of the rectangle bigger than the cornerRadius of our roundedRect,
// otherwise it the corner will look stretched
mySprite.scale9Grid = new Rectangle(60, 60, 100, 100);

// Test the movie with the next line commented and uncommented so you can see
// that event the sprite is resized, the corners keep their shapes
mySprite.width = 400;

Copy and paste the code above into the first frame of a fla file (AS3) and test the app. You should see the rounded rectangle preserves it’s beautiful corners.

It will also always work if the Rectangle that you set will not fall outside the bounds of the graphics object, even if the display object contains other children.

Let’s see an example:

var mySprite : Sprite = new Sprite();
mySprite.graphics.beginFill(0xff0000);
mySprite.graphics.lineStyle(2, 0x00ff00);
mySprite.graphics.drawRoundRect(0, 0, 200, 200, 50);
mySprite.graphics.endFill();

mySprite.x = 100;
mySprite.y = 100;

var mySprite2 : Sprite = new Sprite();
mySprite2.graphics.beginFill(0x0000ff);
mySprite2.graphics.drawRoundRect(0, 0, 50, 50, 20);
mySprite2.graphics.endFill();

mySprite2.x = 80;
mySprite2.y = 80;

mySprite.addChild(mySprite2);
this.addChild(mySprite);
mySprite.scale9Grid = new Rectangle(60, 60, 100, 100);

mySprite.width = 400;

Make note of the of the second Sprite we built, and added it as a child of the first Sprite object.
Now test your movie, with and without resizing the sprite, and notice that scale9Grid apply only to the current display object, and not to it’s children. (the second Sprite is stretched, doesn’t keep it’s rounded corner shape)

If you don’t have any drawings in the graphics object of the display object, but the display object has children, if you try to set the scale9Grid, and you set it right, you won’t get the #2004 error, but as I said above, the scale grid will not apply to the children, they will be stretched, like in the next example:

var mySprite : Sprite = new Sprite();

mySprite.x = 100;
mySprite.y = 100;

var mySprite2 : Sprite = new Sprite();
mySprite2.graphics.beginFill(0x0000ff);
mySprite2.graphics.drawRoundRect(0, 0, 50, 50, 20);
mySprite2.graphics.endFill();

mySprite2.x = 10;
mySprite2.y = 10;

mySprite.addChild(mySprite2);
this.addChild(mySprite);
mySprite.scale9Grid = new Rectangle(35, 35, 10, 10);

mySprite.width = 300;

If you think that setting scale9Grid property on the children will do the trick, that will not work. Like in the next example:

var mySprite : Sprite = new Sprite();

mySprite.x = 100;
mySprite.y = 100;

var mySprite2 : Sprite = new Sprite();
mySprite2.graphics.beginFill(0x0000ff);
mySprite2.graphics.drawRoundRect(0, 0, 50, 50, 20);
mySprite2.graphics.endFill();
// Set scale9Grid on the child
mySprite2.scale9Grid = new Rectangle(25,25, 10, 10);

mySprite2.x = 10;
mySprite2.y = 10;

mySprite.addChild(mySprite2);
this.addChild(mySprite);
mySprite.scale9Grid = new Rectangle(35, 35, 10, 10);

mySprite.width = 300;

When it fails?!

As I said above, if your display object does not have anything in it’s graphics object, setting scale9Grid might not throw the #2004 error, but it will certainly not have the expected result.

It will always fail if you set the rectangle beyond the bounds of the display object.
Like in this example:

var mySprite : Sprite = new Sprite();
mySprite.graphics.beginFill(0xFF0000);
mySprite.graphics.lineStyle(2, 0x00ff00);
mySprite.graphics.drawRoundRect(0, 0, 200, 200, 50);
mySprite.graphics.endFill();

mySprite.x = 100;
mySprite.y = 100;

this.addChild(mySprite);
mySprite.scale9Grid = new Rectangle(60, 60, 150, 150);

mySprite.width = 300;

Note that although I’ve set the width and height of the rectangle to 150, which is lower than the width of the sprite, 200, scale9Grid fails with #2004 error. That is because 60+150>200, the rectangle falls beyond the bounds of the display object.

Setting scale9Grid also fails if the rectangle is within the bounds of the display object, but falls beyond the bounds of the graphics object. See the next example:

var mySprite : Sprite = new Sprite();
mySprite.graphics.beginFill(0xFF0000);
mySprite.graphics.lineStyle(2, 0x00ff00);
mySprite.graphics.drawRoundRect(0, 0, 200, 200, 50);
mySprite.graphics.endFill();

var mySprite2 : Sprite = new Sprite();
mySprite2.graphics.beginFill(0x0000FF, 0.5);
mySprite2.graphics.drawRoundRect(0, 0, 200, 200, 30);
mySprite2.graphics.endFill();
mySprite2.x = 140;

mySprite.addChild(mySprite2);

mySprite.x = 100;
mySprite.y = 100;

this.addChild(mySprite);
mySprite.scale9Grid = new Rectangle(60, 60, 150, 100);

mySprite.width = 500;

As you can see the rectangle is within the bounds of the display object, but falls outside the bounds of the graphics object. So scale9Grid fails with error “ArgumentError: Error #2004: One of the parameters is invalid.
at flash.display::DisplayObject/set scale9Grid()”

As a bottom line, you should scale9Grid if the display object has drawings in it’s graphics object, and set the Rectangle you pass to the scale9Grid property to fall inside the bounds of the graphics.

Hope I was clear enough and it will help anyone.
Any comments are very welcome.

Actionscript 3.0 , ,

AS3 – Object Oriented Programming – Part2

May 4th, 2009

Hi again,

It took me more time than I first thought to write this part down, since i’ve been very busy, but here it is.

As I said, I’ll talk about packages in this second part of the OOP tutorial.

So. What is a package?: Is a way of grouping things together and distinguish between things that look-a-like but come from different sources.
For example: we create our class Apple and a team collegue brings his own version of class Apple. This creates ambiguity for the compiler (and for us also) because it can’t decide which version of the Apple class to use. To avoid this ambiguity, without changing the name of any of the classes, we move them into separate packages: let’s say mypackage will hold our Apple class and colleguepackage will hold our collegue’s class.
To refer to the classes inside packages, we can use the name of the package, like this:
mypackage.Apple and colleguepackage.Apple
As you can see the “.” [dot] operator is used to access content inside packages. Remember however that classes declared internal are not accesible outside the package they are contained in!

Let’s see some code:

package mypackage {
  public class Apple {
  }
}

What we did: we create the package mypackage, and inside it we created the class Apple, public.

To use the class we need to import it:

import mypackage.Apple;

var myaple: Apple = new Apple();

Every class must be inside a package, but it’s worth noting that a global package exists. The classes inside the global package are available without importing them (still the access specifiers occur). To create a class inside the global package, don’t give any name to the package, like this:

package {
  public class Apple {
  }
}

Now the Apple class can be used directly:

var myApple : Apple = new Apple();

Packages can be nested. That means we can have a package named com and inside this package, we can have another package named com.otherpackage. Of course the nesting can go deeper, any package can hold other packages..
How do we create nested packages, as a regular package, only specifying the entire qualified name for that package. Example:
Define the com package that will hold a Test class:

package com {
  internal class Test {
  }
}

Define the com.otherpackage.mypackage that will hold the Apple class:

package com.otherpackage.mypackage {
  public class Apple {
  }
}

Looking at the com.otherpackage.mypackage package we can say:
com.otherpackage.mypackage is a sub-package of com.otherpackage, which is a sub-package of com, which is a sub-package of the global package.

While classes must be situated in it’s own file, with the name of the class being the name of the file, packages must be insde their own directories, the name of the package being the name of the folder.

This being said, for our com.otherpackage.mypackage package, we need the next directory structure:

  • com
    • otherpackage
      • mypackage

So if we had the com.Test and com.otherpackage.mypackage.Apple classes, just as we defined them above, we’d have the following directory and files structure:

  • com
    • otherpackage
      • mypackage
        • Apple.as
    • Test.as

Ofcourse, putting a class in the global package, we would not create any directory, the file should be placed right on the top folder of the classpath.

I mentioned the classpath, so i think it worth talking a bit about it.
The classpath is nothing else than the path where the flash compiler will look in order to find the classes you use in your project.
The directories of your packages are relative to this classpath.
The default classpath, is the root directory where you have your fla file (that is if you’re using Flash).
But you can change that to fit your needs.

That’s it about this part.
Any comments are welcome!

In the next part we’ll talk about inheritance and more about access specifiers.

Actionscript 3.0, Object Oriented Programming , ,

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 , ,