0
votes

OK so I am having a weird issue. I have some movieclips on screen, 4 of them, each with the following code (with different instance names of course):

stage.addEventListener(MouseEvent.MOUSE_DOWN,globalMouseDown,false,0,true); //add a  global mouse listener

function globalMouseDown(e:Event):void {
    //find out if the target is a descendant of this, if not, then something else was clicked.
    var parent:DisplayObject = e.target as DisplayObject;
    while(parent && parent != stage){
        if(parent == this) return;
        parent = parent.parent;
    }

    //something else was clicked that wasn't this, so go to the up state
    gotoAndStop(1);

}

stop();

addEventListener(MouseEvent.MOUSE_DOWN, onHs1Press);
addEventListener(MouseEvent.MOUSE_OVER, onHs1Over);
addEventListener(MouseEvent.MOUSE_OUT, onHs1Out);

function onHs1Press(event:MouseEvent):void 

{

    // toggle between frame 1 and 3 on button press
    gotoAndStop(this.currentFrame == 3 ? 1 : 3);
    parent.addChild(this)
}

function onHs1Over(event:MouseEvent):void
{

    if (currentFrame != 3)
    {
        gotoAndStop(2);
    }
}

function onHs1Out(event:MouseEvent):void
{
    // only switch back to UP state if the button is "pressed"
    if (currentFrame != 3)
    {
        gotoAndStop(1);
    }
} 

Basically it lets you hover your mouse and the movieclip changes and then when you click on it a little pop up window appears until you click the movieclip again to close it.

There is also a button on screen that allows you to move forward or backwards to other frames with this code:

Next.addEventListener(MouseEvent.CLICK,Nclick);
function Nclick(event:MouseEvent):void { 
    nextFrame();
}
Back.addEventListener(MouseEvent.CLICK,Bclick);
function Bclick(event:MouseEvent):void { 
    prevFrame();
}

The button code is on the main timeline and the movieclip code is on the movieclip's timeline.

For some reason if you have the movieclip in the DOWN state (with the popup window open) and you click the button to go to the next frame, the movieclip follows onto the next and any other frames instead of just going away.

I have this same code present on other frames and none of the other ones behave this way, it's really weird.

You can even click it still when its on the other frames and bring up the popup window where the movieclip and code aren't even present.

What's going on with it?

1
If you comment out the parent.addChild(this) code does it work properly? Or comment out the global mouse listener?BadFeelingAboutThis
You shouldn't use frames. Frames are there for quick/easy animations. Don't use them to control code. Also, in general, you shouldn't ever use parent.moot
There is a comment on the global mouse listener already isn't there? And adding a comment to the parent.addChild(this) does nothing. It's so weird because I have the EXACT same code on another frame and it doesn't do this. As far as the architecture of the piece goes there is the main timeline and on this frame of the main timeline there are 4 movieclips, the first piece of code is on the first frame of each of the 4 movieclips, so the code is not on the maintime line. The second piece of code, for the next and back buttons IS on the main timeline.cholverson
Ok, I think the problem is the parent.addChild(this) part. I was using that to make sure all the child movieclips displayed on top of everything else. Is there another way of accomplishing this?cholverson
when I say comment out I mean turn the whole line into a comment (effectively removing the line of code to test whether it's the cause).BadFeelingAboutThis

1 Answers

1
votes

I tried testing this, and could reproduce your issue. If you add a movieclip to the stage in FlashPro, after changing it's index or parentage, it will from that point on be treated like an object created from code and the timeline will ignore it and even create another instance of it on a frame where it is created.

You'll have to manually remove the buttons from the display list.

function Nclick(event:MouseEvent):void { 
    nextFrame();
    removeBtns();
}

function Bclick(event:MouseEvent):void { 
    prevFrame();
    removeBtns();
}

function removeBtns():void {
    if(currentFrame != 2){  //whatever the frame of your buttons is
        if(btn1 && btn1.parent) removeChild(btn1); //btn1 being whatever your button instnace name is
        if(btn2 && btn2.parent) removeChild(btn2);  //repeat for all buttons
    }
}

OR If you'd prefer to have encapsulated code, instead of the above, put this on your button class/timeline:

var myFrame:int = MovieClip(parent).currentFrame;

this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
this.addEventListener(Event.REMOVED_FROM_STAGE,removedHandler);

function enterFrameHandler(e:Event):void {
    if(MovieClip(parent).currentFrame != myFrame){
        parent.removeChild(this);
    }
}

function removedHandler(e:Event):void {
    this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
    this.removeEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
}