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

2019/4/8

getObjects() doesnt work

SparrowBoy SparrowBoy

2019/4/8

#
I'm trying to make a game where you can shoot enemies and get to the next Level, through a door which is being added once every Enemy is dead. My problem is that the getObjects doesn't work and I'm not sure why. I even asked my teacher in school but she didn't have an answer to my problem.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class LevelOne extends World
{
    /** Constructor for objects of class MyWorld.*/
    
    public LevelOne()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        populateLevelOne();
    }

    public void populateLevelOne()
    {
        addObject(new Player(), 50, 200);
        addObject(new Enemy(), 400, 300);
    }

    public void detectEnemy()
    {
        showText("detectEnemy", 100, 150);      //conformed method works
        if (getObjects(Enemy.class).isEmpty())
        {
            showText("Added door", 100, 200);   //confirm door added
            addObject(new Door(), 500, 200);
        }
    }
}
I've tried
if (getObjects(Enemy.class).isEmpty())
and
if (getObjects(Enemy.class).size() == 0)
Both didn't work..
Super_Hippo Super_Hippo

2019/4/8

#
"//conformed method works" The method "detectEnemy" isn't called from anywhere in the shown code. It won't execute on its own. I am going to bed, so in case you need additional help, try to go with this one:
import greenfoot.*;

public class Level extends World
{
    private int level;
    
    public Level()
    {
        this(1);
    }
    
    public Level(int level)
    {
        super(600, 400, 1);
        
        //add objects for all levels here (GUI e.g. counters)
        
        switch (level)
        {
            case 1: //add objects for level 1 here
            addObject(new Player(), 50, 200);
            addObject(new Enemy(), 400, 300);
            break;
            
            case 2: //add objects for level 2 here
            //…
            break;
        }
    }

    public void noEnemyAddDoor()
    {
        if (getObjects(Enemy.class).isEmpty())
        {
            addObject(new Door(), 500, 200);
        }
    }
    
    public void nextLevel()
    {
        Greenfoot.setWorld(new Level(level+1));
    }
}
//somewhere you may have something like this
if (isTouching(Enemy.class))
{
    removeTouching(Enemy.class));
    ((Level) getWorld()).noEnemyAddDoor();
    
    //in case it is a bullet or something
    getWorld().removeObject(this);
}
Or, if the Enemy has a method which damages itself, it could look like this:
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
if (enemy != null)
{
    enemy.loseHealth(damage);
}
public void loseHealth(int amount)
{
    health -= amount;
    if (health<1)
    {
        Level level = (Level) getWorld();
        level.removeObject(this);
        level.noEnemyAddDoor();
    }
}
Or, might be better:
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
if (enemy != null)
{
    if (enemy.loseHealth(damage))
    {
        ((Level) getWorld()).noEnemyAddDoor();
    }
}
public boolean loseHealth(int amount)
{
    health -= amount;
    if (health < 1)
    {
        level.removeObject(this);
        return true;
    }
    return false;
}
SparrowBoy SparrowBoy

2019/4/10

#
Thanks for the reply. I've implemented the class Level code. It actually changes the Level but it's being looped at case 1. I'm thinking that setWorld is resetting the whole class and also the integer Level. That way the int level stays 1. Do you have any suggestions to solve that?
Super_Hippo Super_Hippo

2019/4/11

#
I forgot one line. Insert this at line 15:
this.level = level;
SparrowBoy SparrowBoy

2019/4/11

#
Thank you very much! It works now. :D
You need to login to post a reply.