简体   繁体   English

ActionScript3 MouseEvent.CLICK无法正常工作

[英]ActionScript3 MouseEvent.CLICK not working with no errors

What i am trying to achieve is that when the cowboy is clicked he disappears. 我要达到的目的是,当牛仔被点击时,他消失了。 I have tried it with a trace comment as well and nothing shows when the cowboy is clicked. 我也尝试过使用痕迹注释,单击牛仔时没有任何显示。

When i run the program i get no errors. 当我运行程序时,我没有任何错误。 I can't figure out why. 我不知道为什么。 Here's my code 这是我的代码

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.display.Bitmap;
    import flash.ui.Mouse;
    import flash.events.MouseEvent;

    /**
     * ...
     * @author Callum Singh
     */
    public class Main extends Sprite 
    {
        public var gun:crosshair;
        public var cowboy:enemy;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            gun = new crosshair();
            stage.addChild (gun);
            addEventListener(Event.ENTER_FRAME, moveCrosshair);

            cowboy = new enemy();
            cowboy.x = Math.random() * 600;
            cowboy.y = Math.random() * 400;
            stage.addChild (cowboy);
            addEventListener(MouseEvent.CLICK, handleShoot);
        }

        private function moveCrosshair(e:Event):void
        {
            gun.x = mouseX -120;
            gun.y = mouseY -115;

        }

        private function handleShoot(e:MouseEvent):void
        {
            if (e.target == cowboy)
                {
                    cowboy.visible = false;
                }
        }

    }

}

Make sure you add the listener to the cowboy object, and sometimes changing MouseEvent.CLICK to MouseEvent. 确保将侦听器添加到牛仔对象,有时将MouseEvent.CLICK更改为MouseEvent。 MOUSE_DOWN helps. MOUSE_DOWN有帮助。

cowboy.addEventListener(MouseEvent.MOUSE_DOWN, handleShoot);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM