all groups > flash actionscript > july 2006 >
You're in the

flash actionscript

group:

losing mouse events for dynamically created movie clips



losing mouse events for dynamically created movie clips SPGAnne
7/10/2006 10:55:35 PM
flash actionscript: HELP! I must be doing something really dumb.
I have a single frame .fla with a movie clip on it called circle_mc, and the
following code on frame 1:

var boxmaker:BoxMaker=new BoxMaker(this,circle_mc);

I've attached the code for the 2 classes BoxMaker and Box. All works fine
until I add the following to frame 1 main timeline:
circle_mc.onRollOver = function() {
trace("roll over received on the circle_mc" );
}
my circle_mc gets the onRollOver but my box instances no longer do. I need to
be able to stack movie clips on top of movie clips the one on the bottom could
be created statically at author time or dynamically at run time and I need it
to be able to
respond to mouse events independently of the ones on top. Am I doing
something really dumb? Thanks very much in advance for anyone who can help.



import Box;

class BoxMaker{

private var box1:Box;
private var box2:Box;

private var target_mc:MovieClip;

public function BoxMaker(toplevel_mc:MovieClip,
target:MovieClip){
target_mc=target;
//initialize boxes
box1=new Box(target_mc,40,26,50,50,10);
box2=new Box(target_mc,70,130,60,90,11);
// register listeners
box1.addEventListener("onOver",this);
box2.addEventListener("onOver",this);
}

private function onOver(evt:Object):Void{
trace("onOver received by BoxMaker");
evt.target.onRollOut=function():Void{
trace("onRollOut received by BoxMaker for onOver function");
}
}
}

import mx.events.EventDispatcher;

class Box {
private var target_mc:MovieClip;
private var box_mc:MovieClip;
private var xPos:Number;
private var yPos:Number;
private var xWidth:Number;
private var yHeight:Number;
private var depth:Number;

private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;

//constructor
public function Box(target:MovieClip,
xPos:Number,
yPos:Number,
w:Number,
h:Number,
d:Number){
target_mc=target;
this.xPos=xPos;
this.yPos=yPos;
xWidth=w;
yHeight=h;
depth=d;
EventDispatcher.initialize(this);
drawBox();
}
private function drawBox():Void{
if(depth==undefined){
depth=target_mc.getNextHighestDepth();
}
box_mc = target_mc.createEmptyMovieClip("box_mc",depth);
box_mc._x=xPos;
box_mc._y=yPos;
box_mc.beginFill(0x0000FF,40);
box_mc.moveTo(0, 0);
box_mc.lineTo(xWidth, 0);
box_mc.lineTo(xWidth, yHeight);
box_mc.lineTo(0, yHeight);
box_mc.lineTo(0, 0);
box_mc.endFill();
setRollOver(box_mc);
}

private function setRollOver(mc:MovieClip):Void{
var thisObj:Box=this;
mc.onRollOver=function():Void{
trace("onRollOver received by box instance");
var eventObject=new Object();
eventObject.target=this;
eventObject.type="onOver";
thisObj.dispatchEvent(eventObject);
}
}

}
Re: losing mouse events for dynamically created movie clips blemmo
7/11/2006 12:07:29 AM
This is because the box MCs are children of the circle MC. Flash doesn't
propagate the events to the children if the parent has some event handling
code, so it won't reach the boxes when circle consumes this event. It's
described very good here:
http://www.senocular.com/flash/tutorials/buttoncapturing/. There are also some
ways described how to work around this.

cheers,
blemmo
Re: losing mouse events for dynamically created movie clips SPGAnne
7/11/2006 12:44:42 AM
AddThis Social Bookmark Button