Question

I'm creating Flash "memory" game, Idea to discover 2 equal cards. Everything is fine, just I need to make that when cards are discovered they will be deleted. Now It stay shown.

When I try: removeChild(_card.currentTarget._type);

I receive error: C:\...\MemoryGame.as, Line 202 1119: Access of possibly undefined property currentTarget through a reference with static type Card.

Here is part of code:

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

Could you help me?

Edit:

When I use: removeChild(_firstCard)

I receive error: 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]`

When I use: removeChild(event.currentTarget);

I receive error:

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.
Was it helpful?

Solution

Try

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

The removal of _firstCard throws an error because you set the var to undefined. remove

 _firstCard = undefined;

or move this line below the removeChild-lines.

If you want a delay try:

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

and within you event listener call

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

and remove the removeChild lines

OTHER TIPS

I think you wanted to do:

removeChild(event.currentTarget);

Also, I think you would need to do:

removeChild(_firstCard);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top