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

2015/7/8

Checking if an object is removed

AD99 AD99

2015/7/8

#
Working on my variation of breakout. I have power up balls that randomly spawn underneath the blocks. That's working well. however, I need to check when a block is removed so a ball can drop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void spawnPowerUp()
    {
        if(counter < 3)
        {
        counter++;
        int rand = Greenfoot.getRandomNumber(((Board) getWorld()).getBlockList().size());
        int x = ((Board) getWorld()).getBlockList().get(rand).getX();
        int y = ((Board) getWorld()).getBlockList().get(rand).getY();
        blockList.add(((Board) getWorld()).getBlockList().get(rand));
        powerUpList.add(new PowerUp());
        getWorld().addObject(((PowerUp)powerUpList.get(powerUpList.size()-1)), x, y);
        ((Board) getWorld()).getBlockList().remove(rand);
        }
    }
This is for the random spawning blocks. I am also storing the object locations of both the blocks and powerUp balls in two separate ArrayLists.
1
2
3
4
5
6
7
8
9
10
11
12
public void isTouching()
    {
       for (int i = 0; i < Block.blockList.size(); i++)
       {
           
          int powerUpX = Block.powerUpList.get(i).getX();
          int blockX = Block.blockList.get(i).getX();
          if (blockX != powerUpX)
              Block.powerUpList.get(i).moving = true;
           
       }
    }
Basically this is supposed to check if the X of both the corresponding block and powerup pairs are the same, and if not, give permission for the power up to move. HOWEVER, I am using getWorld.removeObject() to remove the blocks so my method throws an IllegalStateException(accessing removed objects location). So my problem is what happens to the coordinates of the removed object? I believe I previously caught the error for another part of my program since the error was useless, and it seems the coordinates of the removed object remain the same but it's just not visible? Not sure. anyway to get around this? Much appreciated =D
AD999 AD999

2015/7/8

#
Nvm solved the problem. Since I couldn't get them to check locations in the proper order. I gave my Block.class a removed state to check if it is removed or not.
1
2
3
4
5
6
7
8
9
public boolean removed = false;
 
public void act()
    {
      if(isTouching(Ball.class))
      {
          this.removed = true;
          getWorld().removeObject(this);
      }
and to have to the power up drop
1
2
3
4
5
6
7
8
9
public void isTouching()
    {
       for (int i = 0; i < Block.blockList.size(); i++)
       {
          if (Block.blockList.get(i).removed == true)
              Block.powerUpList.get(i).moving = true;
           
       }
}
Maybe I am over complicating things haha
Super_Hippo Super_Hippo

2015/7/8

#
Why do you store the locations in an array? When are the power ups spawning? In most breakout games, there is a chance for each removed block to spawn a power up.
danpost danpost

2015/7/8

#
AD999 wrote...
Maybe I am over complicating things haha
Giving the blocks a removed state is not needed as you can check if it is not in the world with
1
if (Block.blockList.get(i).getWorld() == null)
I really do not understand the need for the ArrayLists for 'pairing' when you can have each powerup retain a reference to its corresponding block and move itself as needed with code in its own 'act' method.
AD999 AD999

2015/7/9

#
Super_Hippo wrote...
Why do you store the locations in an array? When are the power ups spawning? In most breakout games, there is a chance for each removed block to spawn a power up.
Three power ups are spawned randomly underneath the blocks. I can make them spawn when they break I guess and set a limit to the power ups per level.
danpost wrote...
AD999 wrote...
Maybe I am over complicating things haha
Giving the blocks a removed state is not needed as you can check if it is not in the world with
1
if (Block.blockList.get(i).getWorld() == null)
I really do not understand the need for the ArrayLists for 'pairing' when you can have each powerup retain a reference to its corresponding block and move itself as needed with code in its own 'act' method.
Ah yes that's a good idea. Thank you.
You need to login to post a reply.