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

2020/2/29

If the mouse is clicked, place a new image

DaRafster DaRafster

2020/2/29

#
Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StartGameButtonGuessThatPokemon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StartGameButtonGuessThatPokemon extends Actor
{
    GreenfootSound who = new GreenfootSound("Who's that Pokemon.mp3");
    /**
     * Act - do whatever the StartGameButtonGuessThatPokemon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        startGame();
    }    
    
    public void startGame()
    {
        if(Greenfoot.mouseClicked(this))
        {
            who.play();
            getWorld().removeObjects(getWorld().getObjects(StartGameButtonGuessThatPokemon.class));
            getWorld().addObject(new Golbat(),getWorld().getWidth()/2,getWorld().getHeight()/2);
        }
    }
}
Within the if statement, I made it so if you click the "start game" image it would remove it. Afterward, I tried adding an object into the world (a Golbat) which has an image of a Golbat Then I get an error within my terminal window saying:
java.lang.NullPointerException
	at StartGameButtonGuessThatPokemon.startGame(StartGameButtonGuessThatPokemon.java:27)
	at StartGameButtonGuessThatPokemon.act(StartGameButtonGuessThatPokemon.java:18)
I know I'm definitely pulling a beginner mistake, so when I click the "start game" image, how would I add a Golbat image in the middle of the world screen?
danpost danpost

2020/2/29

#
The getWorld method returns the value of the field:
private World world;
in the Actor class:
public World getWorld()
{
    return world;
}
The field is used to indicate what world, if any, the actor is in. While the actor is not in any world, its value will be set to null. Once you execute line 26, which removes the button from the world, the button loses its reference to the world it was in. So, it would be impossible to add an object into that world at that point. Either add the object into the world before removing the button or save a reference to the world locally before removing the button. Also, you can more simply remove the button with the following line:
getWorld().removeObject(this);
as opposed to the line you are using above.
DaRafster DaRafster

2020/2/29

#
danpost wrote...
The getWorld method returns the value of the field:
private World world;
in the Actor class:
public World getWorld()
{
    return world;
}
The field is used to indicate what world, if any, the actor is in. While the actor is not in any world, its value will be set to null. Once you execute line 26, which removes the button from the world, the button loses its reference to the world it was in. So, it would be impossible to add an object into that world at that point. Either add the object into the world before removing the button or save a reference to the world locally before removing the button. Also, you can more simply remove the button with the following line:
getWorld().removeObject(this);
Thank you, worked out perfectly, I should have put more thought into it! Will keep working on my understanding of java, thanks for the help!
You need to login to post a reply.