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

2017/1/21

File cannot be null in array of Images

Huntwa Huntwa

2017/1/21

#
All of my images are perfectly in the images folder and I've made an array with a corresponding for loop (which I've done successfully in previous games) but for some reason I'm receiving errors, can anybody help? my code:
 private String[] images = new String[13];
    public void Protag(){
        for(int i=1; i<13; i++)
            images[i] = "Player" + i + ".png";
    }
    public void act(){
       movement();
    }  
    public void movement(){
        if(Greenfoot.isKeyDown("W")){
            setImage(images[4]);
            setLocation(getX(),getY()-3);
        }
        if(Greenfoot.isKeyDown("A")){
            setImage(images[7]);
            setLocation(getX()-3,getY());
        }
        if(Greenfoot.isKeyDown("S")){
            setImage(images[1]);
            setLocation(getX(),getY()+3);
        }
        if(Greenfoot.isKeyDown("D")){
            setImage(images[10]);
            setLocation(getX()+3,getY());
        }
    }
Super_Hippo Super_Hippo

2017/1/21

#
I guess the class is named Protag and lines 2-5 should be the constructor. Try to remove the 'void' in line 2.
Huntwa Huntwa

2017/1/21

#
oh my god. Thank you.
Nosson1459 Nosson1459

2017/1/22

#
A side thing which I noticed in your code is that you have your for-loop going from 1 - 12 instead of from 0 - 12 for your "
String[13]
" (Arrays are zero-based so they start from 0 and go to one less than the number you put in, which you obviously know already because of "i<13").
You need to login to post a reply.