For my AP Computer Science final we are supposed to make a game using Greenfoot. My game involves jumping/falling, but I can't get it to work.
I've watched the video, but when I try it like he does my person goes through the floor. If I change it, he either keeps going through it, or he doesn't fall at all. And with jumping, they just go straight up and fall, I can't figure out how to make it to where the player has time to move while jumping.
Here is the code I've been using.
Me and my teacher played around with it for a while, so there are some unused variables in there, but if they help you by all means do whatever.
import greenfoot.*; public class Player2 extends Actor { private int vSpeed = 10; private int acceleration = 1; private int jumpSpeed = 0; private boolean goingLeft = true; GreenfootImage gr = getImage(); private int count = 0; public void act() { checkKeys(); checkFall(); count++; } public void checkKeys() { if(Greenfoot.isKeyDown("left")) { if(goingLeft) { gr.mirrorHorizontally(); goingLeft=false; } this.setLocation(getX()-2,getY()); } if(Greenfoot.isKeyDown("right")) { if(!goingLeft) { gr.mirrorHorizontally(); goingLeft=true; } this.setLocation(getX()+2,getY()); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public boolean onGround() { /*if(getX()<gr.getHeight()-100) { return true; } return false;*/ Actor underG = getOneObjectAtOffset(0,1,Ground.class); return underG != null; } public void stopfall() { } public void fall() {if(count%2==0){ if(!onGround()) { vSpeed= vSpeed + 2; this.setLocation(getX(), getY() + vSpeed); }} } public void jump() { if(onGround()) { jumpSpeed = -60; this.setLocation(getX(), getY() + jumpSpeed); } } private boolean verSpeed() { if(vSpeed>=1) { return true; } else { return false; } } }