So i want that if my actor touches the class "Key.class" that he teleports to a new world
Thanks in advance!
import greenfoot.*;
public class Who extends Actor
{
//class constants
static final int GRAVITY = 2;
static final int JUMP_FORCE = 30;
int xSpeed = 4;
int ySpeed = 0;
GifImage RENNEN_RECHTS = new GifImage ("rennen_nach_rechts.gif");
GifImage RENNEN_LINKS = new GifImage ("rennen_nach_links.gif");
GifImage STANDARD = new GifImage ("gif_rechts.gif");
public void act()
{
moveHorizontally();
moveVertically();
teleport();
}
private void moveHorizontally()
{// get values needed for method
int worldWidth = getWorld().getWidth();
int myWidth = getImage().getWidth();
int dx = 0; //for direction of movement
//determine direction of movement
if (Greenfoot.isKeyDown("A")) dx--;
if (Greenfoot.isKeyDown("D")) dx++;
//move actor
setLocation(getX()+dx*xSpeed,getY());
//set image here using for dx
if (dx ==-1)setImage(RENNEN_LINKS.getCurrentImage());
if (dx == 1)setImage(RENNEN_RECHTS.getCurrentImage());
if (dx == 0)setImage(STANDARD.getCurrentImage());
//check world edges(left and right)
if (getX() < myWidth/2) setLocation(myWidth/2,getY());
if (getX() > worldWidth-myWidth/2) setLocation(worldWidth-myWidth/2,getY());
//check for and move back off any obstacles
Actor obstacle = getOneIntersectingObject(null);
while (obstacle != null)
{ setLocation(getX()-dx,getY());
obstacle = getOneIntersectingObject(null);
}
}
private void moveVertically()
{// get values needed for method
int worldHeight = getWorld().getHeight();
int myHeight = getImage().getHeight();
boolean onGround = false; //for can jump state
//fall
ySpeed += GRAVITY;
setLocation(getX(),getY()+ySpeed);
//check for world edge (bottom)
if (getY() > worldHeight-myHeight/2)
{setLocation(getX(),worldHeight-myHeight/2);
ySpeed = 0;
onGround = true;
}
int dy = (int)Math.signum(ySpeed); //direction of movement
Actor obstacle = getOneIntersectingObject(null);
while (obstacle != null)
{setLocation(getX(),getY()-dy);
obstacle = getOneIntersectingObject(null);
ySpeed = 0;
if (dy >0) onGround = true;
}
//check jump
if (onGround && Greenfoot.isKeyDown("SPACE"))ySpeed=-JUMP_FORCE;
}
private void teleport()
{
if (isTouching(Key.class))
{
Greenfoot.setWorld(new SZENE2());
}
}
}

