mouseChildren and buttonMode in AS2
Posted by: Romuald in talking, tags: as2, class, flashA 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";
-
}



Entries (RSS)
June 7th, 2008 at 10:57 am
He there, I came a cross this post and I also added a ON_CLICK handling routine. I thought I'd post is, if you or others might think it is handy. I basically on roll over/out , push/pop the __target clip into a static array. Then when a mouse down is registered, it checks if the last element == __target.
import mx.utils.Delegate;
import mx.events.EventDispatcher;
class com.soundstep.utils.managers.MouseManager
{
public static var MOUSE_OVER:String = "mouse_over";
public static var MOUSE_OUT:String = "mouse_out";
public static var MOUSE_CLICK:String = "mouse_click";
private static var clipStack:Array;
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 function MouseManager(target:MovieClip) {
EventDispatcher.initialize(this);
if(clipStack.length == 0 || clipStack == undefined) clipStack = [];
__target = target;
__mouseListener = {};
Mouse.addListener(__mouseListener);
__mouseListener.onMouseMove = Delegate.create(this, mouseOverOut);
__mouseListener.onMouseDown = Delegate.create(this, mouseClick);
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});
clipStack.push(__target)
}
}
else {
if (__over) {
__over = false;
this.dispatchEvent({type:MouseManager.MOUSE_OUT, target:__target});
clipStack.pop();
}
}
}
private function mouseClick():Void{
if (__over && __target == clipStack[clipStack.length-1]) {
this.dispatchEvent({type:MouseManager.MOUSE_CLICK, target:__target});
}
}
// PUBLIC
//__________________________________________________________
public function get over():Boolean {
return __over;
}
}
June 7th, 2008 at 6:48 pm
I didn't test it, but I think it might be. Thanks for posting it.
November 11th, 2008 at 7:01 am
Excellent. That was exactly what I'm looking for.
I'm a beginner in Flash and just start to use it for developping eLearning content (that's why I must use AS2 for now).
For sure you are in my bookmarks.
Great Thanks
August 25th, 2009 at 12:35 am
sweet class...
thanks for the post!
April 22nd, 2010 at 4:59 pm
How can I apply this to multiple items within a loop. It seems to only work for one item.