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

2020/5/13

Continuation: accessing integers in other classes

scaps scaps

2020/5/13

#
Hello, I recently finished looking at the tutorial for this matter, however it is still not yielding the results I need, I rlly need help with this. Here is the code. Spawner (important code in act):
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.
     */
    World world=(MyWorld) getWorld();
    public int zombies2kill = 5;
    public int zombieskilled = 0;
    public int zombies2spawn = 0;
    private int delay = 0;
    
    
    public void act() 
    {
        if(delay > 0){
            delay--;
        }
        
        if (zombieskilled == 1){
            Green2 green2 = new Green2();
            getWorld().addObject(green2,200,100);
            
        }
        
        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;
        }
        
    }
    
    }

Zombies/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)){
            MyWorld world = (MyWorld) getWorld();
            Spawner spawner = world.getSpawner();
            spawner.zombieskilled++;
            
        }
    }    
}
MyWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    private Spawner thespawner;
    public MyWorld()
    {    
        super(600, 400, 1); 
        spawnactor();
        thespawner = new Spawner();
    }
    public Spawner getSpawner(){
        
        return thespawner;
        
    }
    private void spawnactor(){
        Play play = new Play();
        addObject(play,300, 200);
    }
}
danpost danpost

2020/5/13

#
You might want to add thespawner into the world.
scaps scaps

2020/5/13

#
ohh, the play button adds a spawner, but does it have to be the spawner. Play:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class play here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Play extends Actor
{
    /**
     * Act - do whatever the play wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)){
            Guy guy = new Guy();
            Spawner spawner= new Spawner();
            getWorld().addObject(guy,300,384);
            getWorld().addObject(spawner,0,0);
            getWorld().removeObject(this);
        }
    }    
}
danpost danpost

2020/5/13

#
scaps wrote...
ohh, the play button adds a spawner, but does it have to be the spawner. << Code Omitted >>
It has to be the MyWorld instance thespawner.
scaps scaps

2020/5/13

#
it worked thanks a million
You need to login to post a reply.