public void act()
{
getDirection(); //direction
move(); //movement
xSensor(); //if passes x coord spawn
}
public void act()
{
getDirection(); //direction
move(); //movement
xSensor(); //if passes x coord spawn
}import greenfoot.*;
public class Player extends Actor
{
final int jSpeed = 30; // the initial 'jump' speed
int ySpeed = 0, xSpeed = 0; // the initial vertical and horizontal speeds
boolean aboutFace; // the direction (left or right) the actor is facing
boolean onGround; // the state of the actor being set on an object or not
/**
* Checks for changes in direction and moves the main actor.
*/
public void act()
{
getDirection();
move();
xSensor();
}
public void xSensor()
{
if (getX() > 400)
{
getWorld().addObject(new controls(), 500, 500);
}
}
/**
* Moves the actor with appropriate image. Checks for obstacles and adjusts
* the position of the actor accordingly.
*/
private void move()
{
ySpeed++; // adds gravity
setLocation(getX()+xSpeed/10, getY()+ySpeed/2);
// check for change in horizontal direction
if((xSpeed>0 && aboutFace) || (xSpeed<0 && !aboutFace))
{
getImage().mirrorHorizontally();
aboutFace = !aboutFace;
}
// check for obstacles
onGround=false; // initialize value
// check below the actor
while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, null)!=null)
{
setLocation(getX(), getY()-1);
onGround=true;
ySpeed=0;
}
// check above the actor
while(getOneObjectAtOffset(0, -getImage().getHeight()/2-1, null)!=null)
{
setLocation(getX(), getY()+1);
ySpeed = 0;
}
// check to right of actor
while(getOneObjectAtOffset(getImage().getWidth()/2+1, 0, null)!=null)
{
setLocation(getX()-1, getY());
xSpeed = 0;
}
// check to left of actor
while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null)
{
setLocation(getX()+1, getY());
xSpeed = 0;
}
}
/**
* Determines any changes in horizontal and vertical speeds for the actor.
*/
private void getDirection()
{
// if (!onGround) return; // if not mid-air changes allowed
// sets requested direction of move, or continues in current direction
if (Greenfoot.isKeyDown("left") && xSpeed>-50){
move(-6);
}// check left
if (Greenfoot.isKeyDown("right") && xSpeed<50){
move(6);
} // check right
if (Greenfoot.isKeyDown("up") && onGround) // check jump
{
ySpeed -= jSpeed; // add jump speed
}
}
}
((SWorld)getWorld()).addObject(/* actor, xLocation, yLocation, */, false);