Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 07:44:54 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[Game Maker] creating a moving platform [SOLVED]
Pages: [1]
Print
Author Topic: [Game Maker] creating a moving platform [SOLVED]  (Read 1906 times)
Sokafootball
Level 0
**


View Profile
« on: May 23, 2016, 03:46:54 AM »

Hello everyone,

I am a beginner game maker developer.

I am trying to create a moving horizontal platform for a platform 2d game in Game Maker.

I did some research and many developers use marks to stop the object from moving and changing its directions.

While i understand this perfectly works, i still want to understand why my code is not working.

In this example the platform should move between a space of 128 pixels at 2 pixels per step, with the center of that space being the x coordinate at the spot of creation.

My platform object has 2 events

------------------------

[Create]

///Speed, distance to travel and starting direction
movespeed = 2;
movedirection = 1; //choose starting direction, 1 for right -1 for left
traveldistance = 64;
startingx = x;

[Step]

destination = startingx + traveldistance * movedirection;
while (x != destination)
{
x += movedirection*movespeed;
}
movedirection *= -1;

-------------------------

What happen is that once the game starts the platform is blinking at ridiculous speed between the two edges of that 128 pixels space.

I am not sure, but it seems it's not just moving very very quickly, but it's actually teleporting back and forth.

Thanks for any help,



Laurent
« Last Edit: May 23, 2016, 10:14:32 AM by Sokafootball » Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #1 on: May 23, 2016, 06:42:12 AM »

With the while loop in [Step], it looks like you've set it up to move completely to its destination every frame. Presumably, you'd want something more like this:

Code:
[Create]

///Speed, distance to travel and starting direction
movespeed = 2;
movedirection = 1; //choose starting direction, 1 for right -1 for left
traveldistance = 64;
startingx = x;
destination = startingx + traveldistance * movedirection;

[Step]

x += movedirection*movespeed;
if (x == destination) {
  movedirection *= -1;
  destination = startingx + traveldistance * movedirection;
}

That way, each time [Step] happens, the x coordinate will only be moved a little bit rather than continuing to move all the way to the destination. Note that I've moved the calculation of destination into [Create], and also put a recalculation of it inside the if statement in [Step].

Something to watch out for: If you ever need to move in non-integer increments, or your traveldistance isn't divisible by your movespeed, the == check for destination won't work and your platform will continue past its destination. Even when using only integers, it's safer to use some form of range check. Since there are two directions, it's a bit more to write, but something like this could do the job:

if ((movedirection == -1 && x <= destination) || (movedirection == 1 && x >= destination))
Logged

Sokafootball
Level 0
**


View Profile
« Reply #2 on: May 23, 2016, 08:56:50 AM »

Thank you so much!!!  Gentleman

now it makes sense   Facepalm
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic