Hi there,
A few friends and me are currently working on a Jump and Run game for school and used existing code for the Player class.
The gravity has an acceleration wich counts +1 per "round".
It can just test if the player is currently standing on (or in) an Object.
So the Problem is that the player most of the time is, because of the steps, in an object and then stopping, but we want it to stop direktly above.
So our idea is to test after each "round" each place until the "future position" under the player and then decrease the vertikalspeed (vspeed) to the difference of the current position and the detected-solid-position.
This is the code i wrote, but it doesn't work.
Perhaps one of you guys can help me,
Sorry for any language or grammar mistakes,
Jonah
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Player extends Actor { private int speed = 8; private int vspeed = 0; private int acceleration = 1; private int imageheight; private int testcount = 0; public void act() { imageheight= getImage().getHeight() / 2; checkKeys(); checkFall(); } private void checkKeys() { if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("space")) { jump(); } if(Greenfoot.isKeyDown("a")) { this.setLocation(getX()- speed,getY()); } if(Greenfoot.isKeyDown("d")) { this.setLocation(getX()+ speed,getY()); } } public boolean onGround()//Methode um zu testen, ob der charakter auf einem objekt steht { Actor under = getOneObjectAtOffset ( 0, imageheight , Solid.class); return under != null; } public void jump() { vspeed = -10;// rückwertung der Fall fall(); } public void fall() { setLocation(getX(),getY()+ vspeed); vspeed = vspeed + acceleration; } public void checkFall() { if(onGround()){ vspeed = 0; }else{ //our Idea /*if(vspeed>0){ for(int i =0;i==vspeed || getOneObjectAtOffset ( 0, imageheight+i, Solid.class)!=null;i++){ testcount++; } vspeed=vspeed-testcount; testcount=0; }*/ fall(); } } }
if(vspeed>0){ for(int i =0;i==vspeed || getOneObjectAtOffset ( 0, imageheight+i, Solid.class)!=null;i++){ testcount++; } vspeed=vspeed-testcount; testcount=0; }