I'm trying to add a Shield-Recharge mechanic after the player's shield gets damaged.
But the shield-recharge is supposed to happen 3 seconds AFTER the shield is damage
But Greenfoot is ignoring this and just healing the shield the millisecond its damaged
I know it's because of Act() but I don't know how to fix it and damn its frustrating
// here is the start of my code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class leftStick here. * * @author (SilverCS) * @version (v1.6) */ // LEFT STICK CODE public class leftStick extends Actor { // Using simpleTimer to setup a new timer method right away private SimpleTimer shotTimersp2 = new SimpleTimer(); // shield time heal private SimpleTimer shieldTimer = new SimpleTimer(); // set animation images GifImage idleanim = new GifImage("idlestickL.gif"); // Standing still left GreenfootImage blockanim = new GreenfootImage("backBlock.gif"); // Blocking left GifImage walkanim = new GifImage("walkingstickL.gif"); // Walking left GreenfootImage crouchanim = new GreenfootImage("idleCrouchLcpy.gif"); // Crouching left GreenfootImage sp2animL = new GreenfootImage("fireballL.gif"); // Sp2 left GifImage guardBrokenL = new GifImage("guardbrokenL.gif"); // guard broken left //leftStick Audio GreenfootSound shieldFXL = new GreenfootSound("ShieldBlock.mp3"); // shield health starts at 3 int shieldL = 3; boolean isGuardBroken = false; boolean shieldStart = false; // used to start the timer int shieldHeal = 9; // 3x3 (3 ticks, 3 seconds) // health bar starts at full int healthL = 100;
// shield recovery AND guard break code public void shieldRecovery() { if (shieldTimer.millisElapsed() > 3000){ shieldStart = true; if (shieldStart == true) { shieldHeal--; // Or Timer = Timer - 1; if (shieldHeal <= 0) { shieldL +=1; isGuardBroken = false; } } } } public void guardBreak() { if (shieldL < 3) { shieldRecovery(); } if (shieldL <= 0) { isGuardBroken = true; setImage(guardBrokenL.getCurrentImage()); } }
// displaying the shield code private Actor shieldFullL = new shieldFullL(); public void shieldBar() { switch (shieldL) { case 3: getWorld().addObject(shieldFullL, 195, 538); break; case 2: shieldFullL.setImage("2shieldCracked.PNG"); break; case 1: shieldFullL.setImage("3shieldLow.PNG"); break; case 0: shieldFullL.setImage("4shieldBroken.PNG"); break; } }
// This is the code that effects the shield 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("s") == true && isGuardBroken == true) { healthL -= 10; getWorld().removeObject(fireballR); } if (fireballR!=null && Greenfoot.isKeyDown("q") == true && isGuardBroken == true) { healthL -= 10; getWorld().removeObject(fireballR); } if (fireballR!=null && Greenfoot.isKeyDown("2") == true){ healthL -= 10; getWorld().removeObject(fireballR); } if (fireballR!=null && Greenfoot.isKeyDown("q") == true && Greenfoot.isKeyDown("s") == false && Greenfoot.isKeyDown("2") == false && isGuardBroken == false) { getWorld().removeObject(fireballR); shieldFXL.play(); shieldL -=1; // Subtracts from shield }//end }//end
// 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 healthBarL(); //stickman01 shieldbar shieldBar(); //stickman01 guardbroken guardBreak(); } //end