MOUSE_DOWN和MOUSE_UP不调度自定义Sprite

时间:2014-08-17 06:54:19

标签: actionscript-3 events

我创建了一个自定义Sprite对象,该对象不调度MouseEvent.MOUSE_DOWN或MouseEvent.MOUSE_UP事件。它正确调度MouseEvent.MOUSE_MOVE事件。

验证所有事件侦听器都已注册。

如果这是一个常见或过于简单的问题,我道歉,但我花了最后一小时谷歌搜索和阅读StackOverflow的问题或答案适合我的情况,只是没有找到一个。据我所知,Sprite是一个InteractiveObject,因此应该调度所有这三个事件而不仅仅是MouseEvent.MOUSE_MOVE事件。

班级声明:

import flash.display.*;
import flash.events.*;
import flash.geom.*;

public class CustomSprite extends Sprite {

main.as中的对象初始化

    var circle:CustomSprite = new CustomSprite();
    circle.graphics.lineStyle(3,0x00FF00);
    circle.graphics.beginFill(0x0000FF,.5);
    circle.graphics.drawCircle(0,0,50);
    circle.graphics.endFill();
    circle.x = 100;
    circle.y = 100;

构造函数中的事件侦听器注册:

addEventListener(MouseEvent.MOUSE_DOWN,mouseDownListener);
if (hasEventListener(MouseEvent.MOUSE_DOWN)) trace("MOUSE_DOWN listener exists");

addEventListener(MouseEvent.MOUSE_UP,mouseUpListener);
if (hasEventListener(MouseEvent.MOUSE_UP)) trace("MOUSE_UP listener exists");

addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveListener);
if (hasEventListener(MouseEvent.MOUSE_MOVE)) trace("MOUSE_MOVE listener exists");

事件监听器注册输出:

MOUSE_DOWN listener exists
MOUSE_UP listener exists
MOUSE_MOVE listener exists

侦听器函数作为CustomSprite类的一部分:

    private function mouseDownListener(e:MouseEvent):void {
        trace("mouseDownListener");
    }

    private function mouseUpListener(e:MouseEvent):void {
        trace("mouseUpListener");
    }

    private function mouseMoveListener(e:MouseEvent):void {
        trace("mouseMoveListener");
    }

将鼠标悬停在对象上并多次单击后输出:

mouseMoveListener
mouseMoveListener
mouseMoveListener
mouseMoveListener

提前感谢您提供的任何帮助 - 非常感谢!

1 个答案:

答案 0 :(得分:1)

从您提供的源代码看起来,您不会将事件侦听器添加到圈子本身。我测试过的这个片段有效,前提是我没有CustomSprite,只有Sprite

var circle:CustomSprite = new CustomSprite();
circle.graphics.lineStyle(3,0x00FF00);
circle.graphics.beginFill(0x0000FF,.5);
circle.graphics.drawCircle(0,0,50);
circle.graphics.endFill();
circle.x = 100;
circle.y = 100;

circle.addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void{
   trace("mouseDown");
});
circle.addEventListener(MouseEvent.MOUSE_UP, function(event:MouseEvent):void{
   trace("mouseUp");
});
circle.addEventListener(MouseEvent.MOUSE_MOVE, function(event:MouseEvent):void{
   trace("mouseMove");
});