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

2012/12/9

Unreachable statement?

programmer274 programmer274

2012/12/9

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Enemy here. * * @author (your name) * @version (a version number or a date) */ public class Enemy extends Animal { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private GreenfootImage image4; private GreenfootImage image5; private GreenfootImage image6; private GreenfootImage image7; private GreenfootImage image8; private GreenfootImage image9; private GreenfootImage image10; private GreenfootImage image11; private GreenfootImage image12; private GreenfootImage image13; private GreenfootImage image14; private GreenfootImage image15; private GreenfootImage image16; private GreenfootImage image17; private GreenfootImage image18; private boolean isFacingLeft; private int waitCycle = 0; private int Health; /** * Act - do whatever the Fighter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Enemy() { image1 = new GreenfootImage("Fighter Neutral 1.png"); image2 = new GreenfootImage("Fighter Neutral 2.png"); image3 = new GreenfootImage("Fighter Neutral 3.png"); image4 = new GreenfootImage("Fighter Kick 1.png"); image5 = new GreenfootImage("Fighter Kick 2.png"); image6 = new GreenfootImage("Fighter Kick 3.png"); image7 = new GreenfootImage("Fighter Punch 1.png"); image8 = new GreenfootImage("Fighter Punch 2.png"); image9 = new GreenfootImage("Fighter Punch 3.png"); image10 = new GreenfootImage("Fighter Neutral 1(f).png"); image11 = new GreenfootImage("Fighter Neutral 2(f).png"); image12 = new GreenfootImage("Fighter Neutral 3(f).png"); image13 = new GreenfootImage("Fighter Kick 1(f).png"); image14 = new GreenfootImage("Fighter Kick 2(f).png"); image15 = new GreenfootImage("Fighter Kick 3(f).png"); image16 = new GreenfootImage("Fighter Punch 1(f).png"); image17 = new GreenfootImage("Fighter Punch 2(f).png"); image18 = new GreenfootImage("Fighter Punch 3(f).png"); Health = 10; } public void act() { move(); attack(); } public void move() { if (!Greenfoot.isKeyDown("right") && (!Greenfoot.isKeyDown("left"))) { waitCycle++; if (waitCycle==18) waitCycle=0; if(isFacingLeft == false) { if (waitCycle==0) setImage(image1); if (waitCycle==6) setImage(image2); if (waitCycle==12) setImage(image3); } if(isFacingLeft == true) { if (waitCycle==0) setImage(image10); if (waitCycle==6) setImage(image11); if (waitCycle==12) setImage(image12); } } if (Greenfoot.isKeyDown("right")) { move(3); setImage(image1); wait(2); setImage(image2); move(3); wait(5); setImage(image3); wait(2); move(3); isFacingLeft = false; } if (Greenfoot.isKeyDown("left")) { move(-3); setImage(image10); wait(2); setImage(image11); move(-3); wait(5); setImage(image12); wait(2); move(-3); isFacingLeft = true; } } public void attack() { if (Greenfoot.isKeyDown("j")) { if (waitCycle==18) waitCycle=0; if(isFacingLeft == false) { if (waitCycle==0) setImage(image7); if (waitCycle==6) setImage(image8); if (waitCycle==12) setImage(image9); } if(isFacingLeft == true) { if (waitCycle==0) setImage(image16); if (waitCycle==6) setImage(image17); if (waitCycle==12) setImage(image18); } } if (Greenfoot.isKeyDown("k")) { if (waitCycle==18) waitCycle=0; if(isFacingLeft == false) { if (waitCycle==0) setImage(image4); if (waitCycle==6) setImage(image5); if (waitCycle==12) setImage(image6); } if(isFacingLeft == true) { if (waitCycle==0) setImage(image13); if (waitCycle==6) setImage(image14); if (waitCycle==12) setImage(image15); } } } private void checkEnemy() { Actor Fighter = getOneIntersectingObject(Fighter.class); if(Fighter != null && (Greenfoot.isKeyDown("j"))) { Greenfoot.playSound("slurp.wav"); Health = Health - 1; } if(Fighter != null && (Greenfoot.isKeyDown("k"))) { Greenfoot.playSound("slurp.wav"); Health = Health - 1; } } private int getHealth() { return Health; if(Health == 0) { eat(Enemy.class); } } public void wait(int time) { Greenfoot.delay(time); } } The problem occurs in the get health method with the if statement
danpost danpost

2012/12/9

#
I think you copy/pasted incorrectly change the following area
       Health = Health - 1;
    }
}   
private int getHealth()
{
 return Health;
  
  if(Health == 0)
  {
      eat(Enemy.class);
    }
}
to this
       Health = Health - 1;
    }
  
  if(Health == 0)
  {
      eat(Enemy.class);
    }
}   
private int getHealth()
{
 return Health;
}
danpost danpost

2012/12/9

#
You can press Control-Shift-I, or select 'Auto-Indent' within the 'Edit' menu of you code pages to make it easier to check you bracketing/blocks. Also you can use the 'code' tab below the 'Post a reply' text editor to insert your code within a discussion posting. If the tab does not work for you, you can instead put the tag before your code and after it.
programmer274 programmer274

2012/12/9

#
thanks for the help, again.
programmer274 programmer274

2012/12/9

#
well it still wont eat the character once health = 0
danpost danpost

2012/12/9

#
Instead of 'eat(Enemy.class);', try 'getWorld().removeObject(this);'.
programmer274 programmer274

2012/12/9

#
ok it works but i would like to learn why. Could you explain?
programmer274 programmer274

2012/12/9

#
Also it removes him to quickly it seems like it's executing too quickly my ounched so that it instantly does 10 damage. What do I fix?
programmer274 programmer274

2012/12/9

#
sorry i meant my punches
danpost danpost

2012/12/9

#
You are going to need one or two more instance variables added to the class. You can use a boolean, which when used will wait for seperation between the Enemy and the Fighter before allowing more damage to occur (I have a feeling this is not what you want). You can use an int, which will act as a cycle counter for slowing down the rate at which damage is applied (this is probably what you want).
// add with your other instance fields
private int damageDelay = 0;
// add at the beginning of your 'checkEnemy' method
if (damageDelay>0) damageDelay--;
if (damageDelay!=0) return;
// after each 'Health = Health - 1;' statements in 'checkEnemy' method
damageDelay = 15; // adjust to suit
You also may want to add an 'else' between the two 'if's that check for 'j' and 'k' keys being pressed in the 'checkEnemy' method.
programmer274 programmer274

2012/12/9

#
ok can you explain what each step means please in case i want to substitute this code in for future reference?
danpost danpost

2012/12/9

#
If you only want one point damage per attack, the boolean is the way to go
// add with your other instance fields
private boolean attacking;
// change the 'if's in your 'checkEnemy' method as follows
if (Fighter!=null)
{
    if (!attacking && (Greenfoot.isKeyDown("j") || Greenfoot.isKeyDown("k")))
    {
        Greenfoot.playSound("slurp.wav");
        Health = Health - 1;
        attacking = true;
    }
    if (attacking && (!Greenfoot.isKeyDown("j") && !Greenfoot.isKeyDown("k"))) attacking = false;
programmer274 programmer274

2012/12/9

#
ok this is very helpful also, do you have any idea how to make the transition through my punching and kicking methods. It seems when he is just bouncing in neutral state my puches and kicks only really show if i catch him at the right time in the cycle
danpost danpost

2012/12/9

#
'int' code breakdown:
// add with your other instance fields
private int damageDelay = 0;
// add at the beginning of your 'checkEnemy' method
if (damageDelay>0) damageDelay--;
if (damageDelay!=0) return;
// after each 'Health = Health - 1;' statements in 'checkEnemy' method
damageDelay = 15; // adjust to suit
Line 2 add an instance of that variable to all objects created within this class. Therefore any Enemy object created will have its very own variable to control the rate of damage. Line 4 runs the delay, if it is being used. If the delay time has expired, its value will be zero; otherwise it will be greater than zero and needs to be decremented. Line 5 checks the new value (or the current value, if it was zero), and does not continue execution of the method if its value is not zero. Line 7 begins the delay timer, initializing it with some value to count down from; this statement was placed immediately after applying damage and now we need to wait so many cycles until the timer returns to zero and we can apply damage once again.
You need to login to post a reply.