0

I am having problems understanding correctly how to dispatch events and capture them in another class.

In this case, I am trying to emulate a mouse click dispatched from an "clickM" class.

On stage I have 2 movieclips to test, a custom cursor and the listeners to capture the click event.

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));
    }
}

}

Stage code, rojo & morado are 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);
}

If I click over morado or rojo, there's no problem - I can get their names. If I just let the code run, I can't get their names, I just get "mano", which is the custom cursor I'm using.

How can I get the desired behavior?

Regards.

Javier S
  • 115
  • 2
  • 13

1 Answers1

0

Add mouseEnabled=false; inside clickM constructor. This should make Flash to ignore your mano in the event dispatch phase, so the underlying object should be the primary target if there's any, otherwise the target will be the stage. If your custom cursor contains more movie clips, you should also add mouseChildren=false;.

public function clickM() {
    mouseEnabled=false;
    // possibly add this too
    mouseChildren=false;
    myTimer.start();
    myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}
Vesper
  • 18,599
  • 6
  • 39
  • 61
  • Unfortunately it didn't worked, for some reason it's still registering mano as the clicked object, actually I even tried those snipepts in my main document code, it still is registering mano as the clicked object – Javier S Mar 11 '14 at 14:40
  • Hmm, weird. Probably try dispatching a `MouseEvent.MOUSE_DOWN` with following `MouseEvent.MOUSE_UP` instead? These want coordinates, which you can get by calling `this.localToGlobal(new Point())`. – Vesper Mar 12 '14 at 04:39
  • 1
    Thanks @Vesper, definitely something else is failing. I simlpified (more) the code by avoiding the external class, tried using your suggestion still getting problems to get it working correctly. It fires when I click directly, but not if I just hover over my movie clips (returns root1). – Javier S Mar 12 '14 at 12:21