@James_Maslaki wrote:
Hi! So here's the problem. I have a list of platforms, and a list of players. When I, the first and only player added, jump onto the last added platform, it lands perfectly. Right? Then, if I jump on ANY of the previous platforms before the last one, it just slowly sinks through, but the fourth(last) platform is landed on perfectly, for some reason(I can confirm it's the last platform by adding only two platforms and testing the second and first).
Here's the important source code:
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); playerSprite = Content.Load<Texture2D>("sprite"); platformSprite = Content.Load<Texture2D>("platform"); Platforms.Add(new platform(platformSprite, new Vector2(30, 400))); Platforms.Add(new platform(platformSprite, new Vector2(350, 300))); Platforms.Add(new platform(platformSprite, new Vector2(700, 350))); Platforms.Add(new platform(platformSprite, new Vector2(900, 350))); } protected override void Update(GameTime gameTime) { if (Players.Count < 1) { player player = new player(playerSprite, "Jimmy", 1, 1, font); Players.Add(player); } foreach(player player in Players) { player.Update(gameTime); foreach (platform platform in Platforms) { platform.Update(gameTime); if (player.hitbox.Intersects(platform.hitbox)) { player.Velocity.Y = 0f; player.hasJumped = false; } else { player.hasJumped = true; } } } static class RectangleHelper { const int penetrationMargin = 5; public static bool isOnTop(this Rectangle r1, Rectangle r2) { return (r1.Bottom >= r2.Top - penetrationMargin && r1.Bottom <= r2.Top + (r2.Height/2) && r1.Right >= r2.Left + 5 && r1.Left <= r2.Right - 5); }
}
Here's the code for player.cs:
if (k.IsKeyDown(Keys.W) && hasJumped == false) { Position.Y -= 10f; Velocity.Y = -5f; hasJumped = true; } if (hasJumped == true) { float i = 1; Velocity.Y += 0.15f * i; } if (hasJumped == false) { Velocity.Y = 0f; }
And platform.cs:
hitbox = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height); //it's a Rectangle
So yeah, I don't know what's wrong. Any solutions? Thanks for reading! And I got this code from: https://www.youtube.com/watch?v=TlHSNjeND9s
If you need any more context or more source code, let me know, please and thank you.
Posts: 7
Participants: 4