Hello,
I want to change the world if my actor touches the right border, how do i do that?
My Actor Class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class DonkeyKong here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class DonkeyKong extends Animal
{
/**
* Act - do whatever the DonkeyKong wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int speed = 5;
private int vSpeed = 5;
private boolean spaceDown;
private int timer = 0;
public static int score = 0;
private GreenfootImage left = new GreenfootImage("DKleft.png");
private GreenfootImage right = new GreenfootImage("DKright.png");
private GreenfootImage up = new GreenfootImage("DKjumping.png");
public DonkeyKong()
{
this.setImage(right);
}
public void act()
{
this.controls();
this.moveVertically();
this.onGround();
this.setLocation(getX(), getY()+vSpeed);
this.touchingBricks();
this.score();
this.brickFix();
this.atWorldEdge();
//if (getOneIntersectingObject(Bricks.class) != null) move(-speed);
//if (getOneIntersectingObject(Bricks.class) != null) move(+speed);
//if (getOneIntersectingObject(Blocks.class) != null) move(-speed);
//if (getOneIntersectingObject(Blocks.class) != null) move(+speed);
}
public void controls()
{
if (Greenfoot.isKeyDown("right"))
{
this.setImage(right);
this.move(speed);
}
if (Greenfoot.isKeyDown("left"))
{
this.setImage(left);
this.move(-speed);
}
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
spaceDown = true;
Weapons bullet = new Weapons();
getWorld().addObject(bullet, getX() + 60, getY() - 15);
if (this.getImage() == left) bullet.setRotation(180);
else if (this.getImage() == up) bullet.setRotation(270);
}
if (spaceDown && !Greenfoot.isKeyDown("space"))
{
spaceDown = false;
}
if (this.isTouching(Enemies.class))
{
getWorld().removeObject(this);
}
}
public boolean onGround()
{
int height = getImage().getHeight();
Actor ground = getOneObjectAtOffset(5, height/2 + 5, Bricks.class);
return ground != null;
}
public void touchingBricks()
{
if (!getIntersectingObjects(Bricks.class).isEmpty())
{
this.setLocation(getX(), getY()-5);
//this.setImage("DKright.png");
}
}
public void moveVertically()
{
if (onGround() && Greenfoot.isKeyDown("up"))
{
this.setImage(up);
this.setLocation(getX(), getY()-300);
}
}
public void score()
{
if (this.isTouching(Blocks.class))
{
this.score++;
}
}
public void brickFix()
{
if (isTouching(Bricks.class) && (getImage().equals(right)))
{
move(-speed);
}
if (isTouching(Bricks.class) && (getImage().equals(left)))
{
move(speed);
}
if (isTouching(Blocks.class) && (getImage().equals(right)))
{
move(-speed);
}
if (isTouching(Blocks.class) && (getImage().equals(left)))
{
move(speed);
}
if (isTouching(Blocks.class) && (getImage().equals(up)))
{
this.setLocation(getX(), getY()+250);
}
}
}


