在鼠标中单击as3删除该框

时间:2012-01-05 18:59:13

标签: actionscript-3 flash-cs4

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

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

谁知道什么是错的?

谢谢

2 个答案:

答案 0 :(得分:0)

  1. 您忘记关闭包裹的}

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

  3. :)

    <强> [编辑]

    只需从顶部删除此行

    即可
    _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);
    }
    

答案 1 :(得分:0)

不确定您想要的结果,但尝试这一点并随时提出任何问题。

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