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

2014/5/22

Healthbar code not working...

1
2
SAAEngineer SAAEngineer

2014/5/22

#
import greenfoot.*; public class Spaceship extends CollisionObjects { Explosion hit = new Explosion(); int hitcounter = 0; int timer = 40; int aftel = timer; Healthbar0 healthbar0 = new Healthbar0(); Healthbar1 healthbar1 = new Healthbar1(); Healthbar2 healthbar2 = new Healthbar2(); Healthbar3 healthbar3 = new Healthbar3(); Healthbar4 healthbar4 = new Healthbar4(); Healthbar5 healthbar5 = new Healthbar5(); Healthbar6 healthbar6 = new Healthbar6(); Healthbar7 healthbar7 = new Healthbar7(); Healthbar8 healthbar8 = new Healthbar8(); Healthbar9 healthbar9 = new Healthbar9(); Healthbar10 healthbar10 = new Healthbar10(); public void act() { hit(); switch (hitcounter){ case 0: getWorld().addObject(healthbar0,24,2); System.out.println("0"); break; case 1: getWorld().removeObject(healthbar0); getWorld().addObject(healthbar1,24,2); System.out.println("1"); break; case 2: getWorld().removeObject(healthbar1); getWorld().addObject(healthbar2,24,2); System.out.println("2"); break; case 3: getWorld().removeObject(healthbar2); getWorld().addObject(healthbar3,24,2); System.out.println("3"); break; case 4: getWorld().removeObject(healthbar3); getWorld().addObject(healthbar4,24,2); System.out.println("4"); break; case 5: getWorld().removeObject(healthbar4); getWorld().addObject(healthbar5,24,2); System.out.println("5"); break; case 6: getWorld().removeObject(healthbar5); getWorld().addObject(healthbar6,24,2); System.out.println("6"); break; case 7: getWorld().removeObject(healthbar6); getWorld().addObject(healthbar7,24,2); System.out.println("7"); break; case 8: getWorld().removeObject(healthbar7); getWorld().addObject(healthbar8,24,2); System.out.println("8"); break; case 9: getWorld().removeObject(healthbar8); getWorld().addObject(healthbar9,24,2); System.out.println("9"); break; case 10: getWorld().removeObject(healthbar9); getWorld().addObject(healthbar10,24,2); System.out.println("10"); break; } } public void hit(){ Actor BadGuy = getOneIntersectingObject(BadGuy.class); if(BadGuy != null) { setImage("broken.jpg"); Greenfoot.playSound("alert.mp3"); Damage hit2 = new Damage(); getWorld().addObject(hit2,30,50); getWorld().removeObject(BadGuy); hitcounter++; } } }
danpost danpost

2014/5/22

#
How is it not working? are there any error messages? Is it showing up at all? When does it begin to not behave like you want?
SAAEngineer SAAEngineer

2014/5/23

#
It shows but it it stuck on "healthbar1.png" and the terminal error only prints zeros.
danpost wrote...
How is it not working? are there any error messages? Is it showing up at all? When does it begin to not behave like you want?
danpost danpost

2014/5/23

#
Is the 'hit' method being executed at all? Put a System.out.println("hit");' statement in it to see.
SAAEngineer SAAEngineer

2014/5/23

#
danpost wrote...
Is the 'hit' method being executed at all? Put a System.out.println("hit");' statement in it to see.
Yes hit is working, let me also mention that because of the switch method it lags a lot!
danpost danpost

2014/5/23

#
SAAEngineer wrote...
danpost wrote...
Is the 'hit' method being executed at all? Put a System.out.println("hit");' statement in it to see.
Yes hit is working, let me also mention that because of the switch method it lags a lot!
The lag is due to the continuous updating of the healthbar. You could initialize it and only change it when the 'hitcounter' field changes to speed things up. However, there is probably a much more efficient way of doing this. You are using 10 different classes to represent one object. This is wasteful -- especially since the only difference between them is the image. Just use one class and change its image when the 'hitcounter' field changes:
import greenfoot.*;

