Search code examples
actionscript-3flashflash-cs6

Remove all created symbols from stage


I don't usually do this, but i'm lost here.

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

var first_tile:colors;
var second_tile:colors;
var pause_timer:Timer;
var game_timer:Timer;
var colordeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6);

function color_match() {
    game_timer = new Timer(10000,1);
    for (x=1; x<=4; x++) {
        for (y=1; y<=3; y++) {
            var random_card = Math.floor(Math.random()*colordeck.length);
            var tile:colors = new colors();
            tile.col = colordeck[random_card];
            colordeck.splice(random_card,1);
            tile.gotoAndStop(7);
            tile.x = ((x-1)*70)+30;
            tile.y = ((y-1)*100)+30;
            tile.addEventListener(MouseEvent.CLICK,tile_clicked);
            game_timer.addEventListener(TimerEvent.TIMER_COMPLETE,end_game);
            addChild(tile);
        }
    }
    game_timer.start();
}
function tile_clicked(event:MouseEvent) {
    var clicked:colors = (event.currentTarget as colors);
    if (first_tile == null) {
        first_tile = clicked;
        first_tile.gotoAndStop(clicked.col);
    }
    else if (second_tile == null && first_tile != clicked) {
        second_tile = clicked;
        second_tile.gotoAndStop(clicked.col);
        if (first_tile.col == second_tile.col) {
            pause_timer = new Timer(1000,1);
             pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
            pause_timer.start();
        }
        else {
            pause_timer = new Timer(1000,1);
            pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
            pause_timer.start();
        }
    }
}
function reset_tiles(event:TimerEvent) {
    first_tile.gotoAndStop(7);
    second_tile.gotoAndStop(7);
    first_tile = null;
    second_tile = null;
    pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
}
function remove_tiles(event:TimerEvent) {
    removeChild(first_tile);
    removeChild(second_tile);
    first_tile = null;
    second_tile = null;
    pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
}

function end_game(event:TimerEvent) {


}

This is a little colour matching game. Click two tiles, they dissappear if matched, or turn back to grey if not. The loop creates instances of colour, in randomly placed pairs, and sets them to frame 7 (grey colour).

I cant work out how to remove any remaining colour blocks when the game time hits zero. Everything i try is throwing errors. The idea is then to let people play again, or a win script.

You don't have to necessarily code it for me, i just need to understand the process! Thanks.


Solution

  • I believe the best way is to create a container, so you can add all tiles and manage them on the best way you decide to.

      var tileContainer:Sprite = new Sprite();
      addChild(tileContainer);
    
      // instead of addChild(tile);
      tileContainer.addChild(tile);
    
      // to remove all tiles
      tileContainer.removeChildren();