如何在as3中将影片剪辑放在前面

时间:2014-06-17 06:32:53

标签: actionscript-3 movieclip

我有两个影片剪辑。使用鼠标点击事件我想将其中一个带到前面。它工作1或2次然后它只是停止响应鼠标点击。我不知道发生了什么。我努力但却无法运作。 这是我的文档类代码:

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;
    }


}

}

这里是scene11 movieclip自定义类的代码

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;
    }
}

}

scene22自定义类的代码是

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;
    }
}   
}

单击movieclips scene11和scene22后,movieclip page11和page22应分别位于舞台前方,但每个页面只发生一次,之后没有任何变化。

2 个答案:

答案 0 :(得分:1)

我早期的自定义类逻辑有问题。花了我2天。添加了mouse_down事件以使其工作。 我有些人通过将自定义类代码更改为此

来设法做到这一点
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;
    }
}

}

还通过从enterframehandler()函数调用pagechange()函数来修改文档类代码,以简化

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);
    }

答案 1 :(得分:0)

您可以使用addChild()在舞台上阅读page11和page22,而不是使用那个bringToFront方法。 addChild总是在父级的其他所有内容上添加一个子级。

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

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

}