c# - How do I fix my platformer collision detection? -
i'm working on game allows 2d sprite jump on platform when lands on platform, 2d sprite not on top of platform, exceeding or same position of platform.
//here code: texture2d jumper; texture2d tile; vector2 position, velocity; rectangle top; rectangle tile_rectangle; rectangle jumper_rectangle; keyboardstate ks; bool jump; //here load content: protected override void loadcontent() { jump = true; position.x = 10; position.y = 10; jumper = content.load<texture2d>("character"); tile = content.load<texture2d>("tile"); tile_rectangle = new rectangle(300, 350, tile.width, tile.height); top = new rectangle(tile_rectangle.x, tile_rectangle.y - 16, tile_rectangle.width, 10); } //here update: protected override void update(gametime gametime) { ks = keyboard.getstate(); position += velocity; float = 1; if (ks.iskeydown(keys.up) && jump == false) { position.y -= 10f; velocity.y = -25f; jump = true; } if (jump == true) { velocity.y += 2f * i; } if (position.y > 400) { jump = false; } if (jump == false) { velocity.y = 0f; } boundingbox(); if (ks.iskeydown(keys.right)) { position.x += 5f; velocity.x = +0.05f; if (jumper_rectangle.left > tile_rectangle.right) { jump = true; } } if (ks.iskeydown(keys.left)) { position.x -= 5f; velocity.x = -0.05f; if (jumper_rectangle.right < tile_rectangle.left) { jump = true; } } jumper_rectangle = new rectangle((int)position.x, (int)position.y, jumper.width, jumper.height); boundingbox(); base.update(gametime); } //here draw: protected override void draw(gametime gametime) { graphicsdevice.clear(color.cornflowerblue); spritebatch.begin(); spritebatch.draw(jumper, jumper_rectangle, color.white); spritebatch.draw(tile, tile_rectangle, color.white); spritebatch.end(); base.draw(gametime); } //here method on landing: public void boundingbox() { if (jumper_rectangle.intersects(top)) { if (jumper_rectangle.bottom > top.top) { position.y--; jump = false; } } }
where did go wrong or there other way?
Comments
Post a Comment