Moving platform help


(Nate ) #1

Hey guys! I have platforms in my game that move left and right, and when the player lands on one it acts like the ground or part of my levels grid which is what I want it to do. So the first thing that comes to my attention is the platform moves out from under the player, as it should, I was not messing with the players x coordinate at all.

So I plugged in some code for the players x value when colliding with a platform and essentially made something like player.x += platform.moveX, this works HOWEVER the player locks to the platforms origin basically.

What would be a good way to have the player able to walk around on top of the platform and still scroll with the platform?

Thanks guys!


(Brad Davies) #2

I prototyped something similar to this the other day, I will post my implementation when I get home in about 30 minutes!


(Nate ) #3

Okay cool thank you so much! :smiley:


(Brad Davies) #4

http://www.fastswf.com/J4oIStE (A & D to move left/right, SPACE to jump)

Something like this? I know you can get stuck in the platform but im sure you can figure out how to solve this :).

So, here is my MovingPlatform class; http://pastebin.com/FqvcnRAS

So here you can see I keep track of a dX and dY which are public variables. I simply add the platform to my level (Im sure you know how to do this), and then this is my Player class code, specifically, the update() code;

    super.update();
var iX:int = 0;
if (Input.check(Key.A)) iX--; if (Input.check(Key.D)) iX++;
//
_dX = 200 * FP.elapsed * iX; //Horizontal movement
//
// solid is just static Grid mask, movingSolid is any MovingPlatform class type
var colliders:Entity = collideTypes(["solid", "movingSolid"], x, y + 1);
if (colliders)
{
	_dY = 0; //Set our dY to 0 because we are on ground
	
	if (colliders is MovingPlatform) //But, if we are on a MovingPlatform
	{
		_dX += MovingPlatform(colliders).dX; //Lets add the platforms dX to our players
	}
	if (Input.pressed(Key.SPACE))
	{
		_dY = -10; //Jump code (v. basic)
	}
}
else
{
	_dY += 30 * FP.elapsed; //If we are not on ground, increase our dY
}

moveBy(_dX, _dY, ["solid", "movingSolid"]); //FP's magical moveBy function is useful!

Apologies if I have not explained this well, I can upload the source files if you wish to look.

NOTES: I know the code is messy and not ideal, but it shows how the basic idea works. It can easily be improved. :slight_smile: I also know there are a few var’s in there I never use, thats because I was testing!

EDIT: Uploaded the FlashDevelop project for anyone interested (Again, the code is messy & buggy! Maybe try improve it?) - FP-Platformer.zip(277.3 KB)


(Nate ) #5

Okay thank you! I will take this apart and re-work it! :smiley: Thanks so much


(Abel Toy) #6

You’ll also need to adjust player speed, because when moving in the same direction as the moving platform, you go twice as fast.


(Nate ) #7

I have tried looking over your code and adapting mine, and even starting from scratch but the unused variables are confusing, here is what I currently have, it snaps my character to the origin of the platform, what should I change to make the character land on the platform wherever he hits it and also be able to move left and right on the platform while he scrolls with the platform?

var collisionPlatform_H:theMovingPlatform_Horizontal = theMovingPlatform_Horizontal(this.collideTypes("floating_platform_h", this.x, this.y + 5));
		if (collisionPlatform_H != null)
		
		{
			impactHasOccured = true;
			another_bool = true;	
		}
		
		if (!collisionPlatform_H)
		{
			impactHasOccured = false;
			another_bool = false;
		}
		if (another_bool == true)
		{		
			x = collisionPlatform_H.x;
		}

(Jacob Albano) #8

Here’s what I do in my current game (more or less):

// this is in the MovingPlatform class
override public function update():void
{
    // store the current position to get the delta
    var _x:Number = x, _y:Number = y;

    // move horizontally
    moveBy(speed, 0);

    // see if the player is on top of this platform
    var p:Entity = collide("player", x, y - 1);
    if (p)
    {
        // move the player the amount that this platform moved this frame
        p.x += _x - x;
        p.y += _y - y;
    }
}

