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

2022/1/15

Act Cycle Keeps Adding Object Over And Over

SilverCS SilverCS

2022/1/15

#
I'm trying to make a HealthBar with Actors because I wasn't able to do it with Images (Draw Image wouldn't let me clear the previous image so I can draw a new healthbar) But Now I have an issue where since the health value is 100 it keeps adding the 100 health bar same with 90 health, 80, 70, 60 etc Sure it works but the game gets extremely laggy from the infinite objects getting added. I guess somehow figuring out how to do this in World class instead of actor would fix? I'm stuck
// The code for the health bar display
public void healthBarL()
    {
       
        if (healthL == 100)
        {
            hpFullL hpMax100 = new hpFullL();
            getWorld().addObject(hpMax100, 164, 565);

        }
        
        if (healthL == 90)
        {
            getWorld().removeObjects(getWorld().getObjects(hpFullL.class));
            hp90L hpHigh90 = new hp90L();
            getWorld().addObject(hpHigh90, 164, 565);
        }

        if (healthL == 80)
        {
            getWorld().removeObjects(getWorld().getObjects(hp90L.class));
            hp80L hphigh80 = new hp80L();
            getWorld().addObject(hphigh80, 164, 565);

        }
        if (healthL == 70)
        {
            getWorld().removeObjects(getWorld().getObjects(hp80L.class));
            hp70L hphigh70 = new hp70L();
            getWorld().addObject(hphigh70, 164, 565);

        }
        if (healthL == 60)
        {
            getWorld().removeObjects(getWorld().getObjects(hp70L.class));
            hp60L hpMidHigh60 = new hp60L();
            getWorld().addObject(hpMidHigh60, 164, 565);

        }
        if (healthL == 50)
        {
            getWorld().removeObjects(getWorld().getObjects(hp60L.class));
            hp50L hpMid50 = new hp50L();
            getWorld().addObject(hpMid50, 164, 565);

        }
        if (healthL == 40)
        {
            getWorld().removeObjects(getWorld().getObjects(hp50L.class));
            hp40L hpLowMid40 = new hp40L();
            getWorld().addObject(hpLowMid40, 164, 565);

        }
        if (healthL == 30)
        {
            getWorld().removeObjects(getWorld().getObjects(hp40L.class));
            hp30L hpLow30 = new hp30L();
            getWorld().addObject(hpLow30, 164, 565);

        }
        if (healthL == 20)
        {
            getWorld().removeObjects(getWorld().getObjects(hp30L.class));
            hp20L hpLow20 = new hp20L();
            getWorld().addObject(hpLow20, 164, 565);

        }
        if (healthL == 10)
        {
            getWorld().removeObjects(getWorld().getObjects(hp20L.class));
            hp10L hpLowest10 = new hp10L();
            getWorld().addObject(hpLowest10, 164, 565);

        }
        if (healthL == 0)
        {
            getWorld().removeObjects(getWorld().getObjects(hp10L.class));
            hp0L hpDead0 = new hp0L();
            getWorld().addObject(hpDead0, 164, 565);
            getWorld().showText("Left Player was Killed!", 120, 100);
            Greenfoot.delay(110);
            Greenfoot.setWorld(new StartScreen());

        }
    }
// The Code for whats changing health value
  public void hitDetectionSp2R () {
        Actor fireballR = getOneIntersectingObject(fireballR.class);
        if (fireballR!=null && Greenfoot.isKeyDown("s") == false && Greenfoot.isKeyDown("q") == false){

            healthL -= 10; 
            getWorld().removeObject(fireballR);

        }//end

        if (fireballR!=null && Greenfoot.isKeyDown("q") == true && Greenfoot.isKeyDown("s") == false)
        {
            getWorld().removeObject(fireballR);
            shieldFXL.play();
        }//end

    }//end
// the code for act method

   public void act()
    {
        // by default, stick01 is idle
        idle();

        // stickman01 blocks upon pressing "q" key
        blocking();

        // stickman01 backWalks upon pressing the "a" key
        backWalking();

        // stickman01 crouches upon pressing the "s" key
        crouching();

        // stickman01 walks upon pressing the "d" key
        walking();

        // stickman01 casts a fireball (super-power-#2) upon pressing the "2" key
        sp2check();

        // if stickman01 is hit by stickman02's sp2 then stickman01 will die lol 
        hitDetectionSp2R();

        //stickman01 healthbar is the issue
        healthBarL();

    } //end
danpost danpost

2022/1/15

#
You should only need one (1) health bar actor per player. Its image should only be updated when the health value changes. The health value, then (upon changing) can be used to determine death of player. You may want to look at my Value Display Tutorial scenario. In your case, best may be to use the SimpleActor class (discussed in tutorial) with fields and methods in your Player class to reference the player's bar and control its value.
SilverCS SilverCS

2022/1/15

#
Thanks so much, I used 1 health for each player and used a switch and it cleaned up my code (and performance) by sooo much
You need to login to post a reply.