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

2020/3/5

How to properly use setImage?

1
2
3
footpickle footpickle

2020/3/5

#
I'm trying to make a health bar with 180 separate sprites to make up 180 total health. I'm trying to do this with setImage. How would I do this? (or is there a more efficient way?) Thanks.
danpost danpost

2020/3/5

#
footpickle wrote...
I'm trying to make a health bar with 180 separate sprites to make up 180 total health. I'm trying to do this with setImage. How would I do this? (or is there a more efficient way?)
Please summarize how the different sprites are named in the file directory.
footpickle footpickle

2020/3/5

#
basically like this: Healh1, Health2, Health3, etc
danpost danpost

2020/3/5

#
footpickle wrote...
basically like this: Healh1, Health2, Health3, etc
Then something like the following should work:
1
setImage("Health"+(health+1)+".png");
I put in the "+1" in the off chance you did not have a "Health0" image. Only set the image initially and when the value of health changes.
footpickle footpickle

2020/3/6

#
But how would I make this happen when my boss takes damage? My healthbar is a subclass of the boss, if that helps
Yoshi21137 Yoshi21137

2020/3/6

#
Do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void act()
{
    takeDamage();
}
 
public void takeDamage()
    {
        if (lives == 9)
        {
            setImage("Health9.png");
        }
        if (lives == 8)
        {
            setImage("Health8.png");
        }
    }
Yoshi21137 Yoshi21137

2020/3/6

#
and put a int in for lives and change the variables in the if statements to match the amount of lives the enemy has
danpost danpost

2020/3/6

#
footpickle wrote...
But how would I make this happen when my boss takes damage? My healthbar is a subclass of the boss, if that helps
Since when is a health bar a boss? It does not have any behaviors of a boss. It is as much like a boss as a bowl of petunias is like a whale. Your healthbar class must not extend the class of the boss. It can extend the Actor class or a subclass of Actor that generally describes what a healthbar is.
footpickle footpickle

2020/3/6

#
danpost wrote...
It is as much like a boss as a bowl of petunias is like a whale.
Thanks for the roast
footpickle footpickle

2020/3/6

#
Yoshi21131, Would I put that in the HealthBar class, or the boss class? if in the healthbar class, how would it detect if the boss has taken damage?
danpost danpost

2020/3/6

#
footpickle wrote...
Thanks for the roast
It was not intended to be a roasting. Just trying to drive my point across (with a little humor -- dry as it may be). You could remove everything dealing with the health bar of the boss (and I mean everything; including the adding of the health bar into the world and the class itself being deleted) and structure your Boss class as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Boss extends Actor
{
    private int health = 180;
    private BossHealth bossHealth = new BossHealth(); // creates the health bar for this boss
     
    protected void addedToWorld(World world)
    {
        positionHealthbar(); // adds bar into world
    }
     
    /** adds healthbar to world, if needed, and updates its position */
    private void positionHealthbar()
    {
        getWorld().addObject(bossHealth, 0, 0);
        bossHealth.setLocation(getX(), getY()-30);
    }
     
    /** class of healthbar */
    private class BossHealth extends Actor
    {
        public BossHealth()
        {
            updateImage(); // initializes image
        }
         
        /** updates image of healthbar */
        protected void updateImage()
        {
            setImage(new GreenfootImage("Health"+health+".png"));
        }
    }
}
In main part of Boss class, when taking damage:
1
2
3
4
5
6
7
8
health--;
if (health == 0)
{
    getWorld().removeObject(bossHealth);
    getWorld().removeObject(this);
    return;
}
bossHealth().updateImage();
That is only the Boss stuff related to its healthbar and does not include calling positionHealthBar after the boss moves.
footpickle footpickle

2020/3/7

#
I have a couple of issues: 1. My healthbar isn't following the boss, it just stays still 2. When my boss gets hit by a bomb, it's health decreases dramatically. I tested the bomb with 63 health as I haven't finished making all my sprites, and it only takes about 3 hits to kill the boss. 3. My healthbar doesn't change image, and it starts off as 63. Have I done something wrong? I've copied the code almost exactly. In fact, here it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Boss here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Boss extends obstacles
{
    private int health = 63;
    private BossHealth bossHealth = new BossHealth();
    public int BossX;
    public int BossY;
     
    protected void addedToWorld(World bossworld)
{
    positionHealthBar();
}
    /**
     * Act - do whatever the Boss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
   {
    Plane2 plane2 = (Plane2)getWorld().getObjects(Plane2.class).get(0);
     
    turnTowards(plane2.getX(), plane2.getY());
    Boss boss = new Boss();
    move(6);
    BossX = getX();
    BossY = getY();
     if (isTouching(Bomb.class))
     {
         health--;
     }
     if (health == 0 )
     {
         getWorld().removeObject(this);
         return;
     }
      
    }
    private void positionHealthBar()
    {
        getWorld().addObject(bossHealth, 0, 0);
        bossHealth.setLocation(getX(), getY()-30);
    }
    private class BossHealth extends Actor
    {
        public BossHealth()
        {
            updateImage(); // initializes image
        }
          
        /** updates image of healthbar */
        protected void updateImage()
        {
            setImage(new GreenfootImage("Health"+health+".png"));
        }
    }
}
This is ALL the code in my boss class. I'm pretty sure it's all right.
danpost danpost

2020/3/7

#
Insert AFTER line 30 (after moving) the following line:
1
positionHealthBar();
and insert BEFORE line 39 (before removing self) the following line:
1
getWorld().removeObject(bossHealth);
danpost danpost

2020/3/7

#
Insert AFTER line 35 (after changing health value) the following line:
1
bossHealth.updateImage();
danpost danpost

2020/3/7

#
How fast do you want the health to decrease whilst the boss is sitting on a bomb?
There are more replies on the next page.
1
2
3