I'm working on making a simple game, and part of controlling the player I just can't het to work. Heres the code. I've been messing with it for a while and I can't get it to work the way I want it too. I would also like to implement a floor, where the Player's character stops falling upon coming in contact with an object or a certain Y value.
Any suggestions and edits are much appreciated. Examples of code similar to this could also be helpful for learning. Probably a simple fix, I'm a code noob :)
public class Player extends Actor
{
private int dy=0;
private int dg=1;
public Player()
{
setImage("hombre1.png");
}
/**
* Runs on act.
*/
public void act()
{
addGravity();
doControls();
changeY();
}
/**
* Exponentially increases the change in y (dy) of the object. Basically Gravity, but can be set negative to jump.
*/
public void addGravity()
{
dy = dy + dg;
}
/**
* Moves the player left, right, and changes the dy variable to negative to give the effect of a jump.
*/
public void doControls()
{
if (Greenfoot.isKeyDown("up"))
{
dy=-5;
}
if (Greenfoot.isKeyDown("left"))
{
move(-3);
}
if (Greenfoot.isKeyDown("right"))
{
move(3);
}
}
/**
* Changes Y of the player by adding variable dy to existing y value.
*/
public void changeY()
{
setLocation(getX(),getY()+dy);
}
}

