Action Script. Remove all objects with Try Again button

 ̄綄美尐妖づ 提交于 2019-12-11 18:59:46

问题


I'm creating Flash "memory" game, Idea to discover 2 equal cards. And I need to make "Try again" button, which remove all cards and spawn new.

Here is main game function:

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

Here is createCards function:

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

And here is removeCard function:

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

But It removes only last card, I don't know how to make to delete them all. Have you any ideas? Thank you.


回答1:


One way would be to add all the cards inside a container MovieClip and not directly to the stage.

CardContainer.addChild(_card);

and when you want to remove all the cards then either remove the CardContainer MovieClip and re add it to the stage. or you can do this in your removeCards function to remove all cards from it like :

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


来源:https://stackoverflow.com/questions/16386572/action-script-remove-all-objects-with-try-again-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!