Hello!
So far, my actor rebounds from a wall if he JUMPS INTO it, however if he WALKS INTO it then he goes right through?
Would you be able to help me to get him to rebound if he WALKS INTO it?
Thanks!
Here's the code having to deal with moving and jumping:
And the other one:
ublic void act()
{
if(inTheAir) //If we're in the air
{
fall(); //we'll fall
} else //If we're not
{
if(Greenfoot.isKeyDown("left")) //If they press the left key
{
deltaX = -5; //deltaX will move us to left
} else if(Greenfoot.isKeyDown("right")) //If they press the right key
{
deltaX = 5; //deltaX will move us right
} else //If neither key is down
{
deltaX=0; //Don't move at all
}
if(Greenfoot.isKeyDown("up")) //If we press the up key
{
deltaY+=5; //Add 15 to deltaY to jump
}
}
move();
} public void move()
{
int newX = getX() + deltaX; // Set the new x and y coordinates by adding or subtracting the deltaX
int newY = getY() - deltaY; // and deltaY. newX and newY may change if we're on the ground or a platform.
Actor platformBelow = getOneObjectAtOffset(0, groundHeight+5, Platform.class); //Look for a platform below us
Actor platformAbove = getOneObjectAtOffset(0, -groundHeight-5, Platform.class); //Look for a platform above us
Actor platformToRight = getOneObjectAtOffset(sideWidth+5, 0, Platform.class); //Look for a platform to right of us
Actor platformToLeft = getOneObjectAtOffset(-(sideWidth+5), 0, Platform.class); //Look for a platform to left of us
if(platformBelow!=null) //If we find a platform below us
{
if(deltaY<0) //If we are falling
{
deltaY=0; //Stop falling
inTheAir = false; //Declare we're on the ground
GreenfootImage platformImage = platformBelow.getImage(); //Get the image of the platform
int topOfPlatform = platformBelow.getY()-platformImage.getHeight()/2; //Find the top of platform
newY = topOfPlatform - groundHeight; //Put our feet at the top of the platform
}
} else if(getY() >= worldHeight - groundHeight) { //If we hit the ground...
if(deltaY<0) //and if we're falling...
{
deltaY=0; //Stop falling
inTheAir = false; //Declare we're on the ground
newY = worldHeight - groundHeight; //Put bottom of actor right on ground
}
} else //If there's no platform below
{
inTheAir = true; //Then we will be in the air
}
if (platformAbove!=null) //If there's a platform above us
{
if(deltaY>0) //If we're going up
{
deltaY=0; //Stop going up
GreenfootImage platformImage = platformAbove.getImage(); //Get the impage of the platform
int bottomOfPlatform = platformAbove.getY()+platformImage.getHeight()/2;//Find the bottom of the platform
newY = bottomOfPlatform + groundHeight; //Put the top of our head at the bottom
}
}
if(getX()<=sideWidth)
{
deltaX=Math.abs(deltaX);
}
if(getX()>=worldWidth-sideWidth)
{
deltaX=Math.abs(deltaX)*-1;
}
if (platformToRight != null) //If there's a platform to the right
{
deltaX = Math.abs(deltaX)* -1; //Bounce to the left
}
if (platformToLeft != null) //If there is a platform to the right
{
deltaX = Math.abs(deltaX); //Bounce to the right
}
