1

This is a memory match game that I need a "restart" button. everything works perfeclty in the game, even the restart button. My problem lies when after the user has found a match (and the array of cards changes), the command im using to restart the game only removes the cards till it "hits" one that is gone already, and stops removing them....

this is the code I wrote for the button

btnRestart.addEventListener(MouseEvent.CLICK, comecaOtraveh);
 function comecaOtraveh(e:MouseEvent):void
 {
    for each(var mc:MovieClip in virarCartas) 
    {                                                       
        if (mc != null)
        {
        mc.parent.removeEventListener(MouseEvent.CLICK, cartaClicada);   

        mc.parent.removeChild(mc);   // Cannot access a property or method of a null object           reference.
        //this.removeChild(mc);      // The supplied DisplayObject must be a child of the caller.
        //removeChild(mc);           // The supplied DisplayObject must be a child of the caller.
        }
    }

    clearInterval(intervalo);   

    myTimer.stop();
    myTimer.removeEventListener(TimerEvent.TIMER, iniciaTempo);

    channel.stop();
    channel.removeEventListener(Event.SOUND_COMPLETE, onComplete);

    removeChild(mcContagem);

    gotoAndStop(1)
}

I tried all //'s option, and tried without the IF statement aswell.... none worked...

Heres the code that puts my cards into the table, shuffles them, shows them to the user than turns them around so the game can start.

for (var i: int = 0; i < QUANT_CARTAS; i++)
{
    cartas.push(i);
}
for (var moeda: int = QUANT_CARTAS - 1; moeda > 0; moeda--)
{
    var pos: int = Math.floor(Math.random() * moeda);
    var carta: int = cartas[moeda];
    cartas[moeda] = cartas[pos];
    cartas[pos] = carta;
}
for (i = 0; i < QUANT_CARTAS; i++)
{
    var novaCarta: Carta = new Carta();
    novaCarta.tipoCarta = cartas[i];
    novaCarta.x = 5 + (novaCarta.width + 2.7) * (i % CARTAS_POR_LINHA);
    novaCarta.y = 5 + (novaCarta.height + 2.7) * (Math.floor(i / CARTAS_POR_LINHA));
    novaCarta.gotoAndStop(cartas[i] + 1); // face pra cima
    novaCarta.buttonMode = true;
    novaCarta.addEventListener(MouseEvent.CLICK, cartaClicada);
    addChild(novaCarta);
    virarCartas.push(novaCarta);
    if (i == 35)
    {
    podeJogar = false;
    mcContagem.x = 884;
    mcContagem.y = 511;
    addChild(mcContagem);
    intervalo = setInterval(desviraCartas, 3000);
    function desviraCartas()
    {
        for (var j: int = 0; j < QUANT_CARTAS; j++)
        {
            virarCartas[j].gotoAndStop(QUANT_CARTAS + 1);
            if (j == 35)
            {
                clearInterval(intervalo);
                iniciaJogo();
                playSound();
            }
            }
        }
    }
 }

and here is the code that I use for the mouseclicks

