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

2023/5/23

help with if statement

Psg1223 Psg1223

2023/5/23

#
I need help I have an odd object and the getObjects is not finding this object for the if statement what should I change?
Psg1223 Psg1223

2023/5/23

#
Also how do I fix the static string variable to work
Psg1223 Psg1223

2023/5/23

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Chooseworld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Chooseworld extends World
{
    public static String player; 
    /**
     * Constructor for objects of class Chooseworld.
     * 
     */
    public Chooseworld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        showText(" Choose Your Hero",300,50); 
        addObject( new Ninja(), 100, 180 ); 
    }
    public void act(){
        checkClick(); 
    }
    private void checkClick(){
        if(getObjects( Ninja.class).size() == 0 ){
            String player = "Ninja"; 
            Greenfoot.setWorld (new Caveworld());
        }
    }
}
Psg1223 Psg1223

2023/5/23

#
it returns as getobjects is always equaling zero and never changes the string variable
danpost danpost

2023/5/24

#
Psg1223 wrote...
it returns as getobjects is always equaling zero and never changes the string variable
It does set a String variable called player to "Ninja" -- just not the one you think it should. Line 35 declares a new variable called player which is not the variable you declared on line 10. Remove the "String" from line 35 so the one on line 10 gets set.
You need to login to post a reply.