我正在制作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.
答案 0 :(得分:2)
尝试
removeChild(event.currentTarget as DisplayObject)
removeChild(_firstCard as DisplayObject)
删除_firstCard会引发错误,因为您将var设置为undefined。除去
_firstCard = undefined;
或将此行移到removeChild-lines下面。
如果您想要延迟,请尝试:
function removeCards(firstCard:DisplayObect, secondCard:DisplayObject):void{
removeChild(firstCard);
removeChild(secondCard);
}
并在您的事件监听器调用中
setTimeout(removeCards, 1000, _firstCard as DisplayObject, event.currentTarget as DisplayObject);
并删除removeChild行
答案 1 :(得分:0)
我想你想做:
removeChild(event.currentTarget);
另外,我认为您需要这样做:
removeChild(_firstCard);