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

2018/12/12

why setImage doesn't work?

Actle Actle

2018/12/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Zombie extends Actor
{   
    private static int[] speed = {1,2,9,3,1,1,3,2,3,3};
    private static int[] healthpoint = {50,50,55,56,55,50,52,40,50,55};
    int SP,HP;
    Zombie zombie[] = new Zombie[10];
    private GreenfootImage easy = new GreenfootImage("easyzombie.png");
    private GreenfootImage normal= new GreenfootImage("normalzombie.png");
    private GreenfootImage super= new GreenfootImage("superZombie.png");
    void setSP(int e){
        SP = e;
    }
    void setHP(int k){
        HP = k;
    }
    int getSP(){
        return SP;
    }
    int getHP(){
        return HP;
    }
    public void setvalue() 
    {
    for(int i = 0; i < 10; i++){ 
         zombie[i].setSP(Greenfoot.getRandomNumber(speed[i])); 
         zombie[i].setHP(Greenfoot.getRandomNumber(healthpoint[i])); 
    }  
    
    }
    public void setpicture() {
        for(int i = 0; i < 10; i++){ 
           if ((zombie[i].getSP()>5)&&(zombie[i].getHP()>5))
                zombie[i].setImage(super);
           else if((zombie[i].getSP()>2)&&(zombie[i].getHP()<2))
                zombie[i].setImage(easy);
           else
           {
                zombie[i].setImage(normal);
           }
           }
    }
   }
Actle Actle

2018/12/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ZombieWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ZombieWorld extends World
{

    public ZombieWorld()
    {    
        super(800, 600, 1); 
        raiseTheDead();
    }
    private void raiseTheDead()
    {

        for(int i = 0; i < 10; i++){ 
        addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }    
    }
    
    }
Actle Actle

2018/12/12

#
I've changed super to superzom and it still doesn't work
nccb nccb

2018/12/12

#
Are you calling the setpicture method anywhere? I can't see a call to it in the code you've given.
danpost danpost

2018/12/12

#
I do not see where your Zombie array is being filled with Zombie objects. Line 7 creates an array as follows:
Zombie[] zombies = { null, null, null, null, null, null, null, null, null, null };
Lines 26, 27, 33, 34, 35, 36 and 39 will all fail and throw a NullPointerException unless you put Zombie objects in the array.
Actle Actle

2018/12/12

#
南京市商业银行 wrote...
Are you calling the setpicture method anywhere? I can't see a call to it in the code you've given.
how?I thought for a long time but couldn't come up with the answer.
Actle Actle

2018/12/12

#
danpost wrote...
I do not see where your Zombie array is being filled with Zombie objects. Line 7 creates an array as follows:
Zombie[] zombies = { null, null, null, null, null, null, null, null, null, null };
Lines 26, 27, 33, 34, 35, 36 and 39 will all fail and throw a NullPointerException unless you put Zombie objects in the array.
My problem has been finally solved. Thank you
danpost danpost

2018/12/12

#
Actle wrote...
how?I thought for a long time but couldn't come up with the answer.
Add a constructor to the class and initialize the zombies through code there. You will probably need a static int counter to track which zombie is being created:
// add class field
private static int zCount;

// constructor
public Zombie()
{
    zCount = (zCount+1)%10;
    SP = speed[zCount]; // or Greenfoot.getRandomNumber(speed[zCount]);
    HP = healthPoints[zCount]; // or Greenfoot.getRandomNumber(healthPoints[zCount]);
    String prefix = null;
    switch (SP)
    {
        case 0: case 1: case 2: prefix = "easyz"; break;
        case 3: case 4: case 5: prefix = "normalz"; break;
        default: prefix = "superZ"; break;
    }
    setImage(new GreenfootImage(prefix+"ombie.png"));
}
With this, I guess you can eliminate everything after line 6 in your Zombie class above. Oh well, nevermind (if you got it fixed already).
Actle Actle

2018/12/12

#
danpost wrote...
Actle wrote...
how?I thought for a long time but couldn't come up with the answer.
Add a constructor to the class and initialize the zombies through code there. You will probably need a static int counter to track which zombie is being created:
// add class field
private static int zCount;

// constructor
public Zombie()
{
    zCount = (zCount+1)%10;
    SP = speed[zCount]; // or Greenfoot.getRandomNumber(speed[zCount]);
    HP = healthPoints[zCount]; // or Greenfoot.getRandomNumber(healthPoints[zCount]);
    String prefix = null;
    switch (SP)
    {
        case 0: case 1: case 2: prefix = "easyz"; break;
        case 3: case 4: case 5: prefix = "normalz"; break;
        default: prefix = "superZ"; break;
    }
    setImage(new GreenfootImage(prefix+"ombie.png"));
}
With this, I guess you can eliminate everything after line 6 in your Zombie class above. Oh well, nevermind (if you got it fixed already).
thx so much
You need to login to post a reply.