• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Moving Platforms! Duplicate Obj. isn't working

W

wesk1288

Guest
Hey everyone, first time posting in GMC and I'm a total beginner.

In my game, I have a basic moving platform that when when the player stands on it, his speed matches it and he moves with it. That's all working fine. However, when I try to duplicate the platform obj. and place more of them into my room, only one of them works (the most recent one created). All of the platforms still have the collision and left and right movement but the player no longer matches his speed with the platform (except for the most recent one created).

What am I missing here? Thanks in advance for any help. Here is the code of my Obj_Platform:

Create Event:

randomize()

dir = choose(-1, 1);

Step Event:

//Platform Movement
x += horizontal_speed * dir;

if(x < position_from || x > position_to) {
dir *= -1;
}
//Player Detection On Platform With Movement
if(place_meeting(x, y-1, Obj_Player)){
Obj_Player.speed = horizontal_speed * dir;
} else {
if(!place_meeting(x, y-1, Obj_Player)){
Obj_Player.speed = 0;
}
}

horizontal_speed, position_from, and position_to are all set in the variable definitions of my object.
 

Anixias

Member
The randomize() function only needs to be called once, at the beginning of the game.

Try putting the detection code in the player's step event rather than in the platform:
Code:
var o = instance_place(x,y+1,obj_platform);
if (instance_exists(o))
{
    speed = o.horizontal_speed*o.dir;
}
else speed = 0;
Also, your detection code is very redundant. You test if there is a meeting with the player, and if there isn't (else), you test if there isn't a meeting with the player. You don't need to check it the second time, since it will only go into that "else" block if there isn't.

PS:
The reason it's only working with the last spawned one is that your code sets the players speed to 0 if it's not on each platform. The last spawned platform runs that code last (after the others) and finds that the player is on it, which is why it works. But any object spawned earlier than that platform will have the speed set to 0 when the newest platform runs the code. Hence, put it in the player's step event.
 

TheouAegis

Member
Also you are setting the player's speed. That basically overrides any speed value you set in the player. Can your player even move on his own while standing on a moving platform? Are you using a custom movement mechanic on top of using the built-in variables?
 
W

wesk1288

Guest
The randomize() function only needs to be called once, at the beginning of the game.

Try putting the detection code in the player's step event rather than in the platform:
Code:
var o = instance_place(x,y+1,obj_platform);
if (instance_exists(o))
{
    speed = o.horizontal_speed*o.dir;
}
else speed = 0;
Also, your detection code is very redundant. You test if there is a meeting with the player, and if there isn't (else), you test if there isn't a meeting with the player. You don't need to check it the second time, since it will only go into that "else" block if there isn't.

PS:
The reason it's only working with the last spawned one is that your code sets the players speed to 0 if it's not on each platform. The last spawned platform runs that code last (after the others) and finds that the player is on it, which is why it works. But any object spawned earlier than that platform will have the speed set to 0 when the newest platform runs the code. Hence, put it in the player's step event.
Thanks for the reply! I’ve been away on business so I’ve been dying to get home and try these fixes. Yes this makes sense now about the reset of the player speed and why it’s not working on all platforms. I will update my code within the player’s step event and let you know. Thank you!
 
W

wesk1288

Guest
Also you are setting the player's speed. That basically overrides any speed value you set in the player. Can your player even move on his own while standing on a moving platform? Are you using a custom movement mechanic on top of using the built-in variables?
Hey TheouAegis, yes! I am using a custom movement system in my player’s code. Basically setting hspeed, spd, vspd, and other move variables to move my player in the X direction and jump. I can see now how that’s probably a bad idea to then use the built in speed mechanic, overriding the system I have in place. I’m going to remove this.

My player can move on his on once on a platform, jumping and all. That’s why I felt I was getting close, but there was just something I wasn’t seeing in my logic.
 
W

wesk1288

Guest
The randomize() function only needs to be called once, at the beginning of the game.

Try putting the detection code in the player's step event rather than in the platform:
Code:
var o = instance_place(x,y+1,obj_platform);
if (instance_exists(o))
{
    speed = o.horizontal_speed*o.dir;
}
else speed = 0;
Also, your detection code is very redundant. You test if there is a meeting with the player, and if there isn't (else), you test if there isn't a meeting with the player. You don't need to check it the second time, since it will only go into that "else" block if there isn't.

PS:
The reason it's only working with the last spawned one is that your code sets the players speed to 0 if it's not on each platform. The last spawned platform runs that code last (after the others) and finds that the player is on it, which is why it works. But any object spawned earlier than that platform will have the speed set to 0 when the newest platform runs the code. Hence, put it in the player's step event.
It's working! Your code works in my players step event and now my platforms can be duplicated and function properly! Thank you!
 
Top