function cartaClicada(e: MouseEvent)
{
    if (podeJogar == true)
    {
    SOM_memoClick.play();

    var carta: Carta = e.currentTarget as Carta;

    if (cartasColetadas.indexOf(carta) == -1)
    {
        cartasColetadas.push(carta);
        carta.gotoAndStop(carta.tipoCarta + 1);
    }

    if (cartasColetadas.length == 3) //APOS 3 CARTAS CLICADAS
    {
        podeJogar = false;
        var carta3: int = cartasColetadas[2].tipoCarta
        var carta2: int = cartasColetadas[1].tipoCarta
        var carta1: int = cartasColetadas[0].tipoCarta

        //0,1,2
        if (((carta1 == 0) || (carta1 == 1) || (carta1 == 2)) &&
            ((carta2 == 0) || (carta2 == 1) || (carta2 == 2)) &&
            ((carta3 == 0) || (carta3 == 1) || (carta3 == 2)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(amarelo, intervaloFim);
        }
        //3,4,5
        else if (((carta1 == 3) || (carta1 == 4) || (carta1 == 5)) &&
            ((carta2 == 3) || (carta2 == 4) || (carta2 == 5)) &&
            ((carta3 == 3) || (carta3 == 4) || (carta3 == 5)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(rosa, intervaloFim);
        }
        //6,7,8
        else if (((carta1 == 6) || (carta1 == 7) || (carta1 == 8)) &&
            ((carta2 == 6) || (carta2 == 7) || (carta2 == 8)) &&
            ((carta3 == 6) || (carta3 == 7) || (carta3 == 8)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(celeste, intervaloFim);
        }
        //9,10,11
        else if (((carta1 == 9) || (carta1 == 10) || (carta1 == 11)) &&
            ((carta2 == 9) || (carta2 == 10) || (carta2 == 11)) &&
            ((carta3 == 9) || (carta3 == 10) || (carta3 == 11)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(branco, intervaloFim);
        }
        //12,13,14
        else if (((carta1 == 12) || (carta1 == 13) || (carta1 == 14)) &&
            ((carta2 == 12) || (carta2 == 13) || (carta2 == 14)) &&
            ((carta3 == 12) || (carta3 == 13) || (carta3 == 14)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(prata, intervaloFim);
        }
        //15,16,17
        else if (((carta1 == 15) || (carta1 == 16) || (carta1 == 17)) &&
            ((carta2 == 15) || (carta2 == 16) || (carta2 == 17)) &&
            ((carta3 == 15) || (carta3 == 16) || (carta3 == 17)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(verde, intervaloFim);
        }
        //18,19,20
        else if (((carta1 == 18) || (carta1 == 19) || (carta1 == 20)) &&
            ((carta2 == 18) || (carta2 == 19) || (carta2 == 20)) &&
            ((carta3 == 18) || (carta3 == 19) || (carta3 == 20)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(preto, intervaloFim);
        }
        //21,22,23
        else if (((carta1 == 21) || (carta1 == 22) || (carta1 == 23)) &&
            ((carta2 == 21) || (carta2 == 22) || (carta2 == 23)) &&
            ((carta3 == 21) || (carta3 == 22) || (carta3 == 23)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(violeta, intervaloFim);
        }
        //24,25,26
        else if (((carta1 == 24) || (carta1 == 25) || (carta1 == 26)) &&
            ((carta2 == 24) || (carta2 == 25) || (carta2 == 26)) &&
            ((carta3 == 24) || (carta3 == 25) || (carta3 == 26)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(azul, intervaloFim);
        }
        //27,28,29
        else if (((carta1 == 27) || (carta1 == 28) || (carta1 == 29)) &&
            ((carta2 == 27) || (carta2 == 28) || (carta2 == 29)) &&
            ((carta3 == 27) || (carta3 == 28) || (carta3 == 29)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(dourado, intervaloFim);
        }
        //30,31,32
        else if (((carta1 == 30) || (carta1 == 31) || (carta1 == 32)) &&
            ((carta2 == 30) || (carta2 == 31) || (carta2 == 32)) &&
            ((carta3 == 30) || (carta3 == 31) || (carta3 == 32)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(marrom, intervaloFim);
        }
        //33,34,35
        else if (((carta1 == 33) || (carta1 == 34) || (carta1 == 35)) &&
            ((carta2 == 33) || (carta2 == 34) || (carta2 == 35)) &&
            ((carta3 == 33) || (carta3 == 34) || (carta3 == 35)))
        {
            SOM_trioInicio.play();
            intervalo = setInterval(vermelho, intervaloFim);
        }
        else
        {
            intervalo = setInterval(resetaCartas, 1150);
        }
    }
}
}

now, during the game, thats how I remove them (lets say I found the 0, 1, 2, cards, that called the function "amarelo")

function amarelo(): void
{
    clearInterval(intervalo);

addChild(mcExplic);
mcExplic.gotoAndStop(1);
mcExplic.y = 384;
mcExplic.x = 512;

addChild(mcBotaoOk);
mcBotaoOk.y = 370;
mcBotaoOk.x = 512;
mcBotaoOk.visible = false;

cartasColetadas[0].removeEventListener(MouseEvent.CLICK, cartaClicada);
cartasColetadas[1].removeEventListener(MouseEvent.CLICK, cartaClicada);
cartasColetadas[2].removeEventListener(MouseEvent.CLICK, cartaClicada);

removeChild(cartasColetadas[0]);
removeChild(cartasColetadas[1]);
removeChild(cartasColetadas[2]);

cartasColetadas = new Array();
podeJogarTrio = true;
cursorTrio = true;
veramarelo();
mouseTipo();
}

so... any thought on how to make that reset button work??? (1st block of code posted)

my variables and etc...

const QUANT_CARTAS: int = 36;
const CARTAS_POR_LINHA: int = 6;

var alturaTrio: int = -180;
var larguraTrio: int = -300;

var intervaloTrio: int = 500;
var intervaloFim: int = 800;
var intervalo: int;

var gameScore: int;
var pontoErro: int;
var pontoAcerto: int;

var erros: int = 0;
var acertos: int = 0;

var musicaOn: Boolean = true;
var podeJogar: Boolean = true;
var podeJogarTrio: Boolean = false;
var cursorTrio: Boolean = false;

var mcExplic: Explic = new Explic();
var mcContagem: Contagem = new Contagem;
var mcBotaoOk: BotaoOk = new BotaoOk;
var mcTrans: Transicao = new Transicao();

var virarCartas: Array = new Array();
var trio: Array = new Array(); 
var cartas: Array = new Array();
var cartasColetadas: Array = new Array();
Surtarso
  • 37
  • 6

1 Answers1

0

The error that you got is because the object is removed from stage but not from the virarCartas array, take a look on this example :

var cards:Array = new Array()

// we create our cards and add it to cards array
for(var i=0; i < 6; i++){

    var card:Card = new Card()
        card.x = 50*i
        // we set and index to our card to identify it when we need to remove it from the cards array
        card.index = i      
        card.addEventListener(MouseEvent.CLICK, card_on_Press)  

    addChild(card)
    cards.push(card)

}

function card_on_Press(e:MouseEvent){

    var this_card = MovieClip(e.currentTarget)
        this_card.removeEventListener(MouseEvent.CLICK, card_on_Press)
        this_card.parent.removeChild(this_card)

    // remove our card = set it as null
    cards[this_card.index] = null

}

var btn_remove_all:Btn = new Btn()
    btn_remove_all.x = 10
    btn_remove_all.y = 80
    btn_remove_all.addEventListener(
        MouseEvent.CLICK, 
        function(e:MouseEvent){
            for each(var card:MovieClip in cards) { 
                if (card != null){
                    card.removeEventListener(MouseEvent.CLICK, card_on_Press)
                    card.parent.removeChild(card)
                    card = null
                }
            }
            cards = null
        }
    )
    addChild(btn_remove_all)

Hope that can help you.

Edit :

To simplify things, you can add an index to your card ( as I did in my answer ) and then when you want remove it, you have just to do virarCartas[card.index] = null :

var novaCarta: Carta = new Carta();

// we add an index, it's the virarCartas array length, of course you should add it to your Carta class
novaCarta.index = virarCartas.length;

novaCarta.tipoCarta = cartas[i];
novaCarta.x = 5 + (novaCarta.width + 2.7) * (i % CARTAS_POR_LINHA);
novaCarta.y = 5 + (novaCarta.height + 2.7) * (Math.floor(i / CARTAS_POR_LINHA));
novaCarta.gotoAndStop(cartas[i] + 1); // face pra cima
novaCarta.buttonMode = true;
novaCarta.addEventListener(MouseEvent.CLICK, cartaClicada);
addChild(novaCarta);
virarCartas.push(novaCarta);

And edit your amarelo function like this :

function amarelo(): void 
{

    ...

    removeChild(cartasColetadas[0]);
    removeChild(cartasColetadas[1]);
    removeChild(cartasColetadas[2]);

    virarCartas[cartasColetadas[0].index] = null;
    virarCartas[cartasColetadas[1].index] = null;
    virarCartas[cartasColetadas[2].index] = null;

    ...

}
akmozo
  • 9,829
  • 3
  • 28
  • 44
  • i've been trying to figure it out trhu your code for a week now... still havent solved it! pretty much the last coding needed to finish my fist game! the problem is that there is no button to remove single cards, they are removed after 3 are hit and only if the 3 of them are the same... Im editing the question w/ more code.... – Surtarso Dec 14 '14 at 19:24
  • @Surtarso We will do easy, could you put the code that remove the 3 cards ? – akmozo Dec 14 '14 at 19:27
  • @Surtarso I see your edit, but I have a question : is `cartasColetadas` the same array as `cards` in my code ( the array where you save your cards ) ? – akmozo Dec 14 '14 at 19:41
  • 1st I add "cartas", the array with all 36 cards, in order. than I shuffle them, and push them into "virarCartas" so they can show their frame number related to their shuffled array number. that array gets cleared after the game starts. after that, "cartasColetadas" is an array of 3, that each mouse click adds one item, and if they are not 0,1,2 etc (in order) it also gets reset. – Surtarso Dec 14 '14 at 19:54
  • the amarelo function removes them correctly =)) the problem is the reset button! or are you telling me that if I index them and remove the index on every 3-match, the reset button will work the way its coded right now?? – Surtarso Dec 14 '14 at 20:11
  • @Surtarso Yes, that's it, you should assign a null value to every element that was removed in `amarelo` to avoid null error in `comecaOtraveh`. – akmozo Dec 14 '14 at 20:14
  • can I send you flowers? I think I love you! hahahahahah thank you! worked perfectly!!! – Surtarso Dec 14 '14 at 20:22