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

2016/6/8

Enemy Health Not Working

1
2
AreebRaza AreebRaza

2016/6/8

#
My Method in my Shoot class:
public void hitEnemy(){
        Skeleton1 e1 = (Skeleton1) getOneObjectAtOffset(0,0, Skeleton1.class);
        SkeletonA2 e2 = (Skeleton2) getOneObjecttOffset(0,0, Skeleton2.class);
        if(e1 != null){
            e1.health--;
            getWorld().removeObject(this);
        }
        if(e2 != null){
            e2.health--;
            getWorld().removeObject(this);
        }
    }
My Enemy conditional if statement in my Skeleton1 class:
if(health <= 0){
   getWorld().removeObject(this);
}
My Enemy conditional if statement in my Skeleton2 class:
if(health <= 0){
   getWorld().removeObject(this);
}
and i don't see any thing i did wrong please help.
SPower SPower

2016/6/8

#
You're going to have to tell where the two if-statements are located, at the very least.
AreebRaza AreebRaza

2016/6/8

#
and i get this error: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:695) at greenfoot.Actor.getX(Actor.java:157) at Skeleton1.act(Skeleton1.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:695) at greenfoot.Actor.getX(Actor.java:157) at Skeleton1.act(Skeleton1.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:695) at greenfoot.Actor.getX(Actor.java:157) at Skeleton1.act(Skeleton1.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:695) at greenfoot.Actor.getX(Actor.java:157) at Skeleton1.act(Skeleton1.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
SPower SPower

2016/6/8

#
In that case, the problem is in calling getWorld() after being possibly removed. In the first if statement in code-snippet 1, you call getWorld().removeObject(this). That means that 'this' is no longer in the world, so calling getWorld will raise an exception, meaning you can't call it in the second if statement. You need to change it to:
boolean removed = false;
if (e1 != null) {
    // stuff
    getWorld().removeObject(this);
    removed = true;
}
if (e2 != null) {
    // stuff
    if (!removed) {
        getWorld().removeObject(this);
    }
}
danpost danpost

2016/6/8

#
First, line 3 should start with 'Skeleton2' -- not 'SkeletonA2'. Next, you must check to see if the actor is still in the world before beginning the 'if' block at line 8 (the 'if' block starting at line 4 could potentially have the actor removed from the world). Line 8 can be changed to this:
if (getWorld() != null && e2 != null){
If you still have issues, you will need to be more specific as to what they are (exactly how it is not working; the behavior you are getting and the behavior you are wanting).
AreebRaza AreebRaza

2016/6/8

#
ok this is my full code for my Skeleton1 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Skelelton1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Skeleton1 extends Enemies
{
    /**
     * Act - do whatever the Skelelton1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int Speed = 1; 
     int vSpeed = 5;
     int gravity = 1; 
     private int frame = 1;
     private int animateCounter = 0;
     public static int health = 2;
     GreenfootImage run1r = new GreenfootImage("Example-skeleton - Copy (2).png");
     GreenfootImage run2r = new GreenfootImage("Example-skeleton - Copy (3).png");
     GreenfootImage run3r = new GreenfootImage("Example-skeleton - Copy (4).png");
     GreenfootImage run4r = new GreenfootImage("Example-skeleton - Copy (5).png");
     GreenfootImage run5r = new GreenfootImage("Example-skeleton - Copy (6).png");
     GreenfootImage run6r = new GreenfootImage("Example-skeleton - Copy.png");
     GreenfootImage run7r = new GreenfootImage("Example-skeleton.png");
     
     GreenfootImage run1l = new GreenfootImage("Example-skeleton - Copy - Copy (2) - Copy.png");
     GreenfootImage run2l = new GreenfootImage("Example-skeleton - Copy - Copy (2).png");
     GreenfootImage run3l = new GreenfootImage("Example-skeleton - Copy - Copy (3) - Copy.png");
     GreenfootImage run4l = new GreenfootImage("Example-skeleton - Copy - Copy (4) - Copy.png");
     GreenfootImage run5l = new GreenfootImage("Example-skeleton - Copy - Copy (5) - Copy.png");
     GreenfootImage run6l = new GreenfootImage("Example-skeleton - Copy - Copy (6) - Copy.png");
     GreenfootImage run7l = new GreenfootImage("Example-skeleton - Copy - Copy (7) - Copy.png");
     boolean chase = false;
     
    public void act() 
    {
        // Add your action code here.
            GreenfootImage currentImage = getImage();
            currentImage.scale(130,130);
            if(health <= 0){
                getWorld().removeObject(this);
            }
            if(FNinja.NinjaX >= getX() - 100 && health >= 0){
                 chase = true;
            }
            if(FNinja.NinjaX >= getX() + 100 && health >= 0){
                 chase = true;
            }
            if(chase){
               chaseNinja();
               
            }
           checkFall();
            animateCounter++;              
    }    
    
    public void animateRight(){
  
        if(frame == 1){
        setImage(run1r);
        }       
         if(frame == 2){
        setImage(run2r);
        }
         if(frame == 3){
        setImage(run3r);
        } 
         if(frame == 4){
        setImage(run4r);
        }
         if(frame == 5){
        setImage(run5r);
        }
         if(frame == 6){
        setImage(run6r);
        }
        if(frame == 7){
        setImage(run7r);
        frame = 1;
        }
        frame++;
        return;

    
   }
   public void animateLeft(){

        if(frame == 1){
        setImage(run1l);
        }       
         if(frame == 2){
        setImage(run2l);
        }
         if(frame == 3){
        setImage(run3l);
        } 
         if(frame == 4){
        setImage(run4l);
        }
         if(frame == 5){
        setImage(run5l);
        }
         if(frame == 6){
        setImage(run6l);
        }
        if(frame == 7){
        setImage(run7l);
        frame = 1;
        }
        frame++;
        return;

    
   }
    
    public void chaseNinja(){
       if(FNinja.NinjaX <= getX()){
           move(-Speed);
        if(animateCounter % 7 == 0){
           animateLeft();
        }
       }else{
           if(animateCounter % 7 == 0){
           animateRight();
        }
           move(Speed);
       }
    }
   
     public void fall(){

        setLocation(getX(), getY() + vSpeed);
        vSpeed += gravity;
    

    } 
    public void moveToGround(Actor ground){
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);

    } 
     public boolean onGround(){
        int SpriteHeight = getImage().getHeight();
        int LookForGround = (int)(SpriteHeight / 2);
        Actor ground = getOneObjectAtOffset(0, LookForGround, Ground.class);
        
        if(ground == null){
            return false;
        }else{
            moveToGround(ground);
            return true;
        }
    }
    public void checkFall(){
        if(onGround()){
            vSpeed = 0;
        }else{
            fall();
        }
    }  
    
    public static int getHealth() {
        return health;
    }
}
my full code for my Skeleton2 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Skelelton2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Skeleton2 extends Enemies
{
    /**
     * Act - do whatever the Skelelton2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     int Speed = 1; 
     int vSpeed = 5;
     int gravity = 1; 
     private int frame = 1;
     private int animateCounter = 0;
     
     GreenfootImage run1r = new GreenfootImage("Example-skeleton - Copy (2).png");
     GreenfootImage run2r = new GreenfootImage("Example-skeleton - Copy (3).png");
     GreenfootImage run3r = new GreenfootImage("Example-skeleton - Copy (4).png");
     GreenfootImage run4r = new GreenfootImage("Example-skeleton - Copy (5).png");
     GreenfootImage run5r = new GreenfootImage("Example-skeleton - Copy (6).png");
     GreenfootImage run6r = new GreenfootImage("Example-skeleton - Copy.png");
     GreenfootImage run7r = new GreenfootImage("Example-skeleton.png");
     
     GreenfootImage run1l = new GreenfootImage("Example-skeleton - Copy - Copy (2) - Copy.png");
     GreenfootImage run2l = new GreenfootImage("Example-skeleton - Copy - Copy (2).png");
     GreenfootImage run3l = new GreenfootImage("Example-skeleton - Copy - Copy (3) - Copy.png");
     GreenfootImage run4l = new GreenfootImage("Example-skeleton - Copy - Copy (4) - Copy.png");
     GreenfootImage run5l = new GreenfootImage("Example-skeleton - Copy - Copy (5) - Copy.png");
     GreenfootImage run6l = new GreenfootImage("Example-skeleton - Copy - Copy (6) - Copy.png");
     GreenfootImage run7l = new GreenfootImage("Example-skeleton - Copy - Copy (7) - Copy.png");
          
     boolean chase = false;
    public void act() 
    {
        // Add your action code here.
            GreenfootImage currentImage = getImage();
            currentImage.scale(130,130);

            if(FNinja.NinjaX >= getX() - 100){
                 chase = true;
            }
            if(FNinja.NinjaX >= getX() + 100){
                 chase = true;
            }
            if(chase){
               chaseNinja();
               
            }
           checkFall();
            animateCounter++;              
    }    
    
    public void animateRight(){
  
        if(frame == 1){
        setImage(run1r);
        }       
         if(frame == 2){
        setImage(run2r);
        }
         if(frame == 3){
        setImage(run3r);
        } 
         if(frame == 4){
        setImage(run4r);
        }
         if(frame == 5){
        setImage(run5r);
        }
         if(frame == 6){
        setImage(run6r);
        }
        if(frame == 7){
        setImage(run7r);
        frame = 1;
        }
        frame++;
        return;

    
   }
   public void animateLeft(){

        if(frame == 1){
        setImage(run1l);
        }       
         if(frame == 2){
        setImage(run2l);
        }
         if(frame == 3){
        setImage(run3l);
        } 
         if(frame == 4){
        setImage(run4l);
        }
         if(frame == 5){
        setImage(run5l);
        }
         if(frame == 6){
        setImage(run6l);
        }
        if(frame == 7){
        setImage(run7l);
        frame = 1;
        }
        frame++;
        return;

    
   }
    
    public void chaseNinja(){
       if(FNinja.NinjaX <= getX()){
           move(-Speed);
        if(animateCounter % 7 == 0){
           animateLeft();
        }
       }else{
           if(animateCounter % 7 == 0){
           animateRight();
        }
           move(Speed);
       }
    }
   
     public void fall(){

        setLocation(getX(), getY() + vSpeed);
        vSpeed += gravity;
    

    } 
    public void moveToGround(Actor ground){
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);

    } 
     public boolean onGround(){
        int SpriteHeight = getImage().getHeight();
        int LookForGround = (int)(SpriteHeight / 2);
        Actor ground = getOneObjectAtOffset(0, LookForGround, Ground.class);
        
        if(ground == null){
            return false;
        }else{
            moveToGround(ground);
            return true;
        }
    }
    public void checkFall(){
        if(onGround()){
            vSpeed = 0;
        }else{
            fall();
        }
    }
    public static int health = 5;
     
    public static void setHealth(int points) {
        health += points;
    }
    public static int getHealth() {
        return health;
    }
}
my full code for my shoot class
SPower SPower

2016/6/8

#
@danpost, correct me if I'm wrong, but I think calling getWorld() if an actor is not in a world will raise an exception by itself, not cause a null pointer exception to occur when doing something with the result.
danpost danpost

2016/6/8

#
SPower wrote...
@danpost, correct me if I'm wrong, but I think calling getWorld() if an actor is not in a world will raise an exception by itself, not cause a null pointer exception to occur when doing something with the result.
Calling 'getWorld' in itself does not cause the NullPointerException. Calling a method on 'getWorld' when 'getWorld' return a 'null' value will.
SPower SPower

2016/6/8

#
danpost wrote...
Calling 'getWorld' in itself does not cause the NullPointerException. Calling a method on 'getWorld' when 'getWorld' return a 'null' value will.
That's not what I was getting, I simply thought that getWorld() would raise an IllegalStateException when the actor is not in a world, not return null, but I was simply confused.
danpost danpost

2016/6/8

#
SPower wrote...
danpost wrote...
Calling 'getWorld' in itself does not cause the NullPointerException. Calling a method on 'getWorld' when 'getWorld' return a 'null' value will.
That's not what I was getting, I simply thought that getWorld() would raise an IllegalStateException when the actor is not in a world, not return null, but I was simply confused.
The 'getWorld' method does not require the actor to be in any world. It just returns the value of a private field of type World held by the Actor class that refers to the world the actor is in. If the actor is not in a world, the value of the field is 'null' and that is what 'getWorld' will return.
SPower SPower

2016/6/8

#
Yes, I got that at this point :)
AreebRaza AreebRaza

2016/6/8

#
danpost wrote...
First, line 3 should start with 'Skeleton2' -- not 'SkeletonA2'. Next, you must check to see if the actor is still in the world before beginning the 'if' block at line 8 (the 'if' block starting at line 4 could potentially have the actor removed from the world). Line 8 can be changed to this:
if (getWorld() != null && e2 != null){
If you still have issues, you will need to be more specific as to what they are (exactly how it is not working; the behavior you are getting and the behavior you are wanting).
i did every thing you told me and i still get the same error Skeleton1 class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Skelelton1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Skeleton1 extends Enemies
{
    /**
     * Act - do whatever the Skelelton1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int Speed = 1; 
     int vSpeed = 5;
     int gravity = 1; 
     private int frame = 1;
     private int animateCounter = 0;
     public static int health = 2;
     GreenfootImage run1r = new GreenfootImage("Example-skeleton - Copy (2).png");
     GreenfootImage run2r = new GreenfootImage("Example-skeleton - Copy (3).png");
     GreenfootImage run3r = new GreenfootImage("Example-skeleton - Copy (4).png");
     GreenfootImage run4r = new GreenfootImage("Example-skeleton - Copy (5).png");
     GreenfootImage run5r = new GreenfootImage("Example-skeleton - Copy (6).png");
     GreenfootImage run6r = new GreenfootImage("Example-skeleton - Copy.png");
     GreenfootImage run7r = new GreenfootImage("Example-skeleton.png");
     
     GreenfootImage run1l = new GreenfootImage("Example-skeleton - Copy - Copy (2) - Copy.png");
     GreenfootImage run2l = new GreenfootImage("Example-skeleton - Copy - Copy (2).png");
     GreenfootImage run3l = new GreenfootImage("Example-skeleton - Copy - Copy (3) - Copy.png");
     GreenfootImage run4l = new GreenfootImage("Example-skeleton - Copy - Copy (4) - Copy.png");
     GreenfootImage run5l = new GreenfootImage("Example-skeleton - Copy - Copy (5) - Copy.png");
     GreenfootImage run6l = new GreenfootImage("Example-skeleton - Copy - Copy (6) - Copy.png");
     GreenfootImage run7l = new GreenfootImage("Example-skeleton - Copy - Copy (7) - Copy.png");
     boolean chase = false;
     
    public void act() 
    {
        // Add your action code here.
            GreenfootImage currentImage = getImage();
            currentImage.scale(130,130);
            if(health <= 0){
                getWorld().removeObject(this);
            }
            if(FNinja.NinjaX >= getX() - 100){
                 chase = true;
            }
            if(FNinja.NinjaX >= getX() + 100){
                 chase = true;
            }
            if(chase){
               chaseNinja();
               
            }
           checkFall();
            animateCounter++;              
    }    
    
    public void animateRight(){
  
        if(frame == 1){
        setImage(run1r);
        }       
         if(frame == 2){
        setImage(run2r);
        }
         if(frame == 3){
        setImage(run3r);
        } 
         if(frame == 4){
        setImage(run4r);
        }
         if(frame == 5){
        setImage(run5r);
        }
         if(frame == 6){
        setImage(run6r);
        }
        if(frame == 7){
        setImage(run7r);
        frame = 1;
        }
        frame++;
        return;

    
   }
   public void animateLeft(){

        if(frame == 1){
        setImage(run1l);
        }       
         if(frame == 2){
        setImage(run2l);
        }
         if(frame == 3){
        setImage(run3l);
        } 
         if(frame == 4){
        setImage(run4l);
        }
         if(frame == 5){
        setImage(run5l);
        }
         if(frame == 6){
        setImage(run6l);
        }
        if(frame == 7){
        setImage(run7l);
        frame = 1;
        }
        frame++;
        return;

    
   }
    
    public void chaseNinja(){
       if(FNinja.NinjaX <= getX()){
           move(-Speed);
        if(animateCounter % 7 == 0){
           animateLeft();
        }
       }else{
           if(animateCounter % 7 == 0){
           animateRight();
        }
           move(Speed);
       }
    }
   
     public void fall(){

        setLocation(getX(), getY() + vSpeed);
        vSpeed += gravity;
    

    } 
    public void moveToGround(Actor ground){
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);

    } 
     public boolean onGround(){
        int SpriteHeight = getImage().getHeight();
        int LookForGround = (int)(SpriteHeight / 2);
        Actor ground = getOneObjectAtOffset(0, LookForGround, Ground.class);
        
        if(ground == null){
            return false;
        }else{
            moveToGround(ground);
            return true;
        }
    }
    public void checkFall(){
        if(onGround()){
            vSpeed = 0;
        }else{
            fall();
        }
    }  
    
    public static int getHealth() {
        return health;
    }
}
[code]import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Skelelton2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Skeleton2 extends Enemies
{
    /**
     * Act - do whatever the Skelelton2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     int Speed = 1; 
     int vSpeed = 5;
     int gravity = 1; 
     private int frame = 1;
     private int animateCounter = 0;
     public static int health = 2;
     GreenfootImage run1r = new GreenfootImage("Example-skeleton - Copy (2).png");
     GreenfootImage run2r = new GreenfootImage("Example-skeleton - Copy (3).png");
     GreenfootImage run3r = new GreenfootImage("Example-skeleton - Copy (4).png");
     GreenfootImage run4r = new GreenfootImage("Example-skeleton - Copy (5).png");
     GreenfootImage run5r = new GreenfootImage("Example-skeleton - Copy (6).png");
     GreenfootImage run6r = new GreenfootImage("Example-skeleton - Copy.png");
     GreenfootImage run7r = new GreenfootImage("Example-skeleton.png");
     
     GreenfootImage run1l = new GreenfootImage("Example-skeleton - Copy - Copy (2) - Copy.png");
     GreenfootImage run2l = new GreenfootImage("Example-skeleton - Copy - Copy (2).png");
     GreenfootImage run3l = new GreenfootImage("Example-skeleton - Copy - Copy (3) - Copy.png");
     GreenfootImage run4l = new GreenfootImage("Example-skeleton - Copy - Copy (4) - Copy.png");
     GreenfootImage run5l = new GreenfootImage("Example-skeleton - Copy - Copy (5) - Copy.png");
     GreenfootImage run6l = new GreenfootImage("Example-skeleton - Copy - Copy (6) - Copy.png");
     GreenfootImage run7l = new GreenfootImage("Example-skeleton - Copy - Copy (7) - Copy.png");
          
     boolean chase = false;
    public void act() 
    {
        // Add your action code here.
            GreenfootImage currentImage = getImage();
            currentImage.scale(130,130);
            if(FNinja.NinjaX >= getX() - 100){
                 chase = true;
            }
            if(FNinja.NinjaX >= getX() + 100){
                 chase = true;
            }
            if(chase){
               chaseNinja();
               
            }
           checkFall();
            animateCounter++;              
    }    
    
    public void animateRight(){
  
        if(frame == 1){
        setImage(run1r);
        }       
         if(frame == 2){
        setImage(run2r);
        }
         if(frame == 3){
        setImage(run3r);
        } 
         if(frame == 4){
        setImage(run4r);
        }
         if(frame == 5){
        setImage(run5r);
        }
         if(frame == 6){
        setImage(run6r);
        }
        if(frame == 7){
        setImage(run7r);
        frame = 1;
        }
        frame++;
        return;

    
   }
   public void animateLeft(){

        if(frame == 1){
        setImage(run1l);
        }       
         if(frame == 2){
        setImage(run2l);
        }
         if(frame == 3){
        setImage(run3l);
        } 
         if(frame == 4){
        setImage(run4l);
        }
         if(frame == 5){
        setImage(run5l);
        }
         if(frame == 6){
        setImage(run6l);
        }
        if(frame == 7){
        setImage(run7l);
        frame = 1;
        }
        frame++;
        return;

    
   }
    
    public void chaseNinja(){
       if(FNinja.NinjaX <= getX()){
           move(-Speed);
        if(animateCounter % 7 == 0){
           animateLeft();
        }
       }else{
           if(animateCounter % 7 == 0){
           animateRight();
        }
           move(Speed);
       }
    }
   
     public void fall(){

        setLocation(getX(), getY() + vSpeed);
        vSpeed += gravity;
    

    } 
    public void moveToGround(Actor ground){
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);

    } 
     public boolean onGround(){
        int SpriteHeight = getImage().getHeight();
        int LookForGround = (int)(SpriteHeight / 2);
        Actor ground = getOneObjectAtOffset(0, LookForGround, Ground.class);
        
        if(ground == null){
            return false;
        }else{
            moveToGround(ground);
            return true;
        }
    }
    public void checkFall(){
        if(onGround()){
            vSpeed = 0;
        }else{
            fall();
        }
    }
    
}
hitEnemy Method:
public void hitEnemy(){
        Skeleton1 e1 = (Skeleton1) getOneObjectAtOffset(0,0, Skeleton1.class);
        Skeleton2 e2 = (Skeleton2) getOneObjectAtOffset(0,0, Skeleton2.class);
        if (getWorld() != null && e1 != null){
            e1.health--;
            getWorld().removeObject(this);
        }
        if (getWorld() != null && e2 != null){
            e2.health--;
            getWorld().removeObject(this);
        }
    }
AreebRaza AreebRaza

2016/6/8

#
can somebody please reply
danpost danpost

2016/6/8

#
AreebRaza wrote...
can somebody please reply
Insert at line 45 of the Skeleton1 class (and the Skeleton2 class) the following:
return;
AreebRaza AreebRaza

2016/6/8

#
You mean like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Skelelton1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Skeleton1 extends Enemies
{
    /**
     * Act - do whatever the Skelelton1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int Speed = 1; 
     int vSpeed = 5;
     int gravity = 1; 
     private int frame = 1;
     private int animateCounter = 0;
     public static int health = 2;
     GreenfootImage run1r = new GreenfootImage("Example-skeleton - Copy (2).png");
     GreenfootImage run2r = new GreenfootImage("Example-skeleton - Copy (3).png");
     GreenfootImage run3r = new GreenfootImage("Example-skeleton - Copy (4).png");
     GreenfootImage run4r = new GreenfootImage("Example-skeleton - Copy (5).png");
     GreenfootImage run5r = new GreenfootImage("Example-skeleton - Copy (6).png");
     GreenfootImage run6r = new GreenfootImage("Example-skeleton - Copy.png");
     GreenfootImage run7r = new GreenfootImage("Example-skeleton.png");
      
     GreenfootImage run1l = new GreenfootImage("Example-skeleton - Copy - Copy (2) - Copy.png");
     GreenfootImage run2l = new GreenfootImage("Example-skeleton - Copy - Copy (2).png");
     GreenfootImage run3l = new GreenfootImage("Example-skeleton - Copy - Copy (3) - Copy.png");
     GreenfootImage run4l = new GreenfootImage("Example-skeleton - Copy - Copy (4) - Copy.png");
     GreenfootImage run5l = new GreenfootImage("Example-skeleton - Copy - Copy (5) - Copy.png");
     GreenfootImage run6l = new GreenfootImage("Example-skeleton - Copy - Copy (6) - Copy.png");
     GreenfootImage run7l = new GreenfootImage("Example-skeleton - Copy - Copy (7) - Copy.png");
     boolean chase = false;
      
    public void act() 
    {
        // Add your action code here.
            GreenfootImage currentImage = getImage();
            currentImage.scale(130,130);
            if(health <= 0){
                getWorld().removeObject(this);
            }
return;
            if(FNinja.NinjaX >= getX() - 100){
                 chase = true;
            }
            if(FNinja.NinjaX >= getX() + 100){
                 chase = true;
            }
            if(chase){
               chaseNinja();
                
            }
           checkFall();
            animateCounter++;              
    }    
     
    public void animateRight(){
   
        if(frame == 1){
        setImage(run1r);
        }       
         if(frame == 2){
        setImage(run2r);
        }
         if(frame == 3){
        setImage(run3r);
        } 
         if(frame == 4){
        setImage(run4r);
        }
         if(frame == 5){
        setImage(run5r);
        }
         if(frame == 6){
        setImage(run6r);
        }
        if(frame == 7){
        setImage(run7r);
        frame = 1;
        }
        frame++;
        return;
 
     
   }
   public void animateLeft(){
 
        if(frame == 1){
        setImage(run1l);
        }       
         if(frame == 2){
        setImage(run2l);
        }
         if(frame == 3){
        setImage(run3l);
        } 
         if(frame == 4){
        setImage(run4l);
        }
         if(frame == 5){
        setImage(run5l);
        }
         if(frame == 6){
        setImage(run6l);
        }
        if(frame == 7){
        setImage(run7l);
        frame = 1;
        }
        frame++;
        return;
 
     
   }
     
    public void chaseNinja(){
       if(FNinja.NinjaX <= getX()){
           move(-Speed);
        if(animateCounter % 7 == 0){
           animateLeft();
        }
       }else{
           if(animateCounter % 7 == 0){
           animateRight();
        }
           move(Speed);
       }
    }
    
     public void fall(){
 
        setLocation(getX(), getY() + vSpeed);
        vSpeed += gravity;
     
 
    } 
    public void moveToGround(Actor ground){
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
         
        setLocation(getX(), newY);
 
    } 
     public boolean onGround(){
        int SpriteHeight = getImage().getHeight();
        int LookForGround = (int)(SpriteHeight / 2);
        Actor ground = getOneObjectAtOffset(0, LookForGround, Ground.class);
         
        if(ground == null){
            return false;
        }else{
            moveToGround(ground);
            return true;
        }
    }
    public void checkFall(){
        if(onGround()){
            vSpeed = 0;
        }else{
            fall();
        }
    }  
     
    public static int getHealth() {
        return health;
    }
}
There are more replies on the next page.
1
2