ActionScript 3.0 Design Patterns

(Chris Devlin) #1

48 | Chapter 1: Object-Oriented Programming, Design Patterns, and ActionScript 3.0


Keep in mind that a getter method, using thegetkeyword, looks like a property, and


is treated like one. Any reference todisplayObjectis actually a method request. The


trick is to add the instance to the display list, and at the same time call the method


that establishes the instance as aDisplayObject. Example 1-41 does just that.


public class VidPlayer extends Sprite implements IVid
{


private var ns:NetStream;
private var vid:Video;
private var nc:NetConnection;


public function get displayObject( ):DisplayObject
{
return this;
}


public function playVid(flv:String):void
{
nc=new NetConnection( );
nc.connect(null);
ns=new NetStream(nc);
ns.play(flv);
vid=new Video( );
vid.attachNetStream(ns);
addChild(vid);
}
}
}


Example 1-41. DoVid.as


package
{
import flash.display.Sprite;


public class DoVid extends Sprite
{
//Type as Interface
private var showTime:IVid;


public function DoVid( )
{
//Play the video
showTime=new VidPlayer( );
showTime.playVid("iVid.flv");
//Include DisplayObject instance
addChild(showTime.displayObject);
showTime.displayObject.x=100;
showTime.displayObject.y=50;
}
}
}


Example 1-40. VidPlayer.as (continued)

Free download pdf