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

2016/6/7

Putting a actor in another world (text)

1
2
KrystalLo KrystalLo

2016/6/7

#
Hey, I've been trying to put a simple text into my gameover code but I don't know how to switch worlds so I thought I'll just put in the code myself, but it doesn't appear on the screen. I also want to put in the score that the player has got from the game, and so I want to put the counter into the gameover world as well but I'm not too sure on how to display the previous score that the player had gotten before s/he died.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gameover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gameover extends World
{

    /**
     * Constructor for objects of class Gameover.
     * 
     */
    public Gameover()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(659, 483, 1); 
    }
    private void prepare(){
        Gameover1 gameover1 = new Gameover1();
        addObject(gameover1,350,219);
    }
    public void act(){
    if(Greenfoot.isKeyDown("esc"))
            Greenfoot.setWorld(new Startmenu());
    }
}
I also want it so when your on gameover world you can press 'esc' to go back to the startmenu world but It doesn't work.
SPower SPower

2016/6/7

#
First, nowhere do you call prepare(), so it's obvious that the code in there never gets executed. You might as well just copy the contents of prepare and paste in in the constructor. As for the score, you'll want to pass it into the Gameover constructor like so:
public Gameover(int score)
{
    // stuff
}
And then display that the way you'd like. When going to the 'game-over screen', you then have to pass the score:
Greenfoot.setWorld(new Gameover(counter.getScore()) );
the counter.getScore() would be your way of getting the score of the player.
KrystalLo KrystalLo

2016/6/7

#
Alright, I'll put the counter in afterwards. But how do I make it so when the game is finished and it moves to the Gameover code how can i make it so when I press it'll go back to the start menu world? Because its currently not working.
SPower SPower

2016/6/7

#
If you want to go back to a pre-existing world (so that you restore the state of that world), you can pass that in the constructor as well, and then set the world to that world, e.g.:
private World previousWorld; // you don't need to know what type it is, other than simply 'World'

public GameOver(int score, World previousWorld)
{
    this.previousWorld = previousWorld;
    // stuff
}

// in the place where you want to restore the previous world:
Greenfoot.setWorld(previousWorld);
KrystalLo KrystalLo

2016/6/7

#
So something like this?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gameover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gameover extends World
{
    private World Startmenu;
    /**
     * Constructor for objects of class Gameover.
     * 
     */
    public Gameover(int score, World previousWorld)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(659, 483, 1); 
        Gameover1 gameover1 = new Gameover1();
        addObject(gameover1,350,219);
        this.Startmenu = Startmenu;
    }
    public void act(){
        if(Greenfoot.isKeyDown("esc"))
                Greenfoot.setWorld(Startmenu);
    }
    }
SPower SPower

2016/6/7

#
No, because you're now setting a variable to itself in line 22. What you want to do is assign the value to it given by the parameter previousWorld:
this.Startmenu = previousWorld;
Also, it is advisable not to start variable names with Uppercase letters, so startMenu would be a better name than StartMenu.
KrystalLo KrystalLo

2016/6/7

#
It says that the previousWorld code cannot be found. Should I be doing this code in the startMenu world? And/or am I doing something incorrectly?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gameover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gameover extends World
{
    private World startMenu;
    /**
     * Constructor for objects of class Gameover.
     * 
     */
    public Gameover(int score, World previousWorld)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(659, 483, 1); 
        Gameover1 gameover1 = new Gameover1();
        addObject(gameover1,350,219);
        this.startMenu = previousWorld;
    }
    public void act(){
        if(Greenfoot.isKeyDown("esc"))
                this.startMenu = previousWorld;
    }
    }
SPower SPower

2016/6/7

#
In the act method you should not re-initialise the startMenu, but do Greenfoot.setWorld(startMenu).
danpost danpost

2016/6/7

#
KrystalLo wrote...
how can i make it so when I press it'll go back to the start menu world? Because its currently not working.
The String representation of the key is not "esc" -- it is "escape". Make that change so Greenfoot will look at that key and change worlds when hit.
KrystalLo KrystalLo

2016/6/7

#
My pirate code now has an error, it is saying that the code new gameOver, constructor gameOver in class gameOver cannot be applied to given types; and says that it requires a int, Greenfoot.World
        if (isTouching(Bomb.class)){
            removeTouching(Bomb.class);
            iHurt++;
            Greenfoot.playSound("explode.wav");
            setImage("pirate"+direction+iHurt+".png");
            if(iHurt>3){
                gameOver g = new gameOver();
                Greenfoot.setWorld(g);
            }
        }
SPower SPower

2016/6/7

#
That's because you're not passing in the arguments into the constructor.
KrystalLo KrystalLo

2016/6/7

#
Sorry put how do I pass the argument to the constructor? Sorry, I'm seriously blank right now and can't think properly as I'm tired. Would that mean i'll need to put in a int and/or a Greenfoot.World argument?
SPower SPower

2016/6/7

#
Yes.
GameOver g = new GameOver(score, this);
Greenfoot.setWorld(g);
danpost danpost

2016/6/7

#
Instead of passing everything to the Gameover world, you could just move the score object to it from where you created it:
Gameover g = new Gameover();
g.addObject(counter, go.getWidth()/2, 80);
Greenfoot.setWorld(g);
I do not believe you want to go back to the world where the game has finished -- and it does not matter if the start menu is a new World object or not; so, using 'Greenfoot.setWorld(new Startmenu());' should be sufficient in the Gameover world for when the "escape" key is pressed.
KrystalLo KrystalLo

2016/6/7

#
Sorry, i'm not understanding anything. T-T Is this what you want me to do?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pirate here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pirate extends Actor
{
    int moveBy = 3;
    int iCounter =0;
    int iHurt =1; 
    private String direction = "right";
    public Pirate(Counter myCounter){
        setImage("pirate"+direction+iHurt+".png");
    }
    /**
     * Act - do whatever the Pirate wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        //sounds & music gotten from http://freesound.org/
        checkKeys();
        setImage("pirate"+direction+iHurt+".png");

        if (isTouching(Bomb.class)){
            removeTouching(Bomb.class);
            iHurt++;
            Greenfoot.playSound("explode.wav");
            setImage("pirate"+direction+iHurt+".png");
            if(iHurt>3){
                gameOver g = new gameOver();
                g.addObject(counter, go.getWidth()/2, 80);
                Greenfoot.setWorld(g);
            }
        }
        if (isTouching(Coin.class))
        {
            Coin coin = (Coin)getOneIntersectingObject(Coin.class);
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(coin.getPoints());
            getWorld().removeObject(coin);
            Greenfoot.playSound("coins.wav");
        }
    }   

    private void checkKeys(){
        if(Greenfoot.isKeyDown("right")){
            direction = "right";
            this.setLocation(getX()+moveBy,getY());

        }
        if (Greenfoot.isKeyDown("left")){
            direction = "left";
            this.setLocation(getX()-moveBy,getY());

        }
    }
}
Theirs a error with new gameOver, counter and go.
There are more replies on the next page.
1
2