Bits & Bytes

Posts Tagged ‘3.0’

Loading an Image Into a Bitmap in Actionscript 3.0

The code below shows how to load a remote image and store it in a Bitmap object (this works for local images too). We begin by creating a Loader object, setting the callback function OnImageLoad() to get called when the image is done loading, and then calling the load() function to load the image. The OnImageLoad() function casts the loaded content to a Bitmap, where the BitmapData is used to create a new Bitmap that is added to the stage for display.

const ksXoaXLogo:String = "http://www.xoax.net/public/XoaXLogoNew.png";

var qLoader:Loader = new Loader();
qLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnImageLoad);
qLoader.load(new URLRequest(ksXoaXLogo));
 
function OnImageLoad(e:Event):void {
    var qTempBitmap:Bitmap 	= qLoader.content as Bitmap;
    var qBitmap:Bitmap		= new Bitmap(qTempBitmap.bitmapData);
    addChild(qBitmap);
}

Responding to Mouse Click Events in Actionscript 3.0

To respond to mouse events of any kind, we create a callback function that takes a MouseEvent and returns void. Then we call addEventListener() with the event and the callback function. Below, we show how this is done for MOUSE_DOWN and MOUSE_UP events, but the same method can be used for all of the mouse event types.

Inside the callback functions, we change the message that is displayed by the TextField and position the message to be displayed where the event occurred. If you click in the box above, you will see the message change and move as you press and release the left mouse button. In this example, we position the message using the stage coordinates. However, we can also use the members localX and localY to get the position based on the object that is listening for the events.

var qMessage:TextField = new TextField();
qMessage.text = "Click In Here";
addChild(qMessage);

stage.addEventListener(MouseEvent.MOUSE_DOWN, OnPress);
function OnPress(e:MouseEvent): void {
	qMessage.text = "Down";
	qMessage.x = e.stageX;
	qMessage.y = e.stageY;
}

stage.addEventListener(MouseEvent.MOUSE_UP, OnRelease);
function OnRelease(e:MouseEvent): void {
	qMessage.text = "Up";
	qMessage.x = e.stageX;
	qMessage.y = e.stageY;
}

Playing a Video File in Actionscript 3.0

We begin by creating an Actionscript 3.0 project and setting the stage dimensions to match our video. In this case, the video is 240 by 180 pixels. Then we add this code, compile, and execute. To play the video file, click the screen as shown above.

// Output the initial instructions to user
var qInstructions:TextField = new TextField();
qInstructions.text = "Click To Play";
addChild(qInstructions);

// Create a video object with 240x180 pixels
var MyVideo:Video = new Video(240, 180);
addChild(MyVideo);

var qNetConnection:NetConnection = new NetConnection();
qNetConnection.connect(null);

var qNetStream:NetStream = new NetStream(qNetConnection);
MyVideo.attachNetStream(qNetStream);
// Add this line to ignore metadata and cue points
qNetStream.client = new Object();

// Play the video when the screen is clicked
stage.addEventListener(MouseEvent.CLICK, OnClick);
function OnClick(e:MouseEvent): void {
	qNetStream.play("http://xoax.net/public/XoaX.flv");
}

The first part of the program outputs the message telling the user to click the screen. Next, we create a Video object that matches the dimensions of our video. Then we create a NetConnection object and call connect() with the argument value null to signal that we are going to load a sound or video file. Finally, we create a NetStream object using the NetConnection and attach it to the Video.

Once this is done, we can play a video file from anywhere: either our local drive or a remote location on the web. To make the video wait to play until the user clicks, we have wrapped the call to play() in a callback that is called when the stage is clicked.

 

© 2007–2024 XoaX.net LLC. All rights reserved.