将对象添加到另一个sprite层会导致问题

时间:2013-05-24 07:25:29

标签: actionscript-3 flash

基本上,产卵船出现在十字准线上方,而我希望它是另一种方式。我尝试将十字准线添加到另一层但是然后点击/“射击”船只什么也没做。有什么想法吗?

     public class Main extends MovieClip {

        public static var backgroundLayer:Sprite = new Sprite();
        public static var gameLayer:Sprite = new Sprite();
        public static var interfaceLayer:Sprite = new Sprite();
        public static var menuLayer:Sprite = new Sprite();

        public var mainMenu:menuMain = new menuMain();
        public var intro:IntroSound = new IntroSound();
        public var soundControl:SoundChannel = new SoundChannel();

        public var crosshair:crosshair_mc;

        static var enemyArray:Array = [];
        private var enemyShipTimer:Timer;
        private var enemyShipTimerMed:Timer;
        private var enemyShipTimerSmall:Timer;

        public function Main()
        {
            addMenuListeners();
            addChild(gameLayer);
            addChild(backgroundLayer);
            addChild(interfaceLayer);
            addChild(menuLayer);

            menuLayer.addChild(mainMenu);
            interfaceLayer.addChild(howtoPlay);
            interfaceLayer.addChild(gameEnd);
            interfaceLayer.addChild(gameAbout);
            soundControl = intro.play(0, 100);


            stage.addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
        }

        private function update(e:Event):void
        {
            for each (var enemy:EnemyShip in enemyArray)
            {
                enemy.update();

                if (enemy.dead)
                {
                    enemy.kill();
                }
            }
        }

        private function mouseDown(e:MouseEvent):void
        {
            if (e.target.name) {
                switch (e.target.name) {
                    case "enemy_big":
                        updateScore(5);
                        e.target.parent.damage();
                        break;
                    case "enemy_medium":
                        updateScore(10);
                        e.target.parent.damage();
                        break;
                    case "enemy_small":
                        updateScore(15);
                        e.target.parent.damage();
                        break;
                }
            }
        }

        function addMenuListeners():void
        {
            //Code to add event listeners
        }


        public function startGame(e:Event)
        {
            removeMenuListeners();
            soundControl.stop();

            if (howtoPlay.parent == interfaceLayer)
            {
                interfaceLayer.removeChild(howtoPlay);
            }

            if (gameAbout.parent == interfaceLayer)
            {
                interfaceLayer.removeChild(gameAbout);
            }

            if (gameEnd.parent == interfaceLayer)
            {
                interfaceLayer.removeChild(gameEnd);
            }

            if (mainMenu.parent == menuLayer)
            {
                menuLayer.removeChild(mainMenu);
            }

            enemyShipTimer = new Timer(2000);
            enemyShipTimer.addEventListener("timer", sendEnemy);
            enemyShipTimer.start();

            enemyShipTimerMed = new Timer(2500);
            enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
            enemyShipTimerMed.start();

            enemyShipTimerSmall = new Timer(2750);
            enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
            enemyShipTimerSmall.start();

            crosshair = new crosshair_mc();
            gameLayer.addChild(crosshair);
            crosshair.mouseEnabled = crosshair.mouseChildren = false;
            Mouse.hide();
            gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);

            resetScore();
        }

        function spawnEnemy(type:String, speed:Number) {
            var enemy = new EnemyShip(type, speed);
            enemyArray.push(enemy);
            gameLayer.addChild(enemy);
            return enemy;
        }

        function sendEnemy(e:TimerEvent):void
        {
            Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
            spawnEnemy("big", Math.random() * 5 + 12);
        }

        function sendEnemyMed(e:TimerEvent):void
        {
            Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
            spawnEnemy("medium", Math.random() * 7 + 14);
        }

        function sendEnemySmall(e:TimerEvent):void
        {
            Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
            spawnEnemy("small", Math.random() * 9 + 16);
        }

        static function updateScore(points)
        {
            score += points;
            scoreText.text = String(score);
            scoreHeader.setTextFormat(scoreFormat);
            scoreText.setTextFormat(scoreFormat);
        }

        static function resetScore()
        {
            score = 0;
            scoreText.text = String(score);
            scoreText.setTextFormat(scoreFormat);
        }

        static function removeEnemy(enemyShip:EnemyShip):void {
            enemyArray.splice(enemyArray.indexOf(enemyShip), 1);
            gameLayer.removeChild(enemyShip);
        }


        function moveCursor(event:Event)
        {
            crosshair.x=mouseX;
            crosshair.y=mouseY;
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

我希望MouseDowns被父图层拦截。您已将mouseEnabled=false用于十字准线,现在您应该为其父级执行此操作。

public static var crosshairLayer:Sprite=new Sprite();
public function Main() {
    ... // initialize everything first
    addChild(gameLayer);
    addChild(crosshairLayer);
    crosshairLayer.mouseEnabled=false;
    crosshairLayer.mouseChildren=false;
}

然后将十字准线添加到crosshairLayer