# How to make a tile vanish after player steps on it, in a tilebased game?

So I'm adding the basic mechanic of making frail/breaking tiles, those that break apart and disappear after the player has walked ontop of them. Now I thought this would be as easy as...:

//Breaking tiles represents the 2d array number, that describes the breaking tiletype.

if( TileWeAreStandingOn == breakingTile){

//frameCount is a variable that counts the frames, after 30 frames (1 second) the tile disappears.

frameCount++;
if(frameCount > 30){

//So, when 30 frames has passed, the tile is removed

TileWeAreStandingOn = noTile;
frameCount = 0;

}
}


Now this works, but it only works WHILE you are standing on the breaking tile, once you jump, the countdown stops since the initial if-statement is not valid anymore. So I am trying to find ways around this where the player could jump onto a frail/breakable tile, and then jump onwards, and the tile would still break after 1 second. Also, if he walks past several breakable tiles in a row, they should all break timely, that is, they should all break after a second of the player has moved off of that tile. (which would result in a domino like timing, where one breaks shortly after another)

I need to make the tiles work independently, so that each tile reacts and starts to disappear/break whenever the player touches it. But my system is build around how the player interacts with the 2d array map and the tiles aren't independent entities, only the player is.

So I'm asking for suggestions how I can make the tiles react independently based on Players interactions with them? Especially in this case, where a tile should disappear a second after the players has touched it.

Isn't it as simple as having a collection of tile locations and frame counters that get updated?

So where you have that code now (in rough pseudocode),

if( thisTile == breakable && !breakingTiles.Contains( thisTile ) )
{
}


And somewhere else you do something like

foreach( tile in breakingTiles )
{
++tile.timeUntilBroken;
if( tile.timeUntilBroken > 30 )
{
tile.Break(); // or whatever
}
}

• Got damnit that is beautiful! YES. That worked perfectly, although I had to translate it into Flash AS3-array syntax. And I created a new object called 'frailTile' each time you stepped on one. And pushed that object into an array called frailTileArray. And the "somewhere else" (like you put it) I placed the for-loop that added counters to their specific time. Splendid! Thx Tetrad. (And oh yah, this is my post, but earlier I couldn't find my login so I used a temporary one, can I take authority back on this post so that I can validate this answer?)
– Kid
Sep 10 '11 at 2:37
• damn this is annoying, I have an hotmail account here, but I see no way of signing in with hotmail? Any how to's on here?
– Kid
Sep 10 '11 at 3:07
• I merged your accounts. Also gamedev.stackexchange.com/faq Sep 10 '11 at 3:44
• thx man. Although I couldn't find anything about logging in with your hotmail account on the FAQ-page. Any ideas how?
– Kid
Sep 10 '11 at 5:09
• You can save some memory by not storing timeUntilBroken in every tile object, but rather in the breakingTiles structure, since most tiles aren't in the process of breaking. Sep 10 '11 at 11:53

Use flash.utils.Timer to fire a destruction event for each tile.

In the simplest case, this would look something like this (no guarantee for syntax correctness, I don't have the SDK installed here).

if( TileWeAreStandingOn == breakingTile)
{
var destructionTimer:Timer = new Timer(1000 /* milliseconds */, 1);
/**
* This assumes TileWeAreStandingOn is a local variable in this method's scope,
* so it can be bound with its current value into the event-handling closure.
* It would need a small rewrite if it's an instance or class variable.
*/
function {) { TileWeAreStandingOn = noTile; });
destructionTimer.start();
}

• +1 Although you should check to not start a new timer every time ;)
– pek
Sep 9 '11 at 22:36
• @Martin Sojka, this seemed most convenient at most understandable to me at first. But once I tried it in many different ways, I kept getting errors, and that probably because of me being a novice programmer with little and less knowledge of the Do's and Dont's with the Timer property. But as it seems so usefull I'm digging into it now even though Tetrads solution worked fine. Thx Martin!
– Kid
Sep 10 '11 at 3:41
• @pek: Depends on the amount of tiles which can be in this "soon to vanish" state at any given time. If it's just a few, use a Timer for each, that's simple and concise enough. If that can be more than about 10 (estimated), better use a global handling function like Tetrad suggested. Sep 10 '11 at 5:48

The way around it is to have/create a memory where You store information WHAT should happen and WHEN it should happen. Then memory should be checked whether time has elapsed (in WHEN) and if it did, then do that WHAT should happen.

You can acomplish this in several ways:

• have few variables, this would work for one event (one WHEN, WHAT)
• have array of variables ... see Tetrad
• have callback ... see Martin

Each has its own pros/cons.

You seem to me to be quite new/unexperienced to programming. I would recommend You to do some more excercises in general programming. Data structures and algorithms. Quite nice book about programming is C Programming Language, the language it is for -C- is similar to Flash. You should also try searching stackexchange for recomendations on beginners book/tutorials on programming.

• Flash (ECMAScript/ActionScript) is actually more of a functional language than anything else. It just hides it rather well. :) Sep 10 '11 at 18:35
• @Martin true, I found quite interesting article on Turing from Steve Yegge. This hiding/abstracting of machine language makes programmers more productive, but ML is still there. Sep 11 '11 at 8:46