gotoandstop问题动作3

时间:2012-03-20 01:43:23

标签: actionscript-3

我有一个记忆游戏程序,当计时器用完时,我希望它转到第3帧,显示“游戏失败”页面。

我已经完成了所有设置,除非游戏时间不足,框架看起来与原始框架重叠,而不是进入完全独立的页面。

任何人都可以帮助我吗?

这是我的代码:

package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.text.TextField;

public class MemoryGame extends MovieClip{

    private var firstTile:cards;
    private var secondTile:cards;
    private var pauseTimer:Timer;
    private var score:int;
    private var cardCount:int;
    var seconds:Number;
    var minutes:Number;


    var numberDeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6);


    public function MemoryGame(){

        //TIMER FUNCTION
        var levelTimer:Timer = new Timer(1000, 180);
        levelTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
        levelTimer.addEventListener(TimerEvent.TIMER, timerHandler);

        // LEVEL FUNCTION
        easyBtn.addEventListener(MouseEvent.CLICK, easyButtonClicked);
        medBtn.addEventListener(MouseEvent.CLICK, medButtonClicked);
        hardBtn.addEventListener(MouseEvent.CLICK, hardButtonClicked);

        score = 0;
        txtScore.text=""+score;

        //Level button events
        function easyButtonClicked(e:MouseEvent):void{
            removeChild(levelText);
            trace("easy button clicked!");
            seconds = 0;
            minutes = 1;
            txtTime.text = "1:00";
            levelTimer.start();
            setupTiles();
        }

        function medButtonClicked(e:MouseEvent):void{
            removeChild(levelText);
            trace("medium button clicked!");
            seconds = 30;
            minutes = 0;
            txtTime.text = "0:30";
            levelTimer.start();
            setupTiles();
        }

        function hardButtonClicked(e:MouseEvent):void{
            removeChild(levelText);
            trace("hard button clicked!");
            seconds = 15;
            minutes = 0;
            txtTime.text = "0:15";
            levelTimer.start();
            setupTiles();
        }


        //Timer handlers
        function timerHandler(e:TimerEvent):void {
            if (seconds > 00) {
            seconds -=1;
            }

            else {
                if (minutes > 0) {minutes -=1;seconds = 59;}
        }
                txtTime.text = minutes+":"+(seconds >= 10 ? seconds : "0"+seconds);
            }

        function timerCompleteHandler(e:TimerEvent):void {
            e.target.reset();
            e.target.stop();
            trace("game over!");
        }



        //Tiles set up
        function setupTiles(){
        for(x=1; x<=4; x++) {
            for (y=1; y<=3; y++){
                var randomCard = Math.floor(Math.random()*numberDeck.length);
                var tile:cards = new cards();
                tile.card = numberDeck[randomCard];
                numberDeck.splice(randomCard,1);
                tile.gotoAndStop(9);
                tile.x = (x-1) * 150;
                tile.y = (y-1) * 200;
                tile.addEventListener(MouseEvent.CLICK,tileClicked);
                addChild(tile);
                cardCount = cardCount + 1
            }
        }
    }
    }




    public function tileClicked(event:MouseEvent) {
        var clicked:cards = (event.currentTarget as cards);
        if (firstTile == null){
            firstTile = clicked;
            firstTile.gotoAndStop(clicked.card);
        }
        else if (secondTile == null && firstTile != clicked){
            secondTile = clicked;
            secondTile.gotoAndStop(clicked.card);
            if (firstTile.card == secondTile.card){
                pauseTimer = new Timer(1000, 1);
                pauseTimer.addEventListener(TimerEvent.TIMER_COMPLETE,removeCards);
                pauseTimer.start();

            }
            else {
                pauseTimer = new Timer(1000, 1);
                pauseTimer.addEventListener(TimerEvent.TIMER_COMPLETE,resetCards);
                pauseTimer.start();
            }
        }

        if (seconds == 0){
            this.gotoAndStop(2);
            pauseTimer.stop();
            //levelTimer.stop();
        }
    }


    public function resetCards(event:TimerEvent) {
        firstTile.gotoAndStop(9);
        secondTile.gotoAndStop(9);
        firstTile = null;
        secondTile = null;
        pauseTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,resetCards);
        score = score - 2;
        txtScore.text=""+score;
    }


    public function removeCards(event:TimerEvent){
        removeChild(firstTile);
        removeChild(secondTile);
        firstTile = null;
        secondTile = null;
        pauseTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,removeCards);
        score = score + 10;
        txtScore.text=""+score;
        cardCount = cardCount - 2;
        trace("Cardcount: " + cardCount);

        if (cardCount == 0){
            this.gotoAndStop(2);
            txtFinalScore.text=" "+score;
            pauseTimer.stop();          
            }
                }
}   
}

非常感谢你!

1 个答案:

答案 0 :(得分:0)

使用addChild(object)添加对象时,它与时间轴上的关键帧无关。

所以你需要做的不是跳到第2帧,removeChild(object)或object.visible = false所有你不想要的孩子,并且addChild(object)你的'不合时宜'资产。

良好的职业道德是创建destroy()函数,删除和清空任何不需要的资产。这样您就可以轻松删除不需要的项目并释放内存。