as3 - 从外部类调度鼠标事件

时间:2014-03-11 12:55:34

标签: actionscript-3 message dispatch

我在正确理解如何分派事件并在另一个类中捕获事件时遇到了问题。

在这种情况下,我试图模拟从“clickM”类调度的鼠标单击。

在舞台上我有2个要测试的动画片段,一个自定义光标和用于捕获click事件的侦听器。

clickM:

package {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
 import flash.events.Event; //dispatcher
import flash.events.MouseEvent;// mouse event



public class clickM extends MovieClip {
    private var delay: uint = 3000;
    private var repeat: uint = 0; //se va por todo el tiempo
    private var myTimer: Timer = new Timer(delay, repeat);


    public function clickM() {
        myTimer.start();
        myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
    }

    private function timerHandler(e: TimerEvent): void {
        //repeat--;
        //statusTextField.text = ((delay * repeat) / 1000) + " seconds left.";
        trace ( "simulate click...");
        //dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        this.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
    }
}

}

舞台代码,rojo& morado是movieclips:

import flash.events.MouseEvent;

stage.addEventListener(Event.ENTER_FRAME, myFunction);

var mano: clickM = new clickM();
mano.name = "mano";

addChild (mano);

morado.addEventListener(MouseEvent.CLICK, cl);
rojo.addEventListener(MouseEvent.CLICK, cl);

stage.addEventListener(MouseEvent.CLICK, cl);

function myFunction(event: Event) {
mano.x = mouseX;
mano.y = mouseY;
}

function cl(e: MouseEvent) {
trace("click over " + e.target.name);
}

如果我点击morado或rojo,没有问题 - 我可以得到他们的名字。如果我只是让代码运行,我就无法得到他们的名字,我只是得到“mano”,这是我正在使用的自定义光标。

如何获得所需的行为?

问候。

1 个答案:

答案 0 :(得分:0)

mouseEnabled=false;构造函数中添加clickM。这应该使Flash在事件调度阶段忽略您的mano,因此如果有的话,底层对象应该是主要目标,否则目标将是阶段。如果您的自定义光标包含更多影片剪辑,您还应添加mouseChildren=false;

public function clickM() {
    mouseEnabled=false;
    // possibly add this too
    mouseChildren=false;
    myTimer.start();
    myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}
相关问题