0

我正在创建 Flash“记忆”游戏,想发现 2 张相等的卡片。一切都很好,只是我需要这样做,当发现卡片时,它们将被删除。现在它保持显示。

当我尝试: removeChild(_card.currentTarget._type);

我收到错误: C:\...\MemoryGame.as, Line 202 1119: Access of possibly undefined property currentTarget through a reference with static type Card.

以下是部分代码:

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

private function checkCards(event:MouseEvent):void
{

    event.currentTarget.removeEventListener(MouseEvent.CLICK, checkCards);

    if(_firstCard == undefined)
    {
        _firstCard = event.currentTarget;
    }
    else if(String(_firstCard._type) == String(event.currentTarget._type))
    {
        trace("Match");
        _message = "Match";
        message_txt.text = _message;
        _firstCard = undefined;
        _currentMatches ++;
        removeChild(_card.currentTarget._type);

你可以帮帮我吗?

编辑:

当我使用:removeChild(_firstCard)

我收到错误:TypeError:

Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/removeChild() at MemoryGame/checkCards()[C:\Users\Rimante\Desktop\gerase\gerase\MemoryGame.as:218]`

当我使用:removeChild(event.currentTarget);

我收到错误:

C:\Users\Rimante\Desktop\gerase\gerase\MemoryGame.as, Line 217  1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
4

2 回答 2

2

尝试

 removeChild(event.currentTarget as DisplayObject)
 removeChild(_firstCard as DisplayObject)

删除 _firstCard 会引发错误,因为您将 var 设置为未定义。消除

 _firstCard = undefined;

或将此行移到 removeChild 行下方。

如果您想延迟尝试:

function removeCards(firstCard:DisplayObect, secondCard:DisplayObject):void{
    removeChild(firstCard);
    removeChild(secondCard);
}

在你的事件监听器调用中

setTimeout(removeCards, 1000, _firstCard as DisplayObject, event.currentTarget as DisplayObject);

并删除 removeChild 行

于 2013-05-04T08:41:40.190 回答
0

我想你想做:

removeChild(event.currentTarget);

另外,我认为您需要这样做:

removeChild(_firstCard);
于 2013-05-04T08:52:02.360 回答