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

2016/9/4

my health bar wont work :(

heeyyyoooopatty heeyyyoooopatty

2016/9/4

#
it kept on saying that it cant find the variable 'healthbar' and nonstatic method
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bloctopus here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bloctopus extends Actor
{
    int health= 3;
    boolean touchingEnemies = false;
    GreenfootImage photo2 = new GreenfootImage("photo2.png");
    GreenfootImage photo3 = new GreenfootImage("photo3.png");
    GreenfootImage photo4 = new GreenfootImage("photo4.png");
    GreenfootImage photo5 = new GreenfootImage("photo5.png");
    GreenfootImage photo6 = new GreenfootImage("photo6.png");
    GreenfootImage photo7 = new GreenfootImage("photo7.png");
    
    GreenfootImage pat1 = new GreenfootImage("pat1.png");
    GreenfootImage pat2 = new GreenfootImage("pat2.png");
    GreenfootImage pat3 = new GreenfootImage("pat3.png");
    GreenfootImage pat4 = new GreenfootImage("pat4.png");
    GreenfootImage pat5 = new GreenfootImage("pat5.png");
    GreenfootImage pat6 = new GreenfootImage("pat6.png");
    public int frame = 1;
    public int animationCounter = 0;
    
    /**
     * Act - do whatever the Bloctopus wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       movements();
       animationCounter = animationCounter + 1;
       hitEnemies();
    }    
    
      public void movements ()
    {
    
    if (Greenfoot.isKeyDown("d"))
    {
        move(3);
        if(animationCounter % 6 == 0)
        animate();
     if ("space".equals(Greenfoot.getKey()))
    {
        getWorld().addObject (new Bullet(), getX(), getY());
         Greenfoot.playSound("Silencer.mp3");
    }
    }
    

    if ( Greenfoot.isKeyDown("a"))
{  
    move (-3);
    if(animationCounter % 6 == 0)
    animatee();
     if ("space".equals(Greenfoot.getKey()))
    {
        getWorld().addObject (new Bullet2(), getX(), getY());
       Greenfoot.playSound("Silencer.mp3");
    }
}
}

public void animate ()
{
    if (frame == 1)
    {
        setImage(photo2);
        frame = 2;
    }
   else if (frame ==2)
   {
       setImage(photo3);
       frame = 3;
    }
    else if (frame == 3)
    {
        setImage(photo4);
        frame = 4;
    }
    else if (frame == 4)
    {
        setImage(photo5);
        frame = 5;
    }
    else if (frame == 5)
    {
        setImage(photo6);
        frame = 6;
    }
    else if (frame == 6)
    {
        setImage(photo7);
        frame = 1;
    }
}

public void animatee ()
{
    if (frame == 1)
    {
        setImage(pat1);
        frame = 2;
    }
   else if (frame ==2)
   {
       setImage(pat2);
       frame = 3;
    }
    else if (frame == 3)
    {
        setImage(pat3);
        frame = 4;
    }
    else if (frame == 4)
    {
        setImage(pat4);
        frame = 5;
    }
    else if (frame == 5)
    {
        setImage(pat5);
        frame = 6;
    }
    else if (frame == 6)
    {
        setImage(pat6);
        frame = 1;
    }
    
}

public void hitEnemies()
{
Actor health = getOneIntersectingObject(HealthBar.class);
Actor enemy = getOneIntersectingObject (Enemy1.class);
if (enemy != null)
{
    World myWorld = getWorld();
    City city = (City) myWorld;
    if(touchingEnemies == false)
    {
        HealthBar. loseHealth();
        touchingEnemies = true;
        if (healthbar.health <=0)
        {
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
}
}
} else 
{
    touchingEnemies = false;
}
}
}
Super_Hippo Super_Hippo

2016/9/4

#
In line 140 you create an Actor variable named "health" and later in line 150 you use 'healthbar' as a 'Healthbar' object. In line 148 you even just use the class name Healthbar and try to use a method on the class. You can't call a non-static method on a class. You have to call it on an object. I don't really think that it makes sense that the Bloctopus has to hit the enemy and the healthbar to damage the enemy. (Right now, even if the healthbar code would work, it would give a nullpointer exception when only touching the enemy and not its healthbar.) What I suggest is to just check for an intersection with the enemy and call a method on the enemy object to damage it. The enemy knows its healthbar and changes its image.
danpost danpost

2016/9/4

#
Line 148 is telling the HealthBar class to lose health -- not an HealthBar object. Your HealthBar object is currently referenced by 'health' (see line 140). However, that field holds an Actor object and can hold an Actor object of any type. Therefore, you cannot directly call 'loseHealth' on 'health' with:
// this will not work
health.loseHealth();
Somewhere, the compiler must be informed that the variable actually holds a HealthBar object before you can call 'loseHealth' on it:
// one way (creating an additional variable of type HealthBar using typecasting)
HealthBar healthbar = (HealthBar)health;
healthbar.loseHealth();

// another way (typecasting to type HealthBar when method is called)
((HealthBar)health).loseHealth();

// one more way (creating initial variable of type HealthBar, still typecasting)
HealthBar health = (HealthBar)getOneIntersectingObject(HealthBar.class);
health.loseHealth();
As a sidenote, I believe that you should retain a field in the Bloctopus class to hold a reference to its HealthBar object. That way, it will avoid having to "search" for the intersecting HealthBar object every act method, but it can be typed properly and avoid typecasting at all. Besides, if any other actor has a HealthBar object and it ends up intersecting this actor at some point, the returned HealthBar object may end up to be the wrong one when using 'getOneIntersectingObject'..
danpost danpost

2016/9/4

#
Super_Hippo wrote...
I don't really think that it makes sense that the Bloctopus has to hit the enemy and the healthbar to damage the enemy.
I could be mistaken, but I think the health bar is for the Bloctopus actor and not for the enemy (which would make a little more sense).
You need to login to post a reply.