Discuss Scratch

MiaPie
Scratcher
59 posts

Snap to grid?

How do I make a sprite snap to an irregular grid? Usually I use the following code:
whenclickedforevergotox:roundmousex/16*16y:roundmousey/16*16

However, in my current project the grid is offset by 4 on the x axis and 8 on the y axis. How can I make the sprite snap to such an offset grid without it hovering around my mouse weirdly?
asivi
Scratcher
1000+ posts

Snap to grid?

MiaPie wrote:

How do I make a sprite snap to an irregular grid? Usually I use the following code:
whenclickedforevergotox:roundmousex/16*16y:roundmousey/16*16

However, in my current project the grid is offset by 4 on the x axis and 8 on the y axis. How can I make the sprite snap to such an offset grid without it hovering around my mouse weirdly?

This would be doing the job pretty well

whenclickedforevergotox:roundmousex/4*4y:roundmousey/8*8

If it looks weird check the costume center in the paint editor.
awesome-llama
Scratcher
1000+ posts

Snap to grid?

you can also add numbers to the rounded coordinates to get the sprite in the right position.
MiaPie
Scratcher
59 posts

Snap to grid?

The game is a mining simulator, and I need this crosshairs sprite to snap onto the blocks so it is only touching one block at a time. That's why it has to move in 16 pixel increments. Normally i could just do the code I submitted in the OP, but I want the crosshair to match exactly. If I do addition (adding 4 to the x for example) it looks weird and I can't make it look normal again.
awesome-llama
Scratcher
1000+ posts

Snap to grid?

Can you share and link the project?

Last edited by awesome-llama (June 24, 2017 01:13:01)

asivi
Scratcher
1000+ posts

Snap to grid?

Make two costumes for those blocks, one with the crosshair painted on and the othe without it, so all that you need to do is a costume change.
That if i'm not missunderstanding you otherwise explain you want to achieve more in detail.

Last edited by asivi (June 24, 2017 10:12:54)

CatOverload
Scratcher
100+ posts

Snap to grid?

You can't figure this out without acutally knowing the pixel dimensions in each block. (Or maybe not, i can't figure it out any other way) You can find the dimensions in the Costume section (Xdim. x Ydim. ) But i'm not sure if you made it with clones, or if you made the whole map by itself. If the latter, you'll have to redesign the whole map, and even maybe the mechanics.
Sorry, please get back to me if you think this might be a solution \\\
gtoal
Scratcher
1000+ posts

Snap to grid?

drag and drop with snap to grid demo: https://scratch.mit.edu/projects/58544578/
griefercube
Scratcher
500+ posts

Snap to grid?

MiaPie wrote:

The game is a mining simulator, and I need this crosshairs sprite to snap onto the blocks so it is only touching one block at a time. That's why it has to move in 16 pixel increments. Normally i could just do the code I submitted in the OP, but I want the crosshair to match exactly. If I do addition (adding 4 to the x for example) it looks weird and I can't make it look normal again.
Ah yes just type it in and add a number that’s it!
griefercube
Scratcher
500+ posts

Snap to grid?

Like this:
gotox:roundmouseX/xposition+numbery:roundmouseY/yposition+number

Last edited by griefercube (Sept. 7, 2020 00:07:50)

deck26
Scratcher
1000+ posts

Snap to grid?

griefercube wrote:

MiaPie wrote:

The game is a mining simulator, and I need this crosshairs sprite to snap onto the blocks so it is only touching one block at a time. That's why it has to move in 16 pixel increments. Normally i could just do the code I submitted in the OP, but I want the crosshair to match exactly. If I do addition (adding 4 to the x for example) it looks weird and I can't make it look normal again.
Ah yes just type it in and add a number that’s it!
Please don't necropost - this is from 2017. Your post doesn't add anything new.
joshuaho
Scratcher
1000+ posts

Snap to grid?

griefercube wrote:

MiaPie wrote:

The game is a mining simulator, and I need this crosshairs sprite to snap onto the blocks so it is only touching one block at a time. That's why it has to move in 16 pixel increments. Normally i could just do the code I submitted in the OP, but I want the crosshair to match exactly. If I do addition (adding 4 to the x for example) it looks weird and I can't make it look normal again.
Ah yes just type it in and add a number that’s it!
Thanks for your help! However, this thread is old - so I doubt they're still looking for answers. I would suggest looking for newer topics (that still need to be answered) and respond to those.

Powered by DjangoBB