| View previous topic :: View next topic |
| Author |
Message |
gameyoda Expert Cheater
Reputation: 0
Joined: 06 Oct 2006 Posts: 150
|
Posted: Mon Dec 07, 2009 3:21 pm Post subject: Need help with breakout (greenfoot) |
|
|
Hello,
I have been trying to make breakout in greenfoot (starting with the basic program at http://www.greenfoot.org/scenarios/).
The problem is I dont know how to add health to my blocks. I tried going into ball as the blocks are deleted there and calling a self made method from brick, but it says it doesnt know brick and actor doesnt have the method I tried to call.
What I added to ball:
| Quote: | if (actor instanceof Brick)
{
brick.geraakt();
if(brick.getHitpoints() <= 0)
{
world.removeObject(actor);
world.checkIfWon();
}
} |
What the basic class contained was just:
| Quote: | world.removeObject(actor);
world.checkIfWon(); |
(Im dutch and geraakt means hit incase you wondered why I chose that name )
The code for the method geraakt in brick:
| Quote: | public void geraakt()
{
hitpoints--;
}
public int getHitpoints()
{
return hitpoints;
} |
hitpoints is an integer variable I made that starts out with the value 1 and when the brick is hit it goes to 0 and should be deleted (I made that in Ball as seen above).
In case you want it, greenfoot is downloadable from: http://www.greenfoot.org/download/
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Mon Dec 07, 2009 4:07 pm Post subject: Re: Need help with breakout (greenfoot) |
|
|
| gameyoda wrote: |
The problem is I dont know how to add health to my blocks.
| Quote: | public void geraakt()
{
hitpoints--;
} |
|
I'm not sure what your question/problem is but hitpoints--; decreases the hitpoints, and hitpoints++; will increase the hitpoints.
| Quote: | public void VoegHealthToe()
{
hitpoints++;
} |
| Quote: | if (actor instanceof Brick)
{
brick.geraakt();
if(brick.getHitpoints() <= 0)
{
world.removeObject(actor);
world.checkIfWon();
}
} |
I don't know what 'actor' is, but don't you mean world.removeObject(brick) if you want to remove the brick.
|
|
| Back to top |
|
 |
gameyoda Expert Cheater
Reputation: 0
Joined: 06 Oct 2006 Posts: 150
|
Posted: Mon Dec 07, 2009 11:59 pm Post subject: |
|
|
I know that hitpoints-- decreases it, thats what I wanted and when its at 0 the brick gets deleted. The question is how do I get greenfoot to anknowledge its a brick?
and I do want world.removeObject(actor); to be with actor, because thats how I started off (with what I downloaded). The only problem is that I cant call upon methods of the actor (which is a brick).
edit: nevermind, found the problem. I needed to make the actor a brick.
Thanks for the help though.
|
|
| Back to top |
|
 |
|