snap sprite to grid in game

Get help using Construct 2

Post » Tue May 14, 2013 2:12 am

Hey guys,
I created this little game with falling blossoms that make sounds, when you drag them into a tree they stick, and when a "time arrow" passes over them they play their sound. My problem is I need them to snap the blossoms to some sort of grid when the drag and drop is released, or else its impossible to make a little tune with any structure.
So basically, how to I get sprites to snap to a grid when they're dropped by the cursor?

Thanks in advance!
Dougx
B
2
Posts: 8
Reputation: 166

Post » Tue May 14, 2013 2:15 am

http://rosietheband.com/cadenseed/

I chucked it up online so you guys can see what I'm on about!
:)
B
2
Posts: 8
Reputation: 166

Post » Tue May 14, 2013 2:19 am

@dougieddug

In a nutshell:

1. Determine the size of your grid tiles such as 16x16
2. Take the x coordinate of your sprite such as 77.
3. Divide that coordinate by the size of your grid tile so 77/16 = 4.8125
4. Discard the .8125 by using the floor(77/16) = 4
5. Multiply your new value by the size of your grid tile so 4 x 16 = 64.

What this does it removes the fractional component of each coordinate. So any x coordinate from 0 to 15 will snap to 0. From 16 to 31 will snap to 16. From 32 to 47 will snap to 32. From 48 to 64 will snap to 48.farmerdwight2013-05-14 02:21:10
B
10
S
3
Posts: 102
Reputation: 1,888

Post » Tue May 14, 2013 3:01 am

Thanks man, I really appreciate the help, but I'm a little confused. How do I go about implementing this? (be gentle I picked up construct yesterday)
Also, just to check, when all this is done, will I be able to drop a sprite anywhere and it will snap to the nearest point to it on the grid?

sorry to be a pain, you've been really helpful :)
B
2
Posts: 8
Reputation: 166

Post » Tue May 14, 2013 11:40 pm

@dougieddug

here's an example

https://dl.dropboxusercontent.com/u/123756210/GridSnapExample.capx
B
10
S
3
Posts: 102
Reputation: 1,888

Post » Tue May 14, 2013 11:55 pm

A simpler way:

- On Drag and Drop Dropped
-> Object set X to Object.X - (Object.X % CellSize)
-> Object set Y to Object.Y - (Object.Y % CellSize)
B
40
S
12
G
7
Posts: 603
Reputation: 7,656

Post » Thu May 16, 2013 12:13 am

Both really helpful guys :) thanks a bunch!
B
2
Posts: 8
Reputation: 166


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 20 guests