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

2012/12/17

Breakout block degrading

Motifaded Motifaded

2012/12/17

#
so im making a simple breakout game with degrading blocks. I have the ball and block code so that every time the ball hits a block, the block image changes to a more degraded version. On the last image change, the block is removed. The problem is that if i add more than one block to the world, they all change image at the same time even when only one was hit. I'd really appreciate some advice please. This source is here: http://www.greenfoot.org/scenarios/7074
Gevater_Tod4711 Gevater_Tod4711

2012/12/17

#
the problem is that your variable hitCounter is static which means that it is the same value in all objects of this class (in your block class). You have to use non-statics then it'll work.
vonmeth vonmeth

2012/12/17

#
The problem is is that your hitCount is static. That means there is only one instance of the variable hitCount, and it is shared between each instance of the class Block. Make hitCount non-static, create a method to increase hitCount, and call that method based on what block is hit.
danpost danpost

2012/12/17

#
The problem is in the Ball class with the following statement:
Block.hitCount = Block.hitCount + 1;
and the fact the 'hitCount' in the Block class was made 'static'. By making the 'hitCount' field 'static', it is being made a class field (one, and only one, field that belongs to the class as a whole; not a field that is given to each and every instance of the class individually). First, remove the 'static' modifier from the 'hitCount' field in the Block class. Then, change the line above that increments the 'hitCount' value to
Block block=(Block) red;
red.hitCount=red.hitCount+1;
or more simply
((Block) red).hitCount++;
davmac davmac

2012/12/17

#
The problem is that the 'hitCount' field is static. Sorry, I didn't even actually look at your code, I just wanted to join the chorus XD
Motifaded Motifaded

2012/12/18

#
lol, awesome. thanks for the help guys, it worked.
danpost danpost

2012/12/18

#
danpost wrote...
Block block=(Block) red;
red.hitCount=red.hitCount+1;
or more simply
((Block) red).hitCount++;
In the first code block, I had inadvertently used the wrong variable in the second line. It should read:
block.hitCount=block.hitCount+1;
You need to login to post a reply.