1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ What's the actual problem with the way you're doing things now? Being a "hack" is fine as long as it works. Telling us what the problem is now allows us to give an answer for "better way", otherwise we don't know what problem we're trying to solve. \$\endgroup\$
    – House
    Commented May 26, 2015 at 22:40
  • \$\begingroup\$ Well I don't like using the trialBrick at all. If I could live without it that would be better. 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. I'll update the question with these details. \$\endgroup\$
    – Rossco
    Commented May 26, 2015 at 22:43
  • \$\begingroup\$ Don't you just write the method once? What do you mean you have to "remember to exclude the trialBrick"? \$\endgroup\$
    – House
    Commented May 26, 2015 at 22:53
  • \$\begingroup\$ I mean when I iterate through all the bricks on the screen to see if they fall outside of the play area, then I need to exclude the trialBrick since its not part of the game. I'm looping through the bricks in a game controller step event. \$\endgroup\$
    – Rossco
    Commented May 27, 2015 at 3:40

1 Answer 1

1
\$\begingroup\$

Turns out what I was looking for is the instance_position function.

When you use this you are checking a single point in the room for an instance or an object.

Here is my updated code without need for the trialBrick.

// 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)
        {
            if (instance_position(column, row, objFallingParent))
            {
                instance_create(column, row, objBrick);
            }
        }
    }

    // remove ourselves from the game.
    instance_destroy();
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .