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

2014/12/25

loosing health after time

Steven.Z. Steven.Z.

2014/12/25

#
hi there im new to grenfoot and starting programming an online tutorial where you are a pie and are trying to hit banana; if they hit you, you loose health on your health bar; thats perfectly working, but now i want my pie to looses health ON ANOTHER HEALTHBAR after time and that you regain health after hitting another object. and i have no idea how to to that; if someone of you experienced guys could help me? thats my code of the healthbar: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class HealthBar100 extends Actor { int health = 100; // you have at the beginning 100 health points int healthBarWidth = 102; int healthBarHeight = 22; int pixelsPerHealthPoint = (int)healthBarWidth/health; public HealthBar100() { update(); } public void act() { update(); } public void update() { setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2)); GreenfootImage myImage = getImage(); myImage.setColor(Color.BLACK); myImage.drawRect(0, 0, healthBarWidth + 2, healthBarHeight + 2); myImage.drawRect(1, 0, healthBarWidth, healthBarHeight); // makes the black frame thicker myImage.drawRect(0, 1, healthBarWidth, healthBarHeight); // makes the black frame thicker myImage.setColor(Color.RED); myImage.fillRect(2, 2, health*pixelsPerHealthPoint, healthBarHeight - 2); } public void loseHealth25() // you lose 25 health points { health-=25; } and thats the code of the pie hitting a banana: public void hitBanana() // if you touch a banana you lose health { Actor banana = getOneIntersectingObject(Banana.class); if(banana !=null) { World world = getWorld(); Waterworld waterworld = (Waterworld)world; HealthBar100 healthbar100 = waterworld.getHealthBar100(); if(touchingBanana== false) { healthbar100.loseHealth50(); touchingBanana = true; if(healthbar100.health <=0) // GameOver-Screen appears if healthbar reaches 0 { GameOver gameover = new GameOver(); world.addObject(gameover, 300, 200); world.removeObject(this); } Greenfoot.playSound("much.mp3"); } } else { touchingBanana = false; } } as i said, these codes are working and the pie is loosing health, but i want it to loose health on ANOTHER healthbar automatically thanks!!!
danpost danpost

2014/12/25

#
It appears you have the first healthbar saved in a field in your world (Waterworld object). You can create and save another Healthbar100 object in your world class with a different field name along with a getter method (just as you did with the first). Then add an int field to the world for the timer for decreasing the value of the bar and use the getter method from the actor class to increase its value.
Steven.Z. Steven.Z.

2014/12/25

#
i copied some stuff from here and from a tutorial site; thats what i thought and i already copied another healthcare, renamed it and copied the getter method; but i don't know how to create the timer; i want it loose 1 health in 1 second; i couldn't find it on any tutorial sites and just started with green foot some days ago. and im actually totally happy that the health bar works;
danpost danpost

2014/12/25

#
The 'timer' is just an int field within the class that the act method increment, followed by checking for a certain limit to its value. When the limit is reached, the action (decreasing the value of the bar) is taken and the timer is reset to zero -- the cycle repeats. You may be interested in looking at my Bar Subclasses scenario. It has a Bar class (for a health bar, progress bar ... whatever; plus, it has three subclasses that give it specific abilities (a switch, an adjustable control, and a timer for counting either act cycles or seconds).
danpost danpost

2014/12/25

#
Steven.Z. wrote...
i already copied another healthcare, renamed it
This is not how you create another healthbar. This just creates another class that can produce healthbars. You create multiple healthbars by using the 'new' keyword on the class name multiple times. The HealthBar100 class is too restrictive, as written, to be used for the purpose you wish to use it for. That is, the objects created from the class have only five possible health values (0, 25, 50, 75 and 100) because it can only subtract 25 from its value at a time. It can be adjusted to work for both purposes without having to create a second class with very little effort -- that is, by adding a method (or changing the one already in the class) to allow changing its value by units.
danpost danpost

2014/12/25

#
There is also another thing about the HealthBar100 class that should be of concern. It refreshes its image every act cycle, which is not very efficient. It only needs to refresh its image when the value of the bar changes. You can remove the act method from the class and add a call to 'update' as the last line in the loseHealth25 method.
You need to login to post a reply.