Graham Long
Graham Long

Reputation: 25

AS3: MouseEvent not working?

When I click on it nothing is being outputted. Is there a reason why the MouseEvent is not being applied to the rectangle. Where am I going wrong?

My Code:

import flash.display.Shape;
import flash.events.MouseEvent;

stop();

var rectangle001:Shape = new Shape;

rectangle001.graphics.beginFill(0x00D783);
rectangle001.graphics.drawRect(10, 10, 50, 50);
rectangle001.addEventListener(MouseEvent.CLICK, rectangle001Click);
function rectangle001Click (event:MouseEvent){
trace("Hello World!");
}
addChild(rectangle001);

All answers appreciated.

Upvotes: 0

Views: 375

Answers (1)

Iggy
Iggy

Reputation: 4888

You should use Sprite instead of Shape if you want mouse interactivity.

Quoting from an answer on this page:

  • Shape is the one with the least possibilities. Use it when you only want a DisplayObject with graphics, and no mouse interaction.

  • Sprite is the parent class of quite everything you need. Since it is a DisplayObjectContainer, you can use it as a basic container for other components. You can also catch mouse events on this one.

  • MovieClip is a Sprite with the ability to use frames. Only use it for frame-by-frame animation (Flash style).

Upvotes: 2

Related Questions