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

2020/2/24

Enemy with healthbar

Yoshi21137 Yoshi21137

2020/2/24

#
I am making a game where the enemies spawn in with a health bar above their heads but I don't know how to either spawn in?
danpost danpost

2020/2/24

#
??? Context. Codes. Clarification.
Yoshi21137 Yoshi21137

2020/2/24

#
What I am trying to do is that after a certain amount of kills, the boss spawns in with the health bar above it's head The Boss: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class GoblinPriest here. * * @author (your name) * @version (a version number or a date) */ public class GoblinPriest extends Enemies { int bossx = getX(); int bossy = getY(); int kills = ((Knight)getWorld().getObjects(Knight.class).get(0)).kills; /** * Act - do whatever the GoblinPriest wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { die(); } public void die() { if (((BossHealth)getWorld().getObjects(BossHealth.class).get(0)).lives == 0) { getWorld().removeObject(this); } } } The BossHealth: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class BossHealth here. * * @author (your name) * @version (a version number or a date) */ public class BossHealth extends GoblinPriest { int lives = 10; int bossx= ((GoblinPriest)getWorld().getObjects(GoblinPriest.class).get(0)).bossx; int bossy= ((GoblinPriest)getWorld().getObjects(GoblinPriest.class).get(0)).bossy; /** * Act - do whatever the BossHealth wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { takeDamage(); die(); //setLocation(bossx, bossy); } public void die() { if (lives <= 0) { getWorld().removeObject(this); } } public void takeDamage() { if (lives == 9) { setImage("BossHealth9.png"); } if (lives == 8) { setImage("BossHealth8.png"); } if (lives == 7) { setImage("BossHealth7.png"); } if (lives == 6) { setImage("BossHealth6.png"); } if (lives == 5) { setImage("BossHealth5.png"); } if (lives == 4) { setImage("BossHealth4.png"); } if (lives == 3) { setImage("BossHealth3.png"); } if (lives == 2) { setImage("BossHealth2.png"); } if (lives == 1) { setImage("BossHealth1.png"); } if (lives == 0) { setImage("BossHealth0.png"); } } } The Player: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Knight here. * * @author (your name) * @version (a version number or a date) */ public class Knight extends Players { int bulletSpeed = 100; int kills = 0; /** * Act - do whatever the Knight wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeys(); } public void checkKeys() { if (Greenfoot.isKeyDown("w")) { turn(-90); move(5); turn(90); } if (Greenfoot.isKeyDown("s")) { turn(90); move(5); turn(-90); } if (Greenfoot.isKeyDown("d")) { move(5); } if (Greenfoot.isKeyDown("a")) { move(-5); } if(Greenfoot.isKeyDown("enter") && getObjectsInRange(bulletSpeed, Bullet.class).isEmpty()) { getWorld().addObject(new Bullet(), getX()+40, getY()+8); } } } Enemy: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class GoblinShaman here. * * @author (your name) * @version (a version number or a date) */ public class GoblinShaman extends Enemies { /** * Act - do whatever the GoblinShaman wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(-5); turn(); newEnemy(); kill(); die(); } public void die() { if (canSee(Bullet.class)) { getWorld().removeObject(this); } } public void turn() { if ( Greenfoot.getRandomNumber(100) < 10 ) { turn( Greenfoot.getRandomNumber(40)-10 ); } } public void newEnemy() { if ( Greenfoot.getRandomNumber(450) < 1 ) { createNewShaman(); } } private void createNewShaman() { GoblinShaman newGoblinShaman; newGoblinShaman = new GoblinShaman(); int x = Greenfoot.getRandomNumber(1000); int y = Greenfoot.getRandomNumber(650); getWorld().addObject(newGoblinShaman, x, y); } }
danpost danpost

2020/2/24

#
First off, the health bar is NOT a boss. The class for the health bar should extend Actor (or something that generalizes a health bar). Secondly, you cannot use getWorld in a field descriptor. The actor must be created and added into a world before getWorld will return a World object. Move lines 12 and 14 in your BossHealth class into the act method. Also, note that where you had those lines, their values would be set, if possible, once when the actor is being created. The lines would not have their values update from there.
Yoshi21137 Yoshi21137

2020/2/25

#
What do you mean by
Also, note that where you had those lines, their values would be set, if possible, once when the actor is being created. The lines would not have their values update from there.
and what about
The actor must be created and added into a world before getWorld will return a World object.
danpost danpost

2020/2/25

#
Yoshi21137 wrote...
What do you mean by << Quote Omitted >>
To give a simple example:
1
2
3
4
5
6
7
8
9
public class Aktor extends greenfoot.Actor
{
    int angle = getRotation();
     
    public void act()
    {
        turn(1);
    }
}
Here is an Actor subclass that describes something that slowly spins in place. The angle field, though initially set by the rotation of the actor, will have a value that does not change. There is no permanent link between the actor's rotation and the value of the field.
and what about << Quote Omitted >>
Just that. If the actor is not in a world (added via the addObject method), then getWorld will not return a World object, but instead will return a null value. Likewise, if the actor is removed from the world, again getWorld will return null. Fields are only assigned their initial values once, when the object is being created, and for an actor, before it has a chance to be added into any world.
You need to login to post a reply.