操作脚本。删除与重试按钮中的所有对象重试、脚本、按钮、对象

2023-09-08 11:50:06 作者:别动︶我要耍流氓

我创建Flash记忆的游戏,理念,发现2等于卡。而我需要再试一次按钮,删除所有的卡和产生新的。

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

下面是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);
            }

这里是removeCard功能:

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.

推荐答案

一种方法是将一个容器内添加的所有卡影片剪辑,而不是直接在舞台上。

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

CardContainer.addChild(_card);

,当你要删除所有的牌,然后要么删除CardContainer影片剪辑,并重新将其添加到舞台。或者你可以做到这一点在你的 removeCards 函数从它删除所有的牌,如:

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);
   }
}
 
精彩推荐
图片推荐