Hey all!
Im making a platformer and i have my gravity in it but i want to be able for the character to stop when it hits a block
heres the code hopefully you can help!
public void act()
{
checkKeys();
kill();
Actor grounddd = getOneIntersectingObject(grounddd.class);
Actor block = getOneIntersectingObject(block.class);
int groundLevel = 520 - getImage().getHeight()/2;
boolean onGround = (getY() == groundLevel);
if (!onGround) // in middle of jump
{
ySpeed++; // adds gravity effect
setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
if (getY()>=groundLevel) // has landed (reached ground level)
{
setLocation(getX(), groundLevel); // set on ground
Greenfoot.getKey(); // clears any key pressed during jump
}
}
else // on ground
{ if(isTouching(block.class)){
setLocation( getX(), getY());
}
if ("space".equals(Greenfoot.getKey())) // jump key detected
{ Greenfoot.playSound("jump.mp3");
ySpeed = -20; // add jump speed
setLocation(getX(), getY()+ySpeed); // leave ground
}
}
if(canSee(wall.class)){
setLocation( getX() - speed, getY() );
}
