-1

I have two movieclips one over other. using mouse click event I want to bring one of them in the front. It works 1 or 2 times then it just stops responding to mouse clicks. I don't know what is happening. I tried hard but could not get it work. Here is my code for document class :

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;


public class THREE2DP extends MovieClip {

    public var page11:page1;
    public var page22:page2;

    public var scene11:scene1;
    public var scene22:scene2;


    public function THREE2DP() {
        // constructor code
        stop();
        createscene();
        createpage2();
        createpage1();
        this.addEventListener(Event.ENTER_FRAME, enterframehandler);
    }

    public function enterframehandler(e:Event):void
    {
        if(scene11.front)
        {
            bringToFront(page11);
            scene22.front = false;
        }

        if(scene22.front)
        {
            bringToFront(page22);
            scene11.front = false;
        }

    }
    private function bringToFront(mcl:MovieClip) 
    {
        mcl.parent.setChildIndex(mcl,mcl.parent.numChildren - 1);
    }
    public function createpage1()
    {
        page11 = new page1();
        addChild(page11);
        page11.x = 0;

        page11.y = 0;
    }
    public function createpage2()
    {
        page22 = new page2();
        addChild(page22);
        page22.x = 0;

        page22.y = 0;
    }
    public function createscene()
    {
        scene11 = new scene1();
        addChild(scene11);
        scene11.x = 0;
        scene11.y = 635;

        scene22 = new scene2();
        addChild(scene22);
        scene22.x = 400;
        scene22.y = 635;
    }


}

}

here is code for scene11 movieclip custom class

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;


public class scene1 extends MovieClip {

    public var front:Boolean = false;

    public function scene1() {
        // constructor code
        stop();
        this.addEventListener(MouseEvent.CLICK, clickhandler, false, 0, true);
    }
    public function clickhandler(event:MouseEvent): void
    {

        front = true;
    }
}

}

code for scene22 custom class is

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;


public class scene2 extends MovieClip {

    public var front:Boolean = false;

    public function scene2() {
        // constructor code
        stop();
        this.addEventListener(MouseEvent.CLICK, clickhandler, false, 0, true);
    }
    public function clickhandler(event:MouseEvent):void
    {
        front = true;
    }
}   
}

Upon click on movieclips scene11 and scene22, movieclip page11 and page22 should come on front of stage respectively but that happens only once for each page, after that nothing changes.

blue112
  • 52,634
  • 3
  • 45
  • 54
sam
  • 13
  • 3

2 Answers2

1

my earlier logic for custom classwas faulty. took me 2 day. added mouse_down event to make it work. i some how managed to do this right by changing custom class code to this

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;


public class scene1 extends MovieClip {

    public var front:Boolean = false;

    public function scene1() {
        // constructor code
        stop();
        this.addEventListener(MouseEvent.MOUSE_DOWN, presshandler, false, 0, true);
        this.addEventListener(MouseEvent.CLICK, clickhandler, false, 0, true);
    }
    public function presshandler(event:MouseEvent): void
    {

        front = true;
    }
    public function clickhandler(event:MouseEvent): void
    {

        front = false;
    }
}

}

also modified document class code by calling pagechange() function from enterframehandler() function for sake of simplicity

public function pagechange()
    {
        if (scene11.front && scene22.front == false)
        {
            bringToFront(page11);

        }

        if (scene22.front && scene11.front == false)
        {
            bringToFront(page22);

        }
    }
    private function bringToFront(mcl:MovieClip)
    {
        mcl.parent.setChildIndex(mcl,mcl.parent.numChildren - 1);
    }
sam
  • 13
  • 3
0

Instead of using that bringToFront method, you could just readd page11 and page22 on stage with addChild(). addChild always adds a child on front of everything else in the parent.

public function enterframehandler(e:Event):void
{
    if(scene11.front)
    {
        addChild(page11);
        scene22.front = false;
    }

    if(scene22.front)
    {
        addChild(page22);
        scene11.front = false;
    }

}
  • thanks for reply, i changed enterframehandler function with yours(Cristina Georgescu) but it gives me same result as mine before. it happens only twice after that it just stop responding to mouse click. but program should be able to bring forth movieclip on to stage any number of times. – sam Jun 17 '14 at 17:25
  • I also think that using an ENTER_FRAME event is overkill in your case and the front variables might not update correctly. I mean, you want the pages to come in front of everything else only on click. So, in your THREE2DP add click event listeners for scene11, scene22 and in the handler methods, do the addChild(page11) and respectively, addChild(page22). – Cristina Georgescu Jun 18 '14 at 09:48