Archive for October, 2010


YouTube Flash AS3 API

Custom interface for YouTube videos

I would like to break away from games and physics engines for this post to demonstrate something new that I learned last week at work. I had to use the new YouTube AS3 API to import video hosted on YouTube, into the clients site. They didn’t want the standard YouTube player interface, but something a lot simpler and styled to the website.

The API is very easy to use, you don’t even need to import custom scripts into your code. All you need to do is load in the chrome less player from YouTube’s site with Flash’s Loader Class and you will then have access to all the functions and properties of the player. The most important thing to note though, is that since this is AS3, you need to the YouTube domain security clearance in order to be able to communicate between your swf, and the one you load in from YouTube. Let’s take a look at the code.

You can view my custom player here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package {
    //The usual imports
    import flash.display.*;
    import flash.net.URLRequest;
    import flash.events.*;
 
    //Security to allow communication with the YouTube swf
    import flash.system.Security; 
 
    //A great Tween package that I use all the time
    import com.greensock.TweenLite;
 
    public class YouTubeVideo extends flash.display.MovieClip
    {		
        private var _player:Object; //The player
        private var _loader:Loader; //The loader to get the player
 
        //Three buttons: Pause, Play and Fullscreen
        private var _fullButton:SimpleButton;
        private var _pauseButton:SimpleButton;
        private var _playButton:SimpleButton;
 
        private var _controls:Sprite; //A sprite to hold the controls
 
        private var _blank:Sprite; //A sprite to act as a detect area for the controls
 
        public function YouTubeVideo() 
        {
            //Allow coms between our swf and YouTube's
            Security.allowDomain("www.youtube.com");  
 
            //Load in the chromeless YouTube player swf 
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
            _loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
        }
 
        private function onLoaderInit(event:Event):void 
        {
            addChild(_loader); //Add the loader to the display list
            _player = _loader.content; //Load the content of the loader into a player object
 
            //Set up a bunch of event listeners on the player to handle any changes
            _player.addEventListener("onReady", onPlayerReady);
            _player.addEventListener("onError", onPlayerError);
            _player.addEventListener("onStateChange", onPlayerStateChange);
            _player.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
        }
 
        private function onPlayerReady(event:Event):void 
        {
            // Once this event has been dispatched by the player, we can use
            // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
            // to load a particular YouTube video.		     
            _player.loadVideoById("YOUTUBE_ID", 0, "default");
 
            // Set appropriate player dimensions for your application
            _player.setSize(533, 300);
 
            //Add controls to the display list
            _controls = new Sprite();
            addChild(_controls);
 
            //This invisible sprite gives us an area of interaction for the controls
            //which we can use to fade out the controls when the mouse leaves.
            _blank = new Blank();
            _blank.width = 485;
            _blank.height = 240;
            _blank.x = 30;
            _blank.y = 30;
            _controls.addChild(_blank);
 
            //Create the three buttons we want for the interface and add
            //mouse click event listeners to them
            _fullButton = new FullScreenButton();
            _fullButton.addEventListener(MouseEvent.CLICK, onFullScreenClick);
            _fullButton.x = 40;
            _fullButton.y = 240;
            _controls.addChild(_fullButton);
 
            _pauseButton = new PauseButton();
            _pauseButton.addEventListener(MouseEvent.CLICK, onPauseClick);
            _pauseButton.x = 240;
            _pauseButton.y = 120;
            _controls.addChild(_pauseButton);
 
            //Play button isn't added to the display list as the video is already playing
            //and so only the pause button can be seen
            _playButton = new PlayButton();
            _playButton.addEventListener(MouseEvent.CLICK, onPlayClick);
            _playButton.x = 240;
            _playButton.y = 120;
 
            //Events to detect if the mouse is hovering over the controls
            _controls.addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
            _controls.addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
 
        }
 
        private function onMouseOver(e:MouseEvent):void
        {
            //When the mouse is over the controls, fade them up using the
            //custom Tween class we already imported
            TweenLite.to(_playButton, .6, {alpha:1});
            TweenLite.to(_pauseButton, .6, {alpha:1});
            TweenLite.to(_fullButton, .6, {alpha:1});
        }
 
        private function onMouseOut(e:MouseEvent):void
        {
            //And fade them down when the mouse leaves
            TweenLite.to(_playButton, .6, {alpha:0});
            TweenLite.to(_pauseButton, .6, {alpha:0});
            TweenLite.to(_fullButton, .6, {alpha:0});
        }
 
        private function onFullScreenClick(e:MouseEvent):void
        {
            //This function toggles the fullscreen on and off
            if (this.stage.displayState == StageDisplayState.FULL_SCREEN)
            {
                this.stage.displayState=StageDisplayState.NORMAL;
                //Actions for going to normal mode from full screen
            }
            else
            {
                this.stage.displayState=StageDisplayState.FULL_SCREEN;	
                //Actions for going to full screen mode, for example,
                //masks will need to be turned off in full screen mode.
            }
        }
 
        private function onPauseClick(e:MouseEvent):void
        {
            //When pause is clicked, we pause the video and swap
            //the pause button for the play button.
            _player.pauseVideo();
            _controls.removeChild(_pauseButton);
            _controls.addChild(_playButton);
        }
 
        private function onPlayClick(e:MouseEvent):void
        {
            //When play is clicked, we play the video and swap
            //the play button for the pause button.
            _player.playVideo();
            _controls.addChild(_pauseButton);
            _controls.removeChild(_playButton);
        }
 
        private function onPlayerStateChange(event:Event):void 
        {
            //Test if the video is finished, and turn the play button on if true
            if(_player.getPlayerState() == 0)
            {
                _controls.removeChild(_pauseButton);
                _controls.addChild(_playButton);
            }
        }
    }
}

