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

2017/3/5

Points counter?

1
2
3
danpost danpost

2017/3/6

#
OKNEM wrote...
I didn't really get much out of that, I already knew the most of it. The text part I know, but what I do need to know is how to detect whether an object is a specific image.
Alright, the first thing is in your Space class, you need to keep a reference to the Health object:
private Health healthbar = new Health();
Make sure you do not create another Health object in the class and add this Health object into the world (do not use 'new Health()' anywhere else in the class). Then you can use an instance int field for the health value in your Space class:
private int health;
Now, you need a method in the Space class to set the appropriate image depending on the 'health' value given it:
public void adjustHealth(int changeAmount)
{
    health += changeAmount;
    if (health > 10) health = 10;
    if (health < 0) health = 0;
    healthbar.setImage("health"+health+".png");
    if (health == 0)
    {
        // game over code goes here
    }
}
In the world constructor, 'public Space()', add the following line to initialize the healthbar:
adjustHealth(10);
You do not need to add any code into the Health class. In the Asteroid class:
if (canSee(Rocket.class))
{
    ((Space)getWorld()).adjustHealth(-1);
    getWorld().removeObject(this);
}
OKNEM OKNEM

2017/3/6

#
Okay. I want to make it so that the health bar goes down with every hit, so if the the picture is health5 and the Rocket is hit, it goes to health4. health4 goes to health3, health3 goes to health2 and so on until health1 is hit, then the game over code is triggered. Any way?
danpost danpost

2017/3/6

#
OKNEM wrote...
Okay. I want to make it so that the health bar goes down with every hit, so if the the picture is health5 and the Rocket is hit, it goes to health4. health4 goes to health3, health3 goes to health2 and so on until health1 is hit, then the game over code is triggered. Any way?
I just explained how to accomplish that in my last post.
OKNEM OKNEM

2017/3/6

#
danpost wrote...
OKNEM wrote...
Okay. I want to make it so that the health bar goes down with every hit, so if the the picture is health5 and the Rocket is hit, it goes to health4. health4 goes to health3, health3 goes to health2 and so on until health1 is hit, then the game over code is triggered. Any way?
I just explained how to accomplish that in my last post.
Ohhh. I see how you did that now. Well, I tried this and an error message came up saying that it couldn't find "health10.png". I changed all the 10's to 5's and the Health bar doesn't appear at all now.
danpost danpost

2017/3/6

#
Still getting the error message? Show the entire error output, if so, and the releted codes. Probably best you show your revised codes, anyway.
OKNEM OKNEM

2017/3/6

#
 public void adjustHealth(int changeAmount)
    {
        health += changeAmount;
        if (health > 5) health = 5;
        if (health < 0) health = 0;
        healthbar.setImage("health"+health+".png");
        if (health == 0)
        {
            addObject(new GameOver(), 400, 300);
        }
    }
That's the only bit that I changed, and the health bar doesn't spawn now.
        adjustHealth(5);
Oh, and that in the constructor.
danpost danpost

2017/3/6

#
OKNEM wrote...
That's the only bit that I changed, and the health bar doesn't spawn now.
Alright, show the entire Space class code.
OKNEM OKNEM

2017/3/6

#
import greenfoot.*;  // (Game, MyWorld, background.png)

/**
 * My World! :)
 */
public class Space extends World
{
    private Health healthbar = new Health();
    private int health;
    private Counter theCounter;
    /**
     * Default constructor that prepares the world.
     */
    public Space()
    {    
        super(800, 600, 1); 
        prepare();
        adjustHealth(5);
    }

    public Counter getCounter()
    {
        return theCounter;
    }
    // Checks the width and height of the Rocket.
    public int x = (new Rocket()).getImage().getWidth();
    public int y = (new Rocket()).getImage().getHeight();

    // Sets the delay for the Asteroid at 0.
    private int delayAsteroid=0;
    /**
     * Checks the delay of the Asteroid class, and if it's at zero, adds another Asteroid at a
     * an x coordinate of 40 degrees, and a random y coordinate.
     */
    public void act()
    {
        if (delayAsteroid>0 && --delayAsteroid>0) 
        {
            return;
        }
        addObject(new Asteroid(), 30, (Greenfoot.getRandomNumber(570)));
        delayAsteroid = (Greenfoot.getRandomNumber(60));

    }

    public void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(new Rocket(270),410,510);
        theCounter = new Counter();
        addObject(theCounter, 76, 30);
    }

    public void adjustHealth(int changeAmount)
    {
        health += changeAmount;
        if (health > 5) health = 5;
        if (health < 0) health = 0;
        healthbar.setImage("health"+health+".png");
        if (health == 0)
        {
            addObject(new GameOver(), 400, 300);
            Greenfoot.stop();
        }
    }
}
I'm trying it out a bit and I can see that it's working. The error's coming up that it can't find a health0 picture, but that's an easy fix. I just need it to show now.
danpost danpost

2017/3/6

#
You removed line 45 from the Space class (see previous posting of class -- not the last one). Put that line back in.
OKNEM OKNEM

2017/3/6

#
Where should this (addObject(health,690,34); if I'm right) go in?
danpost danpost

2017/3/6

#
OKNEM wrote...
Where should this (addObject(health,690,34); if I'm right) go in?
Right where you had it. Oh, but it should be 'healthbar' -- not 'health'.
OKNEM OKNEM

2017/3/6

#
Line 45?
OKNEM OKNEM

2017/3/6

#
I put it in the prepare method, and it appears. The image doesn't change, though, but the image count still goes down to 0. ?
danpost danpost

2017/3/6

#
OKNEM wrote...
I put it in the prepare method, and it appears. The image doesn't change, though, but the image count still goes down to 0. ?
Strange. Either you are not being clear as to what is happening ('image count' is same as 'health' ?) or all your image files have the same image in them. I cannot conceive another possibility at the moment; except that maybe you added both lines 44 and 45 back in, which would be an easy fix (removing line 44 again -- keeping line 45 in the method).
OKNEM OKNEM

2017/3/6

#
Aha. I removed line 44 and an error appears saying "int cannot be converted to greenfoot.Actor". Any clue? Also, it's supposed to go in the prepare method, right?
There are more replies on the next page.
1
2
3