MouseChildren and buttonMode in AS2

| 2 min read

A quick post for an AS2 class I wrote. I won't do that a lot as I'm not really using AS2 anymore.

Have you ever had a Button in another Button in as2? Or let's say, you wanted a onRollOver on a MovieClip and a onRelease on another MovieClip in this MovieClip?

Well, it happened to me several times and this is just not possible by using only onRelease and onRollOver in the same time. In AS2, the first onRelease or whatever you use that will give a pointer cursor will "block" the onRelease/onRollOver of the children, annoying...

In AS3, life is easy, you manage it with the mouseChildren and buttonMode properties.

I don't know how you solved this problem, maybe there's another simpler solution (let me know, I'm curious) but mine was to play with the hitTest method of the MovieClip.

I wrote a class that I found useful for me. After creating a instance with the MovieClip target as a parameter, the class dispatch a MouseManager.MOUSE_OVER and MouseManager.MOUSE_OUT.

You can see a demo and download the source.

The code of the class:

/*
*
* Copyright info: Free to use and change, an notification email will be welcome for a commercial use
* Actionscript: built for actionscript 2.0
* 05-2008
*
* @author Romuald Quantin - romu@soundstep.com - www.soundstep.com
* @version 1.0
* @usage
*
*/



import mx.utils.Delegate;
import mx.events.EventDispatcher;

class com.soundstep.utils.managers.MouseManager {

private var __target:MovieClip;
private var __mouseListener:Object;
private var __over:Boolean = false;
private var dispatchEvent:Function;

public var addEventListener:Function;
public var removeEventListener:Function;

public static var MOUSE_OVER:String = "mouse_over";
public static var MOUSE_OUT:String = "mouse_out";

public function MouseManager(target:MovieClip) {
EventDispatcher.initialize(this);
__target = target;
__mouseListener = {};
Mouse.addListener(__mouseListener);
__mouseListener.onMouseMove = Delegate.create(this, mouseOverOut);
mouseOverOut();
}

// PRIVATE
//__________________________________________________________

private function mouseOverOut():Void {
var xMousePos:Number = _level0._xmouse;
var yMousePos:Number = _level0._ymouse;
if (__target.hitTest(xMousePos, yMousePos, true)) {
if (!__over) {
__over = true;
this.dispatchEvent({type:MouseManager.MOUSE_OVER, target:__target});
}
}
else {
if (__over) {
__over = false;
this.dispatchEvent({type:MouseManager.MOUSE_OUT, target:__target});
}
}
}

// PUBLIC
//__________________________________________________________

public function get over():Boolean {
return __over;
}

}

and how to use it:

import mx.utils.Delegate;
import com.soundstep.utils.managers.MouseManager;

var mm:MouseManager = new MouseManager(mc);
mm.addEventListener(MouseManager.MOUSE_OVER, Delegate.create(this, mouseOver));
mm.addEventListener(MouseManager.MOUSE_OUT, Delegate.create(this, mouseOut));

var t:String = mc.mcText.text;

function mouseOver():Void {
mc.mcText.text = t + " rollover";
}

function mouseOut():Void {
mc.mcText.text = t;
}

mc.bt.onRollOver = function():Void {
this.btText.text = "Button in MovieClip rollover";
}

mc.bt.onRollOut = function():Void {
this.btText.text = "Button in MovieClip";
}