While I am sure you can learn all the details you need from the documentation page for the YouTube AS3 API, I will quickly describe a few of the functions. As you can see, pausing and playing the video is as easy as calling the playVideo() and pauseVideo() functions. You can seek to any point in the timeline using seekTo(seconds:Number, allowSeekAhead:Boolean) and set the volume with setVolume(volume:Number). Starting to get the picture? It is very straight forward and offers a great alternative to hosting your own videos.

Have fun!

Pétanque or Garden Bowls game

In my last post I spoke about a platform called PlayerIO which can be used to make multi-player flash games. Using the Box2D AS3 tile based game engine that I have been working on, I have created the basic outline for a Lawn Bowls type of game. You get four shots to get as close as you can to the small ball, also known as the “Jack”. There are movable obstacles that can get in your way, and a terrain to navigate. Obviously I will create a more fun way of throwing the balls, at the moment you just choose a vertical and horizontal thrust to give the ball.

After you throw the four balls, the game calculates the distances of all your balls from the “Jack”. In a multi-player version, the two players would take turns, and throw different coloured balls. It could be very fun given the right level design. Bowls is always interesting when you get down to the last few balls and need to throw a Kamikaze death ball at the end to try to knock out the enemies balls.

Anyway, have a look, and I will be researching PlayerIO a bit and try to get some multi-player action going. All I need to be able to do is pass the force impacting the ball from one client to another. Oh and you probably have to click in the flash window to interact with it.
 

Get Adobe Flash player

PlayerIO logo

PlayerIO allows multiplayer Flash games

Just a quick post to share something that I am going to be experimenting with over the next few weeks (whenever I get a chance). It is a website called PlayerIO which hosts all sorts of services for Flash game developers for free. They will host your databases for games scoreboards, they’ll store your files, but more interestingly you can get your own access to a server capable of executing custom server side game logic. In short, since instances of Flash can’t communicate with each other by themselves, they give you the server capable of sending and receiving messages between them. Setting up and maintaining a server like this by your self would be a costly and time consuming venture, so this is really very good news.

This is gonna require some C#

The only downside could be the fact that the server only communicates in full programming languages like C#. I think, this is kinda cool though, as it gives us all an incentive to learn a bit. Luckily, it is very similar to AS3, and depending on the complexity of the game, you can get away with only a little C# just to pass the messages back and forth between the Flash clients.

An idea for a turn based Petanque game

An idea for a turn based Petanque game

Luckily for us, there is a very comprehensive AS3/C# tutorial already on the site. I am going to be experimenting with this site and trying to make a turn based Pétanque style game with my physics engine. I can create terrain and obstacles with my engine, and then give the players turns to take shots. Each turn the player chooses an angle and velocity of shot to try to get as close as possible to the goal (in the case of Petanque it is the Jack).

Powered by WordPress. Theme: Motion by 85ideas.