Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 - Can Bitmap classes dispatch mouse events?

I'm trying to learn AS3 and have run into a small problem.

I have a Bitmap class to which I add a MouseEvent.CLICK listener, but the event doesn't seem to be dispatched.

I use Flashdevelop to write AS3 code and Flex to compile.

I have two classes, Enemy.as and Player.as

The Player.as looks like this:

package Player 
{
import flash.display.Sprite;
import flash.events.MouseEvent;

[Embed(source="../../assets/leek.swf", symbol="Leek")]

public class Player extends Sprite 
{
    public function Player() 
    {
        trace("Player constructed");
        addEventListener(MouseEvent.CLICK, handleClick);
    }

    private function handleClick(e:MouseEvent):void 
    {
        trace("Clicked Player");
    }

}

}

The Enemy.as looks like this:

package enemies 
{

import flash.display.Bitmap;
import flash.events.MouseEvent

[Embed(source="../../assets/gardengnome.png")]

public class Enemy extends Bitmap 
{
    public function Enemy() 
    {
        trace("enemy constructed");
        addEventListener(MouseEvent.CLICK, handleClick);
    }

    private function handleClick(e:MouseEvent):void 
    {
        trace("Clicked Enemy");
    }

}

}

The two classes are pretty much identical except that one is a Sprite and I embedded a symbol from a swf file that I got from a tutorial, and the other is a Bitmap and I embedd a png file into that.

The Player class (the one that's a sprite and uses a symbol) fires off the MouseEvent.CLICK when I run the project and click on the Player image, but the Enemy class does not.

There are no compile warnings or errors, so I'm having a hard time understanding what is the issue exactly. Is it because one is a Sprite and the other a Bitmap, or is it because one uses a prepared symbol from a swf, while the other is just a png?

How can I make a Bitmap class respond to MouseEvent?

Thanks for any help!

like image 805
Garry Wong Avatar asked Dec 26 '22 03:12

Garry Wong


1 Answers

From ActionScript® 3.0 Reference for the Adobe® Flash® Platform:

The Bitmap class is not a subclass of the InteractiveObject class, so it cannot dispatch mouse events. However, you can use the addEventListener() method of the display object container that contains the Bitmap object.

like image 81
Jason Sturges Avatar answered Dec 28 '22 22:12

Jason Sturges