This site requires JavaScript, please enable it in your browser!
Greenfoot back
Ostarius
Ostarius wrote ...

2017/4/21

Actor at Border > Start new World

Ostarius Ostarius

2017/4/21

#
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);
        }  
    }
    
    
}
Super_Hippo Super_Hippo

2017/4/21

#
You will need something like this. Maybe you also need to change the world accordingly to the one which it is in right now.
if (getX() > getWorld().getWidth()-2)
{
    Greenfoot.setWorld( **something** );
}
danpost danpost

2017/4/21

#
The 'atWorldEdge' method of the Animal class will return true for ANY edge. You only want to check a specific one to change worlds. You can use a condition like this:
if (getX() == getWorld().getWidth()-1)
for the condition of the actor reaching the right edge. BTW, the use of 'this.' before calls to non-static method and on non-static fields of the class is optional (usually not used unless there is a naming conflict).
Ostarius Ostarius

2017/4/21

#
It doesn't work :/ I wrote it in the DonkeyKong class Did i do smthng wrong?
 public void changeWorld()
    {
        if (getX() == getWorld().getWidth()-1)
        {
            MyWorld2 mw2 = new MyWorld2();
            Greenfoot.setWorld(mw2);
        }
    }
Ostarius Ostarius

2017/4/21

#
Oh well the thing is, i can't put it in DonkeyKong class I have 8 worlds, i want it to go go the next world every time i touch the right border How do i do that?
Ostarius Ostarius

2017/4/21

#
So i have something like this:
DonkeyKong donkeykong = new DonkeyKong();
        addObject(donkeykong,78,538);
        int dkX = donkeykong.getX();
and this
 public void changeWorld()
    {
        if (dkX == this.getWidth()-1)
        {
            MyWorld2 mw2 = new MyWorld2();
            Greenfoot.setWorld(mw2);
        }
    }
doesn't work, how do i fix it? MyWorld Class
danpost danpost

2017/4/21

#
You will have to systematically determine what type of world the actor is currently in, and proceed to the next accordingly:
World world = null;
if (getWorld() instanceof MyWorld) world = new MyWorld2();
else if (getWorld() instanceof MyWorld2) world = MyWorld3();
else ...
....
....
// afterwards
Greenfoot.setWorld(world);
Ostarius ur prof pic is fugly
Ostarius Ostarius

2017/4/21

#
Works great. Thank you so much both of you!
You need to login to post a reply.