Add a mouse event listener to a bitmap using ActionScript 3

I'm new to ActionScript 3 (no Flash development experience) and was wondering how I would use adding a mouse event listener to a bitmap ? The code works with a sprite, not a bitmap. Here is a shorthand version of the code I'm trying to run, I hope it makes sense!

var fsImageRequest:URLRequest = new URLRequest("img/fullscreen.png");
var fsImageLoader:Loader = new Loader();
fsImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, fsImageLoaded);
fsImageLoader.load(fsImageRequest);
addChild(fsImageLoader);

function fsImageLoaded(e:Event):void {
    var fsImageLoader:Loader = Loader(e.target.loader);
    fsImage = Bitmap(fsImageLoader.content);
    fsImage.addEventListener(MouseEvent.CLICK, fullScreenClick)
}

      

Thanks in advance.

+2


source to share


2 answers


You have added an event listener to the fsImage.addEventListener (...

Now you need to write a function that handles this event, for example:

private function fullScreenClick(event:MouseEvent):void
{
  // do something here
}

      



EDIT: To add a bitmap to a sprite, you can do the following:

var sprite: Sprite = new Sprite();
sprite.addChild(fsImage);
addChild(sprite);

      

+9


source


You cannot add MouseEvents to the bitmap because it is not a descendant of the InteractiveObject type. You will have to wrap it in Sprite or MovieClip and add it to the List Listeners in the container.



+7


source







All Articles