Archive for November, 2008
AS3 onKeyDown, Key Down listener
Saturday, November 22nd, 2008 | Actionscript 3, Quick Tip | 1 Comment
Overview
Another big question i get asked is how do you detect the user actioning the keyboard? It’s slightly more complicated than AS2, where you could just attach a listener directly to the Key class singleton.
Because of this I’ve seen loads of odd ways that people have tried to implement keyboard detection in AS3, from making a textfield that sits onto of everything receiving focus, to a textfield stashed in every class.
By far the simplest way is to add a listener straight to the stage e.g.
//call onKeyboardDown on key down
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardDown);
If you’re trying to put this in a class that extends DisplayObject like MovieClip, then you’ll have to wait until the instance has been added to another DisplayObject that’s already attached to the stage directly or in a tree. i.e. after a addChild(myDisplayClass) has been called.
You don’t have to worry about checking keyCode’s against numbers now either. The Keyboard class is full of static variables relating to practically every key on the keyboard. i.e.
protected function onKeyboardDown(event:KeyboardEvent):void
{
-
- this.dispatchDialogComplete();
//test key code against statics in the Keyboard class
if(event.keyCode == Keyboard.ENTER)
{
}
}
Here’s a clump of code to help explain:
It’s a basic LoginDialog that you’d code generate like this
var dialog = new LoginDialog();
dialog.x = 100// arbitrary
dialog.y = 100// arbitrary
this.addChild(dialog);
//as the stage variable inside the dialog now exists, call draw() to add key listener to stage
dialog.draw();
Example: try pressing enter after adding some values
Using the youtube player as a radio station
Sunday, November 16th, 2008 | Actionscript 2, Youtube Player | 3 Comments
In the previous post i created an example of loading the Youtube player into your application, as2 or as3. To do this i had created a wrapper class component to make it simple to just drag and drop the component into your as2 application. With this you can start using Youtube videos, and controlling playback. Simple really.
This is an example of what you can do with that sort of functionality simply and more importantly, very quickly. At its core, it’s an instance of the youtube wrapper component, a list component, some code to let users add in new youtube songs, and retrive all the songs from the database. And all i’ve done is hide the video player.
Be aware though that a video must be enabled to share on youtube before you can use it. So Official videos from bands usually aren’t enabled for sharing, and as such you’ll need to find one added by a fan instead.
Loading Youtube player into AS3
Sunday, November 16th, 2008 | Actionscript 2, Actionscript 3 | No Comments
Here’s some demo code of how to get youtube into AS3. Simply put, you load the youtube player into an as2 wrapper swf, then load that swf into your as3 file. A bit of LocalConnection communication between the two and you’re good to go. There’s already a few examples around the internet, but they seem to overcomplicate what is a pretty simple task.
Here i’ve made an as2 class to wrap the youtube player, handle all the loading, state changes etc, and converted it into an open component. This is dropped into an as2 swf with some buttons to show it working. I’ve used Grant Skinners SWFBridge to handle the communcation between the as2 swf and the as3 swf, like the youtube wrapper class i made, it just serves to make it even easier to plumb together.
I’ve made it easy to delete all the controls from the as2 version and shrink the swf size to the size you want the player to be. Then simply load into any as3 swf and copy the SWFBridge code to control the video playback.
Source Code: Can be found here, make sure you check the licensing code for Grants Classes before usage, i can only speak for my own code with regard to license.
License – Creative Commons – for any use commercial or personal.
Search
Recent Posts
Categories
@AdenForshaw
- On the train back from london, sat next to a portly chap wearing double denim rocking a pungent aroma of Eu de sweaty man. #BadTimes
- Presenting new @Coull Affiliator in #Cocoon lounge bar Picadilly. Only snag is it's soft drinks only... torture!
- The old boys rockin it hard over at the #OldDuke http://twitpic.com/1mrsn2
- Strong set from #GazBrookfield over at Mr Wolfs, shame about the cider selection.
- Doing the pub quiz over at #theorchard , the questions are pretty hardcore tonight http://twitpic.com/1l1uf1