# Legacy GM[SOLVED] Moving between points?

#### Wuzzems

##### Member
I'm relatively new to coding.

Let's say I have a player object, and I want this obj_player to be able to move between three points on the y axis.

The first point will be the center of the y axis (room_height/2)

the other two points will be the halfway point between the center and the top and bottom of the room

point 2: (room_height/2)/2
point 3: (room_height/2)*1.5

I want to be able to press up once and for the obj_player to move to the point above it, same story for pressing down once. How might I go about implementing this? Also please mention in which Events all of this needs to be implemented to.

Last edited:
T

#### TheMatrixHasMe

##### Guest
This is one way to do it off the top of my head but I'm sure a more optimal way can be found. But this should work for you. A lot of it is pseudo code so you'll have to come up with the code that fits the logic.

Do this in the create event. This will initialize your arrays and store the values you need in the arrays.
Code:
``````point[2] = false;
point_x[2] = (the top x coordinate here);
point_y[2] = (the top y coordinate here);
point[1] = true;//if you want to start in the middle
point_x[1] = (the middle x coordinate here);
point_y[1] = (the middle y coordinate here);
point[0] = false;
point_x[0] = (the bottom x coordinate here);
point_y[0] = (the bottom y coordinate here);``````
Do this in the step event and do the same thing for your pressed down but switch the //do nothing part to point[2] when pressing down and then put the inverse codes in the other blocks. For example when at point[1] you become point[2] instead of point[0].
Code:
``````if pressed_up{
if point[0]{
//do nothing
}
else if point[1]{
point[1] = false;
point[0] = true;
code to move to point_x[0],point_y[0];
}
else if point[2]{
point[2] = false;
point[1] = true;
code to move to point_x[1],point_y[1];
}
}``````
I designed this so you'll actually have to think it through and put in the correct syntax and codes. In the end after you implement it you'll be smarter than you would have been if you had copied and pasted code that worked. I can coach you if you have questions.

And don't be afraid of those arrays, they are just like variables point0 point1 point2 etc. accept you can use them in things like for loops which you'll learn is very powerful later on so you may as well dive into them now

#### Perseus

##### Not Medusa
Forum Staff
Moderator
Should be easy.

Create:
Code:
``````pt[0] = (room_height/2)/2;
pt[1] = room_height/2;
pt[2] = (room_height/2) * 1.5;
pos = 1;``````
Step:
Code:
``````if (keyboard_check_pressed(vk_up)) {
if (pos > 0) {
pos -= 1;
}
}
else if (keyboard_check_pressed(vk_down)) {
if (pos < 2) {
pos += 1;
}
}
if (point_distance(x, y, x, pt[pos]) > 4) {
move_towards_point(x, pt[pos], 4);
}
else {
speed = 0;
y = pt[pos];
}``````

#### Wuzzems

##### Member
Thanks a lot guys.

My biggest problem was that I was checking for the up key together with point_distance and this caused my object to only move about 10 pixels upwards cause my if statement was only true for one frame.

#### Wuzzems

##### Member
Another quick question! When I changed the speed at:

move_towards_point(x, pt[pos], 4)

I made it 20 and it's doing the vibrating thing as if the speed is not being set to 0 afterwards. It's only doing this at the top and bottom locations.

Is it because of the size of my room?
Right now it's 1366 x 768, does the speed per frame have to be able to divide 192 into an integer, a number without fractions?
If so is there a way to bypass this and will it affect the speed for people who are playing on different resolutions?

#### Perseus

##### Not Medusa
Forum Staff
Moderator
Change this:
Code:
``if (point_distance(x, y, x, pt[pos]) > 4) {``
...to this:
Code:
``if (point_distance(x, y, x, pt[pos]) > 20) {``
Basically a speed of 20 means that the instance would move by 20 pixels per step. The statement checks if you're at least 20 pixels away from the target. If you are, you move at a speed of 20 towards it. If you're already within 20 pixels range of the target, your speed would be set to 0 and you'll jump to the target, so as to avoid vibration of the instance.