Hi guys so this is my first physics game, and right now I am having a little problem with my player. For some reason he can move left, but not right. Also, there is a delay of a second or two before he starts moving, does anyone know how to fix this? Here is the code for the Player.
import greenfoot.*;
public class player extends Actor
{
int ySpeed;
double xSpeed;
double power = 3.5;
int angle = 45;
/**
* Ball2 Constructor: calculates and sets the initial values for the x and y speeds
*
* @param angle: the initial direction of travel for the ball
* @param power: the initial speed of the ball in the direction of travel
*/
/**
* Method act: calculates and sets new location of the ball and removes it upon reaching the right end of the world
*/
public void act()
{
world gameWorld = (world) getWorld();
Platform platform = gameWorld.getPlatformFriction();
int newX = getX() + (int) Math.round(xSpeed); // calculates the new x-coordinate value
int newY = getY() - ySpeed; // calculates the new y-coordinate value
setLocation(newX, newY);
xSpeed += ((double) platform.friction - xSpeed) / 100;
jump();
movement();
reduceYSpeed();
collisionWithPlatform();
}
public void collisionWithPlatform()
{
Actor coll = (Platform) getOneObjectAtOffset(0, 30, Platform.class);
if (coll == null)
{
reduceYSpeed();
}
else
{
ySpeed = 0;
}
}
public void movement()
{
world gameWorld = (world) getWorld();
Platform platform = gameWorld.getPlatformFriction();
if (Greenfoot.isKeyDown("right"))
{
xSpeed += ((double) platform.friction - xSpeed) / 50;
}
else
{
if (!Greenfoot.isKeyDown("left"))
{
xSpeed = 0;
}
}
if (Greenfoot.isKeyDown("left"))
{
xSpeed += ((double) platform.friction - xSpeed) / -50;
}
}
public void jump()
{
}
public void reduceYSpeed()
{
ySpeed--;
}
}