【问题标题】:Removing the box on mouse click in as3在 as3 中单击鼠标时删除框
【发布时间】:2012-01-05 18:59:12
【问题描述】:

我正在尝试删除我在屏幕上创建的框。我有一个框导出为 作为播放器导出的盒子和一艘船,它们都是电影剪辑。这是代码:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends MovieClip {

        //playermovevar
        private var _player:MovieClip;
        private var _playerSpeed:Number=5;
        private var _destinationX:int;
        private var _destinationY:int;



        //boxaddvar
        private var boxAmount:Number=0;
        private var boxLimit:Number=20;
        private var _root:Object;

        public function Main() {
            createPlayer();

            //playermovlisten
            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandlerdown);

            //boxaddlisten
            addEventListener(Event.ENTER_FRAME, eFrame);

            _box.addEventListener(MouseEvent.CLICK, boxclick);


        }

        //playermoving
        private function createPlayer():void {
            _destinationX=stage.stageWidth/2;
            _destinationY=stage.stageHeight/2;

            _player = new Player();
            _player.x=stage.stageWidth/2;
            _player.y=stage.stageHeight/2;
            stage.addChild(_player);
        }


        private function enterFrameHandler(event:Event):void {
            _player.x += (_destinationX - _player.x) / _playerSpeed;
            _player.y += (_destinationY - _player.y) / _playerSpeed;
        }


        private function mouseHandlerdown(event:MouseEvent):void {
            _destinationX=event.stageX;
            _destinationY=event.stageY;
            addEventListener(MouseEvent.MOUSE_UP, mouseHandlerup);

            rotatePlayer();
        }
        private function mouseHandlerup(event:MouseEvent):void {

        }

        private function rotatePlayer():void {
            var radians:Number=Math.atan2(_destinationY-_player.y,_destinationX-_player.x);
            var degrees:Number = radians / (Math.PI / 180) + 90;
            _player.rotation=degrees;
        }
        //boxadding
        private function eFrame(event:Event):void {
            if (boxAmount<=boxLimit) {
                boxAmount++;

                var _box:Box=new Box  ;

                _box.y=Math.random()*stage.stageHeight;

                _box.x=Math.random()*stage.stageWidth;

                addChild(_box);


            } else if (boxAmount >= boxLimit) {
                removeEventListener(Event.ENTER_FRAME, eFrame);
            } else {
                addEventListener(Event.ENTER_FRAME, eFrame);
            }
        }
        function boxclick(event:MouseEvent):void {
            removeChild(_box);
        }
    }

它给了我这个错误:

1120:访问未定义的属性 _box。 _box.addEventListener(MouseEvent.CLICK, boxclick);

1120:访问未定义的属性 _box。 removeChild(_box);

有人知道怎么回事吗?

谢谢

【问题讨论】:

    标签: actionscript-3 flash-cs4


    【解决方案1】:

    不确定您想要什么结果,但可以试试这个并随时提出任何问题。

    package {
        import flash.display.MovieClip;
        import flash.events.Event;
        import flash.events.MouseEvent;
    
        public class Main extends MovieClip {
    
            //playermovevar
            private var _player:Player;
            private var _playerSpeed:Number=5;
            private var _destinationX:int;
            private var _destinationY:int;
    
    
    
            //boxaddvar
            private var boxAmount:Number=0;
            private var boxLimit:Number=20;
            private var _root:Object;
    
            private var _box:Box;
            private var arrayOfBoxes:Array = new Array(boxLimit);
    
            public function Main() {
                createPlayer();
    
                //playermovlisten
                stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
                stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandlerdown, false, 0, true);
    
                //boxaddlisten
                addEventListener(Event.ENTER_FRAME, eFrame, false, 0, true);
    
    
    
    
            }
    
            //playermoving
            private function createPlayer():void {
                _destinationX=stage.stageWidth/2;
                _destinationY=stage.stageHeight/2;
    
                _player = new Player();
                _player.x=stage.stageWidth/2;
                _player.y=stage.stageHeight/2;
                stage.addChild(_player);
            }
    
    
            private function enterFrameHandler(event:Event):void {
                _player.x += (_destinationX - _player.x) / _playerSpeed;
                _player.y += (_destinationY - _player.y) / _playerSpeed;
            }
    
    
            private function mouseHandlerdown(event:MouseEvent):void {
                _destinationX=event.stageX;
                _destinationY=event.stageY;
                addEventListener(MouseEvent.MOUSE_UP, mouseHandlerup, false, 0, true);
    
                rotatePlayer();
            }
            private function mouseHandlerup(event:MouseEvent):void {
    
            }
    
            private function rotatePlayer():void {
                var radians:Number=Math.atan2(_destinationY-_player.y,_destinationX-_player.x);
                var degrees:Number = radians / (Math.PI / 180) + 90;
                _player.rotation=degrees;
            }
            //boxadding
            private function eFrame(event:Event):void {
                if (boxAmount<=boxLimit) {
                    boxAmount++;
    
                    _box =new Box();
    
                    _box.y=Math.random()*stage.stageHeight;
    
                    _box.x=Math.random()*stage.stageWidth;
    
                    _box.addEventListener(MouseEvent.CLICK, boxclick, false, 0, true);
    
                    arrayOfBoxes.push(_box);
    
                    addChild(_box);
    
    
                } else if (boxAmount >= boxLimit) {
                    removeEventListener(Event.ENTER_FRAME, eFrame);
                } else {
                    addEventListener(Event.ENTER_FRAME, eFrame, false, 0, true);
                }
            }
            function boxclick(evt:MouseEvent):void {
                removeChild(evt.currentTarget as Box);
            }
        }
    }
    

    【讨论】:

    • 效果很好,但只有两件事 (1) 在我单击另一个框之前我必须单击其他地方。 (2) (我想我忘了说这个)当我点击这个框时,它需要重新出现在舞台上的其他地方。谢谢,虽然它帮助了很多
    • 在这种情况下,不要移除盒子,只需移动它。 function boxclick(evt:MouseEvent):void { //removeChild(evt.currentTarget as Box); _box = evt.currentTarget as Box; _box.y=Math.random()*stage.stageHeight; _box.x=Math.random()*stage.stageWidth; }
    • 我想这行得通,但我宁愿它删除并重新添加到其他地方而不是移动它
    【解决方案2】:
    1. 您忘记关闭包的}

    2. 您的 _box 变量不作为全局属性存在,但您正尝试全局访问它。

    :)

    [编辑]

    只需从顶部删除这一行

    _box.addEventListener(MouseEvent.CLICK, boxclick);
    

    放在后面

    var _box:Box=new Box  ;
    

    然后将boxclick函数改为:

    function boxclick(event:MouseEvent):void {
        var _box:Box = event.currentTarget as Box; // here is your box again
    
        // make with _box what you want here. Like,
    
        removeChild(_box);
    }
    

    【讨论】:

    • 我如何使它成为全局并且仍然让它引用框
    • 看看我的帖子。在您的情况下,无需将其设为全局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多