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

2024/1/13

Problem: In an linear array of block objects, how to make each block in the array two hit before being destroyed by a ball object (bad understanding soz)

ChillDude23 ChillDude23

2024/1/13

#
Breakout World public class Breakout extends World public void drawBlockOne() { int xCoord = 29; int yCoord = 29; int blockWidth = 60; for(int col = 0; col < maxCol; col++) { addObject(new blockOne(), xCoord, yCoord); xCoord += blockWidth; } Block Object public class Block extends Actor public void act() { }
danpost danpost

2024/1/13

#
ChillDude23 wrote...
Problem: In an linear array of block objects, how to make each block in the array two hit before being destroyed by a ball object (bad understanding soz) << Code Omitted >>
Just add an instance int variable to the Block class to track hits:
// variable in Block class
private int hitsRemaining = 2;

// when hit, call the following method in Block class
public void hit() {
    hitsRemaining--;
    if (hitsRemaining == 0) getWorld().removeObject(this);
}
You need to login to post a reply.