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

2020/5/13

Removing objects

scaps scaps

2020/5/13

#
Hey im trying to get it so that when the victory method is selected it removes the Guy actor.
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 == zombies2kill)victory();
            
            
       
        
        if(delay == 0 && zombies2spawn < zombies2kill) spawn();
        
    }    
    private void victory(){
        Actor Guy;
        Guy = getOneObjectAtOffset(0,0, Guy.class);
        if (Guy == null){
        getWorld().removeObject(Guy);
    }
    }
    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;
        }
        
    }
    
    }

scaps scaps

2020/5/13

#
The important code is at victory()
danpost danpost

2020/5/14

#
scaps wrote...
The important code is at victory()
Line 38 gets the guy, but only if the guy intersects the center of the Spawner object. I doubt that is what you want. Also, you would not want to try to remove the referenced object if it was indeed null.
scaps scaps

2020/5/14

#
oh so how do I get the guy no matter where he is at?
danpost danpost

2020/5/14

#
scaps wrote...
oh so how do I get the guy no matter where he is at?
You can get the guy by way of the world he is in. However, I do not think you have to go that far. Just have the world remove any and all Guy objects from the world:
getWorld().removeObjects(Guy.class);
scaps scaps

2020/5/14

#
I am getting an error with Guy.class something about incompatible types with java.lang.Class<Guy> cannot be converted to java.util.Collection<?
danpost danpost

2020/5/14

#
scaps wrote...
I am getting an error with Guy.class something about incompatible types with java.lang.Class<Guy> cannot be converted to java.util.Collection<?
Show related codes. No, wait. My fault ... try:
getWorld().removeObjects(getWorld().getObjects(Guy.class));
scaps scaps

2020/5/14

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