在AS3中未触发Mouse_down事件

时间:2011-03-29 23:51:01

标签: flash actionscript-3

我写了一些代码是AS3。我的目标是单击MainTimeline阶段并在函数trace()中运行runDraw()。目前,MOUSE_DOWN事件永远不会被触发,我无法弄明白。

代码更新

package circles
{
    import flash.display.Sprite;
    import flash.events.*;
    import flash.geom.Point;
    import flash.display.DisplayObjectContainer;
    import flash.display.MovieClip;

    public class Circles extends Sprite
    {
        var circCentre:Sprite = new Sprite();
        var circOuter:Sprite = new Sprite();
        var point:Sprite = new Sprite();

        trace("Class stuff initilized");

        function Circles():void
        {   
            this.addEventListener(MouseEvent.MOUSE_DOWN, runDraw);
            trace("Constructor done");
        }

        function runDraw(e:MouseEvent):void
        {
            trace("runDraw(e)");
            // centre circle draw and add to timeline
            circCentre.graphics.lineStyle(3, 0x000000);
            circCentre.graphics.beginFill(0xFF6600);
            circCentre.x = e.stageX;
            circCentre.y = e.stageY;
            circCentre.graphics.drawCircle(e.stageX, e.stageY, 100);
            circCentre.graphics.endFill();
            this.addChild(circCentre);
            trace("mc.addChild(circCentre)");

            // smaller outer circle
            circCentre.addChild(circOuter);
            // attach to parent;
            circOuter.graphics.lineStyle(1, 0xDD2211);
            circOuter.graphics.beginFill(0x66FF00);
            circOuter.x = 200;
            circOuter.y = e.stageY;
            circOuter.graphics.drawCircle(200, e.stageY, 50);
            circOuter.graphics.endFill();

            // the draw point;
            circOuter.addChild(point);
            point.graphics.moveTo(5, 0);
            point.graphics.lineTo(5, 10);
            point.graphics.moveTo(0, 5);
            point.graphics.lineTo(10, 5);
            point.x = 50;
            point.y = 90;
            stage.addEventListener(Event.ENTER_FRAME, rotationDraw);
            stage.addEventListener(MouseEvent.MOUSE_UP, finish);
        }

        public function rotationDraw(e:Event):void
        {
            circCentre.rotation++;
            circOuter.rotation++;
        }

        public function finish(e:MouseEvent):void
        {
            stage.removeEventListener(Event.ENTER_FRAME, rotationDraw);
        }
    }
}

非常感谢。

2 个答案:

答案 0 :(得分:1)

以下作品(我修改了您的类代码)。

<强>时间轴

var c:Circles = new Circles(stage);
addChild(c);

类文件

// removed package name for testing
package
{
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.*;
    import flash.geom.Point;
    import flash.display.DisplayObjectContainer;
    import flash.display.MovieClip;

    public class Circles extends Sprite
    {
        var circCentre:Sprite = new Sprite();
        var circOuter:Sprite = new Sprite();
        var point:Sprite = new Sprite();

        var _stage:Stage;

        // shouldn't really have trace statements out here
        trace("Class stuff initilized");

        function Circles(stage:Stage):void
        {   
            _stage = stage;

            _stage.addEventListener(MouseEvent.MOUSE_DOWN, runDraw);
            trace("Constructor done");
        }

        function runDraw(e:MouseEvent):void
        {
            trace("runDraw(e)");
            // centre circle draw and add to timeline
            circCentre.graphics.lineStyle(3, 0x000000);
            circCentre.graphics.beginFill(0xFF6600);
            circCentre.x = e.stageX;
            circCentre.y = e.stageY;
            circCentre.graphics.drawCircle(e.stageX, e.stageY, 100);
            circCentre.graphics.endFill();
            _stage.addChild(circCentre);
            trace("mc.addChild(circCentre)");

            // smaller outer circle
            circCentre.addChild(circOuter);
            // attach to parent;
            circOuter.graphics.lineStyle(1, 0xDD2211);
            circOuter.graphics.beginFill(0x66FF00);
            circOuter.x = 200;
            circOuter.y = e.stageY;
            circOuter.graphics.drawCircle(200, e.stageY, 50);
            circOuter.graphics.endFill();

            // the draw point;
            circOuter.addChild(point);
            point.graphics.moveTo(5, 0);
            point.graphics.lineTo(5, 10);
            point.graphics.moveTo(0, 5);
            point.graphics.lineTo(10, 5);
            point.x = 50;
            point.y = 90;
            _stage.addEventListener(Event.ENTER_FRAME, rotationDraw);
            _stage.addEventListener(MouseEvent.MOUSE_UP, finish);
        }

        public function rotationDraw(e:Event):void
        {
            circCentre.rotation++;
            circOuter.rotation++;
        }

        public function finish(e:MouseEvent):void
        {
            _stage.removeEventListener(Event.ENTER_FRAME, rotationDraw);
        }
    }
}

答案 1 :(得分:0)

mc MovieClip没有维度,因此MOUSE_DOWN事件无法使用。你需要绘制这个命中区域。试试这个:

mc.graphics.beginFill(0xFFFFFF,0);
mc.graphics.drawRect(0,0,800,600);//change 800,600 to whatever is your stage size