Hello again.
I've been working on a game, have redone a bit, tried to make it simpler, etc, and I've ran into an issue. For my jump code (line 67), it is not working correctly. I will press down W, I will be teleported up by my jump height as usual, but it will instantly make me fall down, instead of slowing me down until I go downwards again. But, if I call the jump code right when the game is ran, once, I will jump as I want to; I move up 15, then 14, then 13, etc, until 0, then I fall down until I hit the ground. I can upload a scenario if you need me to, but I cannot find what is causing this issue.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class GroundBack here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Mover { private int vSpeed; private int gravity = 1; private int jumpStrength = -15; private int animationTimer = 0; public boolean onGround; private int test; public Player() { this.velocity = 5; } /** * Act - do whatever the GroundBack wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment.ii */ public void act() { if (test == 0) { jump(); test = 1; } checkKeys(); checkFall(); onGround(); if(onGround == false) { onGroundLeft(); if(onGround == false) { onGroundRight(); } } failSafe(); platformsAbove(); platformAboveLeft(); platformAboveRight(); } //Keys private void checkKeys() { if(Greenfoot.isKeyDown("d")) { if(getOneObjectAtOffset( getImage().getWidth()/2 + 6, 0, Platforms.class) == null) { moveRight(); setImage("Character Right.png"); } } if(Greenfoot.isKeyDown("a")) { if(getOneObjectAtOffset(getImage().getWidth()/-2 - 6, 0, Platforms.class) == null) { moveLeft(); setImage("Character Left.png"); } } if(Greenfoot.isKeyDown("w")) { if(onGround == true) { jump(); } } } private void jump() { vSpeed = jumpStrength; setLocation (getX(), getY() + vSpeed); } //Collision down public void onGround() { if(getOneObjectAtOffset(0, getImage().getHeight()/2, Platforms.class) != null) { onGround = true; } else { onGround = false; } if(getOneObjectAtOffset(0, getImage().getHeight()/2, Platforms.class) == null) { onGround = false; } else { onGround = true; } } private void onGroundLeft() { if(getOneObjectAtOffset(20, getImage().getHeight()/2, Platforms.class) != null) { onGround = true; } if(getOneObjectAtOffset(20, getImage().getHeight()/2, Platforms.class) == null) { onGround = false; } } private void onGroundRight() { if(getOneObjectAtOffset(-20, getImage().getHeight()/2, Platforms.class) != null) { onGround = true; } if(getOneObjectAtOffset(-20, getImage().getHeight()/2, Platforms.class) == null) { onGround = false; } } private void fall() { setLocation (getX(), getY() + vSpeed); if(vSpeed <= 15) { vSpeed = vSpeed + gravity; } } private void checkFall() { if (onGround == true) { vSpeed = 0; } else { fall(); } } private void failSafe() { if(getOneIntersectingObject(Platforms.class) != null) { setLocation(getX(), getY() - 2); } } //Collision above public boolean platformsAbove() { int spriteHeight = getImage().getHeight(); int yDistance = (int)(spriteHeight/-2); Actor ceiling = getOneObjectAtOffset(0, yDistance, Platforms.class); if(ceiling != null) { vSpeed = 1; bopHead(ceiling); return true; } else { return false; } } public boolean platformAboveLeft() { int spriteHeight = getImage().getHeight(); int yDistance = (int)(spriteHeight/-2); Actor ceilingLeft = getOneObjectAtOffset(getImage().getWidth()/-2 - 2, yDistance, Platforms.class); if(ceilingLeft != null) { vSpeed = 1; bopHead(ceilingLeft); return true; } else { return false; } } public boolean platformAboveRight() { int spriteHeight = getImage().getHeight(); int yDistance = (int)(spriteHeight/-2); Actor ceilingRight = getOneObjectAtOffset(getImage().getWidth()/2 + 2, yDistance, Platforms.class); if(ceilingRight != null) { vSpeed = 1; bopHead(ceilingRight); return true; } else { return false; } } public void bopHead(Actor ceiling) { int ceilingHeight = ceiling.getImage().getHeight(); int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2; setLocation(getX(), newY); } }