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

2018/12/19

Checking if an object of the same class as others are still in the world.

iCookieMad iCookieMad

2018/12/19

#
Hello, I'm having trouble checking if a specific object, out of a number of other objects of the same actor class, is still in the world (not removed). Any ideas?
danpost danpost

2018/12/20

#
iCookieMad wrote...
Hello, I'm having trouble checking if a specific object, out of a number of other objects of the same actor class, is still in the world (not removed). Any ideas?
Please show what code you are working with and explain in detail (with reference to the code), making clear what you are wanting to do.
iCookieMad iCookieMad

2018/12/20

#
I have tiles at the bottom of my game, and , when a collectible item (Restore.class) is collected, any tiles that have been removed throughout the game will spawn back again. I want to know how to check if each tile has been removed or not, so that I can spawn it back in when the item is collected- or can you think of an easier method? Below is the relevant code from the World class, as well as the code in the Restore class.
import greenfoot.*;

public class NorthPole extends World
{
    private int restoreY;
    private int spawnRestoreDelayCount=800;
    
    public NorthPole()
    {
        super(800, 500, 1);
        prepare();
    }
    
    public void act()
    {
        if(spawnRestoreDelayCount>0) //timer to spawn a restore collectable item
        {
            spawnRestoreDelayCount=spawnRestoreDelayCount-1;
            if(spawnRestoreDelayCount==0)
            {
                spawnRestore();
                spawnRestoreDelayCount=Greenfoot.getRandomNumber(1800)+1400;
            }
        }
    }

    public void prepare()
    {
        Counter counter = new Counter("Presents collected: ");
        addObject(counter, 97, 21);

        Player player = new Player(counter);
        addObject(player, 400, 394);
        
        Platform platform1 = new Platform(); //spawns platforms into the world (the ones I want to check if they have been removed or not)
        addObject(platform1, 40, 453);
        Platform platform2 = new Platform();
        addObject(platform2, 120, 453);
        Platform platform3 = new Platform();
        addObject(platform3, 200, 453);
        Platform platform4 = new Platform();
        addObject(platform4, 280, 453);
        Platform platform5 = new Platform();
        addObject(platform5, 360, 453);
        Platform platform6 = new Platform();
        addObject(platform6, 440, 453);
        Platform platform7 = new Platform();
        addObject(platform7, 520, 453);
        Platform platform8 = new Platform();
        addObject(platform8, 600, 453);
        Platform platform9 = new Platform();
        addObject(platform9, 680, 453);
        Platform platform10 = new Platform();
        addObject(platform10, 760, 453);
        
        Death death = new Death();
        addObject(death, 391, 484);
    }

    private void spawnRestore() //spawns collectable item
    {
        restoreY=Greenfoot.getRandomNumber(200)+70;
        Restore restore = new Restore();
        addObject(restore, 0, restoreY);
    }
}
Restore (collectible item) class code.
import greenfoot.*;

public class Restore extends Actor
{
    
    public void act() 
    {
        checkBottom();
        move(2);
        
        if(isTouching(Bauble.class))
        {
            getWorld().removeObject(this);
            restoreTiles();
        }
    }

    private void restoreTiles() //will restore all removed tiles when called upon
    {
    }
}
danpost danpost

2018/12/20

#
iCookieMad wrote...
I have tiles at the bottom of my game, and , when a collectible item (Restore.class) is collected, any tiles that have been removed throughout the game will spawn back again. I want to know how to check if each tile has been removed or not, so that I can spawn it back in when the item is collected
List the tiles in an Actor array. Use the array for spawning a tile and call getWorld on any tile in the array to check if in world.
iCookieMad iCookieMad

2018/12/20

#
Thanks for the reply! Could you give an example of how I'd list the tiles in an Actor array? I haven't done this before.
danpost danpost

2018/12/20

#
iCookieMad wrote...
Thanks for the reply! Could you give an example of how I'd list the tiles in an Actor array? I haven't done this before.
I do not know anything about your tiles except that they are actors and they are added and removed from the world. What tiles will you have? Are they all the same, different, unique? How many of each are you going to deal with (at one time)? etc.
iCookieMad iCookieMad

2018/12/20

#
danpost wrote...
iCookieMad wrote...
Thanks for the reply! Could you give an example of how I'd list the tiles in an Actor array? I haven't done this before.
I do not know anything about your tiles except that they are actors and they are added and removed from the world. What tiles will you have? Are they all the same, different, unique? How many of each are you going to deal with (at one time)? etc.
They're all the same, and game will start with a total of 10 tiles. During the game , some tiles will be removed, but when you collect a collectable, all the tiles will respawn again. Does this help?
danpost danpost

2018/12/20

#
iCookieMad wrote...
They're all the same, and game will start with a total of 10 tiles. During the game , some tiles will be removed, but when you collect a collectable, all the tiles will respawn again.
Sounds like all you really need is something like this:
for (int i=0; i<10-getWorld().getObjects(Tile.class).size(); i++)
{
    // add one tile into world
}
iCookieMad iCookieMad

2018/12/21

#
Thanks for the reply, but I couldn't use that code (I probably didn't explain what I wanted very well). I came up with another solution where I'd remove and then spawn back in all the tiles, in one frame, so that they would all be restored. Thanks anyway though!
You need to login to post a reply.