Ok so, I'm trying right now to make a general code for jumping, because my game will have many heroes. I'm having some problems here, and I don't quite understand why. Whenever the "up" is triggered, the Actor won't jump and I don't know if it's because of this part of code or smth else. I'm new to Greenfoot, so any advice would be highly appreciated ^_^.
And also, here's the onGround() :
The "Ground" is divided in some rocks floating and I didn't find any other way to see if it's on the ground itself or on the rocks. I'm thinking that I should separate every rock but will it be truly useful? What can I do to make it simpler? Thanks in advance!! :D
Edit: It'll be a pixel game, that's why I'm using very precise numbers
private int velocity=0;
public void jump(int jumpStrength)
{
velocity++;
setLocation(getX(), getY()+velocity);
if(velocity>jumpStrength)
{
velocity=-jumpStrength;
}
}
public void movement(int x, int y, int speed, int jumpStrength)
{
if(Greenfoot.isKeyDown("up") && !atTopEdge(getY()) && onGround())
{
velocity=0;
jump(jumpStrength);
}
if(Greenfoot.isKeyDown("left") && !atLeftEdge(x))
{
setLocation(x-speed,y);
}
if(Greenfoot.isKeyDown("right") && !atRightEdge(x))
{
setLocation(x+speed,y);
}
} public boolean onGround()
{
//x(ground & floating rocks) 482 370 310 235 106
switch(getX()+getImage().getHeight()/2+5)
{
case 482: return true;
case 370: return true;
case 310: return true;
case 235: return true;
case 106: return true;
default: return false;
}
}
