Spawning objects on a grid?

Get help using Construct 2

Post » Wed Oct 16, 2013 10:32 pm

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
B
9
S
3
Posts: 88
Reputation: 931

Post » Thu Oct 17, 2013 12:25 am

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.
[edit]: 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.onzephyr2013-10-17 00:36:37
B
88
S
14
G
9
Posts: 101
Reputation: 9,962

Post » Thu Oct 17, 2013 6:39 pm

Onzephyr: Thanks!!! Very simple and elegant. yea, learning the native expressions.

bye bye
B
9
S
3
Posts: 88
Reputation: 931


Return to How do I....?

Who is online

Users browsing this forum: max2612 and 6 guests