0

我正在创建 Flash“记忆”游戏,想发现 2 张相等的卡片。我需要制作“再试一次”按钮,删除所有卡片并生成新卡片。

这是主要的游戏功能:

public function MemoryGame()
{
    tryAgain.addEventListener(MouseEvent.CLICK, darKarta);
        timer = new Timer(1000); //create a new timer that ticks every second.
        timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick

        txtTime = new TextField();
        addChild(txtTime);
        tmpTime = timer.currentCount;
        timer.start();

    _cards = new Array();
    _totalMatches = 18;
    _currentMatches = 0;
    createCards();
}

这是 createCards 函数:

private function createCards():void
        {
            _cardX = 45;
            _cardY = 10;

            for(var i:Number = 0; i < 2; i++)
            {
                _card = new Card();
                addChild(_card);
                _boarder = new Boarder();
                _card.setType(_boarder);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 5;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            for(var j:Number = 0; j < 2; j++)
            {
                _card = new Card();
                addChild(_card);
                _blueBoard = new BlueBoard();
                _card.setType(_blueBoard);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 5;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }
                        for(var r:Number = 0; r < 2; r++)
            {
                _card = new Card();
                addChild(_card);
                _penktas = new Penktas();
                _card.setType(_penktas);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 5;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

这是 removeCard 功能:

function removeCards(e:MouseEvent):void
{
    //for(var iv:Number = 0; iv < 18; iv++)
{
    addEventListener(Event.ENTER_FRAME, remCards);
    //}
}

function remCards(evt:Event):void
{
   if (contains(_card)) {
    removeChild(evt.currentTarget._card);
   }
}

但它只删除最后一张卡,我不知道如何将它们全部删除。你有什么想法吗?谢谢你。

4

1 回答 1

0

一种方法是将所有卡片添加到容器内MovieClip,而不是直接添加到舞台上。

CardContainer.addChild(_card);

当您想要移除所有卡片时,请移除 CardContainer MovieClip 并将其重新添加到舞台上。或者您可以在您的removeCards函数中执行此操作以从中删除所有卡片,例如:

function removeCards(e:MouseEvent):void
{
   while(CardContainer.numChildren > 0)
   {
       CardContainer.removeChildAt(0);
   }
}
于 2013-05-05T16:37:46.400 回答