first question on this site :-)
I'm not really sure that the title is very clear so feel free to edit/suggest a better one.
I have a piece of Game Maker code which works, but feels like a bit of a hack and also causes some side affects else where so I wanted to know if there is a better way.
I'm building a Tetris clone and this snippet occurs in the Step event of the falling tile parent object. The idea is that when the falling object collides with something it should be replaced by 'brick' objects so that I can destroy them independently.
So what I want to do is test whether a brick would overlap with the falling object and if so then create a brick there. I'm using an instance of brick (called trialBrick and created globally) to do the testing.
I could just try creating a brick and then destroy it if it doesn't overlap with the falling object but I'm not sure whether this is efficient or not.
Thoughts?
// just move the brick downward if we can by one pixel.
var obstacle = instance_place(x, y + 1, all);
if (obstacle != noone)
{
isFalling = false;
// loop through each row of the object.
var row;
var column ;
for (row = y - 4 * brickSize; row < y + 4 * brickSize; row += brickSize)
{
for (column = x - 4 * brickSize; column < x + 4 * brickSize; column += brickSize)
{
// need to check whether the current row and column intersects with this falling object.
trialBrick.x = column;
trialBrick.y = row;
with (trialBrick)
{
if (place_meeting(x, y, objFallingParent))
{
instance_create(x, y, objBrick);
}
}
}
}
trialBrick.x = 0;
trialBrick.y = 0;
// remove ourselves from the game.
instance_destroy();
}
Edit: The 'problem' is when I test whether the bricks are outside the play area (indicating game end) I have to remember to exclude the trialBrick. It would be nicer if I didn't have the trialBrick at all.