External Exam Download Resources Web Applications Games Recycle Bin

moving platforms left & right | gml



Add a new object objMovingRightBlock use the same sprite as the objBlock.. and make the objBlock the parent object. This way the objMovingRightBlock will inherit the properties of the objBlock:



Don't stack the horizontal blocks side by side to make a wider platform. If you do, you may be standing trigger simultaneous collisions with two blocks at once, doubling the speed at which the platform carries you sideways (this will result in you looking like you are travelling twice as fast as the platform under you).

Instead, make a wider platform sprite:



Basic gml code for objMovingRightBlockStep event could be as follows:

objMovingRightBlock Step

x += 1;
if place_meeting(x, y-1, objPlayer){
  objPlayer.x += 1;
}

This would simply shift the player a fixed speed of 1 step per second right.



moving both directions

A better way might be to create a objMovingLeftRightBlock using the previous sprite, but this time the GML will include range, move speed and direction:

objMovingLeftRightBlock Create

going = "left";
range = 200; //px
move_speed = 3;

objMovingLeftRightBlock Step

if (going == "left"){
  x = x - move_speed;
  if (x <= xstart - range) going = "right";
} else {
  x = x + move_speed;	
  if (x >= xstart + range) going = "left";
}

if place_meeting(x, y-1, objPlayer){ 
	if going == "left" {
		objPlayer.x = objPlayer.x - move_speed;
	}
	if going == "right" {
		objPlayer.x = objPlayer.x + move_speed;
	}
}

Challenges

1. Implement the above code

2. Add a few moving blocks, adjust the range, move speed and direction (on create), and test them to see if they work.

3. Make a platform only viewable within range.. very difficult! might take some research. You can adjust the image_alpha of an object to make it fade as it disappears from sight (or a torch light beam).