views:

182

answers:

2

Hi, I have a movieClip with two buttons inside.

The problem is that when the mouse is over these two buttons, the code that manages the movieClip stops working, as if the mouse is not over the MC (the buttons are children of the MC, shouldn't it work regardless?).

Could you please share some advice? Thanks

/*mc follows mouse. I can't click btns because when mouse rollover  btns the mc moves*/
function showImgOptions (e:Event):void{
    if (mc.hitTestPoint(mouseX,mouseY,false)){
     mc.y = mc.y;
     mc.x = mc.x;
    }else{
     var delayX:int = mc.x - mouseX;
     var delayY:int = mc.y - mouseY;
     mc.x -= delayX / 6;
     mc.y -= delayY/6;
    }
}
mc.btn1.addEventListener (MouseEvent.CLICK, closeClick);
mc.btn2.addEventListener (MouseEvent.CLICK, zoomClick);
function closeClick (e:MouseEvent):void{}
function zoomClick (e:MouseEvent):void{}
stage.addEventListener (Event.ENTER_FRAME, showImgOptions);
addChild (mc);

Changed the code to:

var mc:menuMC = new menuMC();

addChild(mc);

var p:Point = mc.localToGlobal(new Point(mc.mouseX,mc.mouseY));

/*mc follows mouse. I can't click btns because when mouse rollover  btns the mc moves*/
function showImgOptions (e:Event):void
{

    if (! mc.hitTestPoint(p.x,p.y,false))
    {
     mc.y = mc.y;
     mc.x = mc.x;
    }else{
     //move mc towards mc.parent's mouseX and mouseY
     var delayX:int = mc.x - mouseX;
     var delayY:int = mc.y - mouseY;
     mc.x -= delayX / 6;
     mc.y-=delayY/6;
    }
}

mc.btn1.addEventListener (MouseEvent.CLICK, closeClick);
mc.btn2.addEventListener (MouseEvent.CLICK, zoomClick);
function closeClick (e:MouseEvent):void
{
}
function zoomClick (e:MouseEvent):void
{
}
stage.addEventListener (Event.ENTER_FRAME, showImgOptions);

And now I get this error:

TypeError: Error #1010: A term is undefined and has no properties.

Here you can download an FLA. Test it and try to click on the buttons 1 and 2, inside the MC following the mouse

A: 

hitTestPoint expects stage coordinates:

The x and y parameters specify a point in the coordinate space of the Stage, not the display object container that contains the display object (unless that display object container is the Stage).

Use localToGlobal to get the stage coordinates:

var p:Point = mc.localToGlobal(new Point(mc.mouseX, mc.mouseY));
if(!mc.hitTestPoint(p.x, p.y,false))
{
  //move mc towards mc.parent's mouseX and mouseY
}
Amarghosh
/*mc follows mouse. I can't click btns because when mouse rollover btns the mc moves*/function showImgOptions (e:Event):void{ if (mc.hitTestPoint(mouseX,mouseY,false)){ mc.y = mc.y; mc.x = mc.x; }else{ var delayX:int = mc.x - mouseX; var delayY:int = mc.y - mouseY; mc.x -= delayX / 6; mc.y -= delayY/6; }}mc.btn1.addEventListener (MouseEvent.CLICK, closeClick);mc.btn2.addEventListener (MouseEvent.CLICK, zoomClick);function closeClick (e:MouseEvent):void{}function zoomClick (e:MouseEvent):void{}stage.addEventListener (Event.ENTER_FRAME, showImgOptions);addChild (mc);
cayohueso
Sorry, I don't know how to add the code with the same format as in Flash
cayohueso
click the `edit` link below the question (below the `actionscript-3` tag), and add the code to the bottom of the post. now select the code and hit Ctrl-K to format the code. Scroll down and save the edits.
Amarghosh
Add line breaks at the end of lines (by hitting enter key) - otherwise your code will appear in a long single line.
Amarghosh
Thanks, please see the code
cayohueso
are you trying to make the mc follow the mouse when mouse is not over mc and keep it stationary when mouse is over mc?
Amarghosh
what's the parent of mc? is it the stage itself or some nested movieclip?
Amarghosh
A: 

Solved it!!

Changed the code. I don't know if this helps anybody, but I hope so. Thanks to you all.

stage.addEventListener(Event.ENTER_FRAME, moveMC);

var mc:menuMC = new menuMC();

addChild(mc);

function moveMC(e:Event):void {
 if (mc.hitTestObject(big_mc)) {
  mc.visible = true;
 } else {
  mc.visible = false;
 }
 if (mc.hitTestPoint(mouseX,mouseY,false)) {
  mc.y = mc.y;
  mc.x = mc.x;
 } else {
  var delayX:int = mc.x - mouseX;
  var delayY:int = mc.y - mouseY;
  mc.x -= delayX / 6;
  mc.y-=delayY/6;
 }
}


mc.btn1.addEventListener(MouseEvent.CLICK, onBtn1);
mc.btn2.addEventListener(MouseEvent.CLICK, onBtn2);


function onBtn1(e:MouseEvent):void {
 trace("do something");
}
function onBtn2(e:MouseEvent):void {
 trace("do something else");
}
cayohueso