(Nate ) #9

I just did this and I am able to collide with the platform however the player does not scroll with it, the platform will scroll out from underneath the player.


(Jacob Albano) #10

Make sure you’re passing the right collision type to collide(), and try increasing the y offset from 1 in case your player isn’t directly on top of it for some reason.


(Nate ) #11

Yes the collision is taking place I am tracing out a confirmation message. The platform just keeps on scrolling leaving the player in the dust.


(Nate ) #12

This is my update method from the platform class:

if (!isHit)//if false move left
			{
				x -= power;
			}
			
			
			if (isHit)//if true move right
			{
				x += power;
			}
			
			
			
			 if (collide("level", x - 5,  y) && isHit == false)
			{
				isHit = true;
				
			}

			if (collide("level", x + 5,  y) && isHit == true)
			{
				isHit = false;

			}
			
			
			//player interaction

			
			var _x:Number = x, _y:Number = y;
			
			var p:Entity = collide("player", x, y - 1);
			
			if (p)
			{
				p.x += _x - x;
				p.y += _y - y;
				
				trace("character is colliding within theMovingPlatform_Horizontal");
			}

			
	}

(Jacob Albano) #13

What do you expect this to do:

var _x:Number = x, _y:Number = y;
p.x += _x - x;
p.y += _y - y;

You aren’t saving _x and _y (the last position the platform is at) before you move the platform, so _x - x == 0 and _y - y == 0; therefore the player’s position is never changed. You need to save the current position before you move the platform, as in my example.


(Nate ) #14

Thanks Jacob! I didn’t think the logical pipeline mattered much in the Update method since I thought everything just fired per elapsed frame. That being said, I moved the variable assignments above the move code and it is responding the same way. Do you have any other thoughts?


(Jacob Albano) #15

The sequence of functions in update() does matter, very much so. Even though each update() is being called in an order you can’t control, the code inside the update will behave completely differently if you get something out of place. It makes no sense to operate under the assumption that modifying a variable before using it will produce the same result as modifying it after the fact.

Can you show me your current code? And did you try putting my exact code into the entity to see if it worked?


(Nate ) #16

This is my current movingplatform class update function

	//player interaction

			
			var _x:Number = x;
			var _y:Number = y;
			
			var p:Entity = collide("player", x, y - 1);
			
			if (p)
			{
				p.x += _x - x;
				p.y += _y - y;
				
				trace("character is colliding within theMovingPlatform_Horizontal");
			}
		
		
		if (!isHit)//if false move left
			{
				x -= power;
			}
			
			
			if (isHit)//if true move right
			{
				x += power;
			}
			
			
			
			 if (collide("level", x - 5,  y) && isHit == false)
			{
				isHit = true;
				
			}

			if (collide("level", x + 5,  y) && isHit == true)
			{
				isHit = false;

			}
			
			
	

			
	}

(Jacob Albano) #17

No, no, no. You aren’t understanding what I’m saying. You’ve still got the order of events all wrong.

  1. Store the current platform position
  2. Move the platform
  3. Check to see if the player is colliding with the platform.
  • If he is, move him by (_x - x, _y - y).

if { _x, _y } is the last position of the platform, and { x, y } is the current position after moving it, { _x - x, _y - y } is the amount to move the player so that he is carried along with the movement. The order is very important.


(Nate ) #18

Okay thanks for the clarification Jacob! I got it all working fine now!

One thing I had to change was in yours you had:

p.y += _y - y;
p.x += _x - x;

I had to make it p.y -= and p.x -= to make the player scroll with the platform in the correct direction!

Thanks again Jacob!


(Jacob Albano) #19

Oops, I did have that wrong. It should have been:

p.x += x - _x;
p.y += y - _y;