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

2020/5/11

Accessing an integer from another class

scaps scaps

2020/5/11

#
Hello, I want to make it so that when a green dies it adjusts a integer from another class (the spawner), and makes it go up by one digit. Spawner:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Spawner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spawner extends Actor
{
    /**
     * Act - do whatever the Spawner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int zombies2kill = 5;
    public int zombieskilled= 0;
    public int zombies2spawn = 0;
    private int delay = 0;
    public void act() 
    {
        if(delay > 0){
            delay--;
        }
        
        if(delay == 0 && zombies2spawn <= zombies2kill) spawn();
        
    }    
    
    private void spawn(){
        
        int lr = Greenfoot.getRandomNumber(3);
        if(lr==2){
            Green2 green2 = new Green2();
            getWorld().addObject(green2,580,380);
            zombies2spawn++;
            delay = 80;
        }
        if(lr==1){
            Green green = new Green();
            getWorld().addObject(green,0,380);
            zombies2spawn++;
            delay = 80;
        }
    }
    }

Green:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class green here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Green extends Actor
{
    /**
     * Act - do whatever the green wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
        
       
    public void act() 
    {
        move(1);
        if(isTouching(projectile_1.class)){
            removeTouching(projectile_1.class);
            getWorld().removeObject(this);
            
        }
    }    
}
scaps scaps

2020/5/11

#
The integer I want it to change is zombieskilled. Forgot to mention that.
Rocky13 Rocky13

2020/5/11

#
you can move the variable into the world class and then access it from there. You'll have to create a world variable like this
World world=(/*the specific name of your world class*/) getworld();
Rocky13 Rocky13

2020/5/11

#
this is assuming that you have one instance of class "spawner"
danpost danpost

2020/5/11

#
scaps wrote...
I want to make it so that when a green dies it adjusts a integer from another class (the spawner), and makes it go up by one digit. << Code Omitted >>
Here is something I see often -- actually, it is not from another class, but from another object (or actor instance, in this case). You will need a reference to the Spawner object that is in your world before you can work with its field value.
scaps scaps

2020/5/11

#
how can I do all that jazz?
scaps scaps

2020/5/11

#
I went ahead and moved integer zombieskilled to MyWorld.
You need to login to post a reply.