# Spawning objects on a grid?

0 Favourites
• 3 posts
• Hello,

here is my first novice question/problem that I dont know how to tackle properly/simply.

I have a character with top down smooth movement. The background is made of lets say 16x16 tiles. I want to spawn objects (with character) always on the centre of the tile character is standing on. If he is on the x1, y1 - object spawns at (8,8). If he comes to (16,16) his spawned object appears at (8,8). If he comes to (17,17), object spawns at (24, 8) and so forth. I hope I described the idea correctly.

THX on any help!

moxBorealis

• To snap an X Y position the basic idea is

x=round(x/Gridsize)*Gridsize

y=round(y/Gridsize)*Gridsize

so for a 16x16 grid it's

x=round(x/16)*16

y=round(y/16)*16

But depending on your objects origins you may need to add an offset

or instead of round use Floor or Ceil

In your example lets say "character" and "spawn_object" both have centered origin points. It's a 16x16 grid and you want it placed in the center of the tile. you would put an action like this.

system create "spawn_object"

x=floor(character.X/16)*16+8

y=floor(character.Y/16)*16+8

hope that helps.

: I should mention I'm using "system create object" instead of having the "character" spawn the object. That way It can be done in one action with an expression instead of spawning the object then snapping it's position with a second action. Also when one object spawns another the spawned object inherits it's rotation. Your example didn't mention wanting that.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• Onzephyr: Thanks!!! Very simple and elegant. yea, learning the native expressions.

bye bye