AS3 MovieClip点击事件无效

时间:2014-09-02 17:12:46

标签: actionscript-3 flash actionscript

我正在使用as3处理闪存滑块,无法点击点击事件以点击MovieClip。

点击事件在定位舞台时效果很好,但不是任何MC。

以下是项目中的所有代码。

有问题的部分位于 SLIDER.as

的底部
canvas.addEventListener(MouseEvent.CLICK, this.canvasClick);

舞台和图书馆都是空的。提前谢谢。

PS:我对闪光灯很新。

MAIN.AS

package  {

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


public class Main extends MovieClip {

    public function Main() {
        addEventListener(Event.ADDED_TO_STAGE, init)
    }

    private function init(e:Event){
        var images:Array = new Array("./media/engines/Engines-1.jpg", "./media/engines/Engines-2.jpg", "./media/engines/Engines-3.jpg", "./media/engines/Engines-4.jpg", "./media/engines/Engines-5.jpg", "./media/engines/Engines-6.jpg", "./media/engines/Engines-7.jpg", "./media/engines/Engines-8.jpg", "./media/engines/Engines-9.jpg", "./media/engines/Engines-10.jpg", "./media/engines/Engines-11.jpg", "./media/engines/Engines-12.jpg", "./media/engines/Engines-13.jpg");
        var slider:Slider = new Slider(stage, images);          
    }
}

}

SLIDER.AS

package  {

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

public class Slider extends MovieClip {

    private var images:Array;
    private var STAGE:Stage;

    private var stageWidth:Number;
    private var stageHeight:Number;
    private var centerX:Number;
    private var centerY:Number;
    private var tsHeight:Number = 100;
    private var tsPadding:Number = 10;


    public function Slider(stageGlobal, imageConfig:Array) {
        this.STAGE = stageGlobal;
        this.images = imageConfig;

        this.stageWidth = this.STAGE.stageWidth;
        this.stageHeight = this.STAGE.stageHeight;

        this.centerX    = this.stageWidth / 2;
        this.centerY = this.stageHeight / 2;

        this.createCanvas();
    }

    public function createCanvas():void {
        // SLIDER HOLDER
        var canvas:MovieClip = new MovieClip();
        canvas.graphics.drawRoundRect(0, 0, this.stageWidth, this.stageHeight, 15, 15);
        canvas.opaqueBackground = 0xDDDDDD;
        canvas.mouseEnabled = true;
        this.STAGE.addChild(canvas);


        // HEADER / TITLE HOLDER
        var header:MovieClip = new MovieClip();
        header.graphics.drawRoundRect(0, 0, this.stageWidth, 40, 15, 15);
        header.opaqueBackground = 0x000000;

        canvas.addChild(header);


        // FOOTER ACTION HOLDER
        var footer:MovieClip = new MovieClip();
        footer.graphics.drawRoundRect(0, this.stageHeight - 40, this.stageWidth, 40, 15, 15);
        footer.opaqueBackground = 0x000000;

        canvas.addChild(footer);

        // THUMBSTRIP HOLDER
        var thumbstrip:MovieClip = new MovieClip();
        thumbstrip.graphics.drawRoundRect(0, this.stageHeight - 160, this.stageWidth, 120, 15, 15);
        thumbstrip.opaqueBackground = 0x555555;

        canvas.addChild(thumbstrip);

        canvas.addEventListener(MouseEvent.CLICK, this.canvasClick);
    }

    private function canvasClick(e:MouseEvent):void {
        trace(e);
    }

}

}

2 个答案:

答案 0 :(得分:4)

这可能与你绘制MovieClip的方式有关。

来自文档:“不透明的背景区域不响应鼠标事件。”

尝试绘制这样的矩形:

        // SLIDER HOLDER
        var canvas:MovieClip = new MovieClip();
        canvas.graphics.beginFill(0xDDDDDD);
        canvas.graphics.drawRoundRect(0, 0, this.stageWidth, this.stageHeight, 15, 15);
        canvas.graphics.endFill();
        canvas.mouseEnabled = true;
        this.STAGE.addChild(canvas);

答案 1 :(得分:0)

您需要在添加到舞台的滑块后添加“事件”,以便在需要编写的滑块类中添加

package  {

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

    public class Slider extends MovieClip {

        private var images:Array;
        private var STAGE:Stage;

        private var stageWidth:Number;
        private var stageHeight:Number;
        private var centerX:Number;
        private var centerY:Number;
        private var tsHeight:Number = 100;
        private var tsPadding:Number = 10;


        public function Slider(stageGlobal, imageConfig:Array) {

            this.STAGE = stageGlobal;
            this.images = imageConfig;
            this.addEventListener(Event.ADDED_TO_STAGE, added);

        }

        public function added(e:Event)
        {


            this.stageWidth = this.STAGE.stageWidth;
            this.stageHeight = this.STAGE.stageHeight;

            this.centerX    = this.stageWidth / 2;
            this.centerY = this.stageHeight / 2;

            this.createCanvas();
        }

        public function createCanvas():void {
            // SLIDER HOLDER
            var canvas:MovieClip = new MovieClip();
            canvas.graphics.drawRoundRect(0, 0, this.stageWidth, this.stageHeight, 15, 15);
            canvas.opaqueBackground = 0xDDDDDD;
            canvas.mouseEnabled = true;
            this.STAGE.addChild(canvas);


            // HEADER / TITLE HOLDER
            var header:MovieClip = new MovieClip();
            header.graphics.drawRoundRect(0, 0, this.stageWidth, 40, 15, 15);
            header.opaqueBackground = 0x000000;

            canvas.addChild(header);


            // FOOTER ACTION HOLDER
            var footer:MovieClip = new MovieClip();
            footer.graphics.drawRoundRect(0, this.stageHeight - 40, this.stageWidth, 40, 15, 15);
            footer.opaqueBackground = 0x000000;

            canvas.addChild(footer);

            // THUMBSTRIP HOLDER
            var thumbstrip:MovieClip = new MovieClip();
            thumbstrip.graphics.drawRoundRect(0, this.stageHeight - 160, this.stageWidth, 120, 15, 15);
            thumbstrip.opaqueBackground = 0x555555;

            canvas.addChild(thumbstrip);

            canvas.addEventListener(MouseEvent.CLICK, canvasClick);
        }

        private function canvasClick(e:MouseEvent):void {
            trace(e);
        }

    }

}

我希望它会有所帮助:)。