java - JavaFX intersects collision method detecting collisions when they aren't there -
i'm making infinite side-scrolling platform game, player can move vertically (jump or double jump).
at moment have no sprites, rectangles represent bounds of player , level tiles.
the problem this:
when tile directly above player brick, collision detected , player passes through brick.
i leave controls alone, , player still move through above brick.
also when player jumps , there brick 2 slots above, collision detected 1 slot above, , player falls down.
here relevant code:
private int movealong() { if (hitplayergroundbounds()) { if (playerbounds.getyvel() > 0) { while (hitplayergroundbounds() && !gameover) { playerbounds.sety(playerbounds.gety() + 0.1); } gravity.nojumping(); } else { while (hitplayergroundbounds() && !gameover) { playerbounds.sety(playerbounds.gety() - 0.1); } gravity.resetjumping(); } playerbounds.setyvel(0); } board.settranslatex(boardoffset--); levelboard.updateboards(); if (boardoffset == -format.getcellwidth()) { boardoffset = 0; return 1; } else { return 0; } } private boolean hitplayergroundbounds() { int i, j; bounds bounds, player = playerbounds.getgroundbounds().localtoscene(playerbounds.getgroundbounds().getboundsinlocal()); (i = format.getboardheight() - 1; > 0; i--) { (j = 0; j < format.getboardwidth(); j++) { bounds = boundsboard.getboardij(i, j).localtoscene(boundsboard.getboardij(i, j).getboundsinlocal()); if (levelboard.getboardij(i, j) != 0 && bounds.intersects(player)) { return true; } } } return false; }
here code gravity:
private static int jumping; private static final timeline gravity = new timeline(new keyframe(duration.millis(5), ae -> { playerbounds.sety(playerbounds.gety() - playerbounds.getyvel()); playerbounds.setyvel(playerbounds.getyvel() - 0.1); })); public static int jumping() { return jumping; } public static void resetjumping() { jumping = 0; } public static void nojumping() { jumping = 2; } public static void stop() { gravity.stop(); } public static void initialise() { playerbounds.setyvel(0); gravity.setcyclecount(timeline.indefinite); gravity.play(); } public static void jump() { jumping++; switch (jumping) { case 1: playerbounds.setyvel(5); break; case 2: playerbounds.setyvel(4); break; } playerbounds.sety(playerbounds.gety() - playerbounds.getyvel()); playerbounds.setyvel(playerbounds.getyvel() - 0.1); } public static boolean canjump() { return !(jumping == 2); }
i've been stuck problem on week , it's driving me insane. great, thank you!
Comments
Post a Comment