开发者

Mouse_down event not being triggered in AS3

开发者 https://www.devze.com 2023-02-20 20:31 出处:网络
I\'ve got some code written is AS3. My aim is to have the MainTimeline stage clicked and to run the trace() within function runDraw(). At the moment, the MOUSE_DOWN event is never being triggered and

I've got some code written is AS3. My aim is to have the MainTimeline stage clicked and to run the trace() within function runDraw(). At the moment, the MOUSE_DOWN event is never being triggered and I can't figure it out.

Code update:

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(circCen开发者_运维百科tre)");

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

Many thanks.


The following works (I have modified your class code).

Timeline

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

Class file

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


The mc MovieClip has no dimensions, so there is no hitarea for the MOUSE_DOWN event to work. You need to draw this hit area. Try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消