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

2014/10/7

Add objects whit different image

vbh vbh

2014/10/7

#
Hi im trying to add the same object 5 times whit different image, but im stuck. this is the code i have, and its not working :-/ any help will be appreciated private GreenfootImageshields = {new GreenfootImage("shield1.gif"),new GreenfootImage("shield2.gif"), new GreenfootImage("shield3.gif"),new GreenfootImage("shield4.gif"), new GreenfootImage("shield5.gif")}; /** * Constructor for objects of class ForestWorld. * */ public ForestWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); populateWorld(); } public void populateWorld() { int i = 0; while(i < shields.length) { Shield shield = new Shield(shields + ".gif"); addObject(shield, Greenfoot.getRandomNumber(500), Greenfoot.getRandomNumber(300)); } }
danpost danpost

2014/10/7

#
You have the suffix for your image filenames, '.gif', included in the array; but, then you add it again when you create the shields. The shields will then try to find the filename with two suffixes, '.gif.gif'.
davmac davmac

2014/10/7

#
In fact, the 'shields' array is an array of GreenfootImage, not of String. So this line:
 Shield shield = new Shield(shields[i] + ".gif");
... should not actually compile. Since you did not post the code for your Shield class, I don't know whether the constructor expects a string or a GreenfootImage parameter. Please use code tags to avoid the formatting issues that you have in your post.
You need to login to post a reply.