Move only in intersections of 32px

I’m moving my character around using a Tween behaviour and I am trying to restrict his movement to a “grid” of 32 pixels. It works fine when moving in a straight direction, but when two or more directions are given at once, he seems to move off this “grid”

Above he is aligned with the “Grid”

Here he seems to have strayed off.

Here are the events.

Can anyone suggest a method for solving this problem pls?

Do you want 2 keys can be pressed at once? If not,
avoid two keys to be pressed at same time.
For instance:
In your first event add:
a key is pressed (invert condition)
w key is pressed (invert condition)
s key is pressed (invert condition)

Lazy Way: Everyframe move the player’s position to the nearest X and Y divided by 32.

? It’s the same of adding 1.

I wasn’t saying tween, I was saying actually modify the position of the sprite to snap to the closest x / 32 and y / 32.

If I was you I’d redesign the movement logic entirely to use a single tween that moves from the current location to the target location. The input keys would only modify the target location if the tween was not currently playing. But first check if the tween is not playing and the target location is not equal to the current location, in which case play the tween to move from the current location to the target location.

I think is easier keeping like it is and check if other keys are not pressed. The way you suggest he needs to check if 3 different tweens are not playing.

You only need 1 tween: Change Position.
But yeah, you do you… I’m sure you’ll come up with what works best for you.:+1:

1 Like

Thanks anyway! The post owner didn’t come back!! :+1:

1 Like

Ty, guys

Sorry, I took so long. Ty for all the support. Will definitely try your suggestions.

How should I do this? Is there a mathematical tool for this?

Divide the X and Y positional values by 32, take the integer part, add 1 and multiply by 32.

int((player.X() / 32) + 1) * 32

I wonder why don’t you try something like this.

Ty for all the suggestions and support!

I used a premade function which also works like a charm, but ty for all suggestions. Your suggestions obviously provide more flexibility than a premade function. Ty.