动作脚本。使用“重试”按钮删除所有对象

时间:2013-05-05 16:20:15

标签: actionscript-3 flash actionscript parent-child removechild

我正在制作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);
   }
}

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

1 个答案:

答案 0 :(得分:0)

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

CardContainer.addChild(_card);

当您想删除所有卡片时,请移除CardContainer MovieClip并重新添加到舞台上。或者您可以在removeCards功能中执行此操作以删除所有卡片,如:

function removeCards(e:MouseEvent):void
{
   while(CardContainer.numChildren > 0)
   {
       CardContainer.removeChildAt(0);
   }
}