Hey Greenfoot community,
I have recently started "programming" with Greenfoot 3.0.2 (Java version 1.8.0_66). I am not very good yet so please keep that in mind.
I tried to write a simple "Jump 'n' Run" type of game with basic gravity and collision detection. However I have run into some major problems.
First of all let me add my code:
World Generation:
Player Falling Code:
My problems seem to have something to do with "getObjectAtOffset()". The Method detected Ground in midair and stopped the character.
I had the "Game" on a USB stick and used it on my pc at home where even more problems came into play. The character stopped falling at all and after I tried to reset the programm would just load very long until I closed it. Also in my Player1.class it says "Next Error" in the top left corner, but nothing happens if I press the button.
Many Thanks for helping
Tritos
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(900, 900, 1);
create();
}
public void create()
{
Player1 p1 = new Player1();
addObject(p1, 450, 50);
for(int k = 25; k <= 1000; k = k + 50)
{
Ground g1 = new Ground();
addObject(g1, k, 875);
}
}int vSpeed = 0;
int gravity;
int speed = 10;
int jumpHeight = 20;
int fallAcceleration = 2;
boolean collision;
int collisionLength;
public void act()
{
checkKeys();
checkFall();
}
public void checkFall()
{
if(!isTouching(Ground.class))
{
fall();
}
}
public void fall()
{
collision = false;
for(int i = 0; i <= vSpeed; i = i++)
{
if(!getObjectsAtOffset(getX(), getY() + i, Ground.class).isEmpty())
{
collision = true;
collisionLength = i;
}
}
if(collision)
{
setLocation(getX(), getY() + collisionLength);
}
else
{
setLocation(getX(),getY()+vSpeed);
vSpeed = vSpeed + fallAcceleration;
}
}


