How do I work with grids/cells?

Not favoritedFavorited Favorited 0 favourites
  • 4 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • Not sure how else to word it but like for example how in the tile based movement behavior you can have it so it moves cell by by cell. Are there ways to make drag and drop snap to cells too? Or to make turret behaviors target cells or bullets to damage every enemy in that same cell etc? Or change the color of the cells a unit can move to the way fire emblem does it? And so on?

  • When I work with cells, i like to give each instance the vars Cell_X and Cell_Y, it doesn't represente the x/y possition, it represent the position on the grid, like 0-0, 0-1, 0-2, etc.

    So you can iterate over it with a for loop inside another for loop:

    -For "x" loop

    ---For "y loop

    If you have a player use also Cell_X and Cell_Y there, so you can compare its possition with the cells of the grid.

  • I'm sorry I don't think I understand how to do any of that.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'm sorry I don't think I understand how to do any of that.

    Ok, i will try to simplify it.

    Every cell in your game is an sprite called Sprite_Cell. Imagine that you make a grid of 8x8 with those Sprite_Cell of size 64x64.

    So you add instance variables(number) to the Sprite_Cell: Cell_X and Cell_Y to identify the column and the cell for each instance.

    Why this? Because "Tile Movement Behavior" has its own grid, but Drag & Drop, Bullet and Turret do not automatically know about it. You have to make them use the same cell size and grid position.

    An object using Tile Movement already knows its cell. You can get this with Player.TileMovement.GridX and Player.TileMovement.GridY.

    If the Player or other object haven´t the Tile Movement you can calculate the Sprite_Cell with:

    Player.Cell_X=floor(Player.X/64), the same for Cell_Y.

    Here is how that apply to your examples:

    -For drag & drop: When you drop the player-> On drop: Set X to floor(Player.X/64)*64+32 and the same for Y. This finds the cell where the piece was dropped and moves it to its center.

    -For damaging every enemy in the bullet's cell: Give the bullet and the enemies instances the same variables: Cell_X and Cell_Y, so you can compare if:

    (Enemy.Cell_X = Bullet.Cell_X and Enemy.Cell_Y = Bullet.Cell_Y)--->Substract health from enemy.

    -Showing movement range: You can check the distance form the player with:

    (abs(Sprite_Cell_X-Player.Cell_X)+abs(Sprite_Cell_Y-Player.Cell_Y)

    For example, if you make a for each(Sprite_Cell) and the distance is less than or equal to the Range of Movement that you chose--->Change Sprite_Cell color.

    The nested X and Y loops I mentioned before are only useful when creating or checking the entire grid. You do not need them just to snap one object to a cell.

    I hope this make sense, but i know that could be difficult to understand, also its difficult to me to explain this with text.

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)