GameMaker Grid movement for an endless runner

C

ColChope

Guest
Hello everyone !

I would like to make an endless runner using tiles on which the character can move. Basically, the character can only move right, so my tiles are moving left. I choose to move only the tiles and not the character, so here is what I struggle to do :

When my character wants to move right, the tiles have to move left. First, a new column of tiles is created outside of the room to the right, the all the tiles (those who are on the already on the room and created before and those who just got created) move left. The tiles that are at the left go outside (to the left) and get destroyed, the other tiles on screen just move one column left, and the new tiles created enter the screen (on the right).

So, to sum it up, if you have 3 column of tiles and you want your character to go right :

  1. A new column is created outside the room (to the right)
  2. The left column leaves the room to the left and is destroyed
  3. The 2nd column takes the place of the first, the 3rd takes the place of the 2nd and the newly created takes the place of the 3rd.
What I've done so far :
  • I created all the columns in a Create event of an obj_game with one single instance created, so I don't have to do it in the tile object, since it would run it for each instance.
  • Still in the obj_game, when D button is pressed (go right), I create the new column outside of the room.
  • - In the tile object, when D is pressed, I run this script in the step event :

if key_D{
if distance_to_point(current_x-(spr_width+obj_game.tile_separation),current_y) > 5 {
move_towards_point(current_x-(spr_width+obj_game.tile_separation),current_y,5);
}
}

where current_x and current_y are created in the Begin Step event, with :
current_x = x;
current_y = y;
So I want each tile to move from its current position to the same y and the same x minus some length, and stop there, that's why I use these current_x/y variables and move_towards_position.


key_D is defined with :
key_D=keyboard_check_pressed("D");
in the step event.


The issue is the following :

When I press D, the tiles start moving left, so it begins well. But then, not only they never stop moving, but the new columns created are not moving yet. If I press D again, the new columns begin to move.

So, here are some questions about my way to do it :
  • I am doing it right ? I know there are plenty of good way to do something, but also plenty of wrong ones. Since I need to be able to create objects on these tiles, wouldn't it be better if I create all the tiles separately and save all their IDs ? If I have 3*2 tiles, I would have 6 variables with 6 IDs, and I could replace these values with the created tile.

  • Also, am I using the different objects well ? I mean, am I running scripts in the right objects and the right event types ?

  • How could I avoid using the distance_to_point function, which is not pixel perfect ? If tiles are getting slightly shifted for each loop, it'll quickly become a mess.
Also, it's quite different from what I speak about above, but you might have a good solution for that :
  • I have to set the width of my tiles so that they perfectly fit the width of the room. For exemple, if I want 3 columns, each column separated by a certain amount of pixels (what I called tile_separation earlier), I want the 3 tiles width + the 4 tile_separation to be equal to the room width. For that, I've used this to give my sprite the good size :
image_xscale=(image_xscale/sprite_get_width(spr_tile))*(room_width-4*obj_game.tile_separation)/3;

It works fine, but it stretches my sprite. I know it's something quite common in game development (aspect ratios, etc.), but I still do not figure out how it's done to adapt you game to any screen in these circumstances.

For example, if you have to make a chess game for mobile and your tile sprites are decorated so that you don't want them to be stretched (if they're only black and white, it doesn't matter if they're stretched or not), how would you do ?


Thank you so much for your precious time, and I hope I explained my issue well. If you want any precision or have any question, please don't hesitate !
 
Top