public class Spaceship extends CollisionObjects
{
    int hitcounter = 0;
    Actor healthbar = new Actor(){};

    // add healthbar to world and set its initial image
    public void addedToWorld(World world)
    {
        healthbar.setImage("healthbar0.png");
        world.addObject(healthbar, 24, 2);
    }

    public void act() 
    {     
        hit();
    }

    public void hit()
    {
        Actor badGuy = getOneIntersectingObject(BadGuy.class);
        if(badGuy != null)
        {
            setImage("broken.jpg");
            Greenfoot.playSound("alert.mp3");
            Damage hit2 = new Damage();
            getWorld().addObject(hit2,30,50);
            getWorld().removeObject(badGuy);
            hitcounter++;
            // update the image of the healthbar
            healthbar.setImage("healthbar"+hitcounter+".png");
        }
    }
}
I removed everything that was not being used in the class (mainly excess fields). Your images may not be named as I show above. It is just an example of how it could be done. Also, you can replace 'Actor healthbar = new Actor(){};' with 'Healthbar healthbar = new Healthbar();' if you want to have a single Healthbar class. The class can look like this:
import greenfoot.*;

public class Healthbar extends Actor
{
    int imageNum;

    public Healthbar()
    { // set initial image
        setImage("healthbar0.png");
    }

    public void showHit()
    { // update image
        imageNum++;
        setImage("healthbar"+imageNum+".png");
    }
}
Then to update its image call 'healthbar.showHit();' (as a replacement for the last line of the 'hit' method above).
SAAEngineer SAAEngineer

2014/5/23

#
Thanks for your help, it's really appreciated but now the healthbar doesnt proceed, it is stuck on "Healthbar0.png" , any solutions? Thanks in advance
danpost danpost

2014/5/23

#
Please show your updated code.
SAAEngineer SAAEngineer

2014/5/23

#
import greenfoot.*;

public class Spaceship extends CollisionObjects
{
    int hitcounter = 0;
    Actor healthbar = new Actor(){};

    // add healthbar to world and set its initial image
    public void addedToWorld(World world)
    {
        healthbar.setImage("Healthbar0.png");
        world.addObject(healthbar, 24, 2);
    }

    public void act() 
    {     
        hit();
    }

    public void hit()
    {
        Actor badGuy = getOneIntersectingObject(BadGuy.class);
        if(badGuy != null)
        {
            setImage("broken.jpg");
            Greenfoot.playSound("alert.mp3");
            Damage hit2 = new Damage();
            getWorld().addObject(hit2,30,50);
            getWorld().removeObject(badGuy);
            hitcounter++;
            // update the image of the healthbar
            healthbar.setImage("Healthbar"+hitcounter+".png");
        }
    }
}
import greenfoot.*;

public class Healthbar extends Actor
{
    int imageNum;

    public Healthbar()
    { // set initial image
        setImage("Healthbar0.png");
    }

    public void showHit()
    { // update image
        imageNum++;
        setImage("Healthbar"+imageNum+".png");
    }
}
danpost danpost

2014/5/23

#
You did not change line 6 of the Spaceship class to:
Healthbar healthbar = new Healthbar();
nor, did you change line 32 to:
healthbar.showHit();
SAAEngineer SAAEngineer

2014/5/23

#
It's still stuck on a full healthbar , or "healthbar0.png".
danpost danpost

2014/5/23

#
I wonder ... what does your CollisionObjects class look like?
SAAEngineer SAAEngineer

2014/5/25

#
danpost wrote...
I wonder ... what does your CollisionObjects class look like?
it's an empty class made for visual classification
danpost danpost

2014/5/25

#
SAAEngineer wrote...
danpost wrote...
I wonder ... what does your CollisionObjects class look like?
it's an empty class made for visual classification
And what does it extend?
SAAEngineer SAAEngineer

2014/5/26

#
danpost wrote...
SAAEngineer wrote...
danpost wrote...
I wonder ... what does your CollisionObjects class look like?
it's an empty class made for visual classification
And what does it extend?
It extends Actor
There are more replies on the next page.
1
2