Hi there, I am looking for some help on my greenfoot project please.
Basically I have an Arraylist storing 4 images within an actor class called ScreenImages.
I have a method in the World which creates a seperate object for each image in the arraylist.
I want to be able to for example click on the image at index 0 and make it go to a new world. If I click on the image at index 1 it will go to a different world etc, but I cannot seem to get it working.
I have tried mouseClicked(null) and mouseClicked(this) but neither work properly for this, the null goes to the new world but from clicking anywhere on the screen.
Thanks in advance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class ScreenImages extends Actor { private int arrayIndex; // An array to store all images GreenfootImage[] appImages = { new GreenfootImage( "music.png" ), new GreenfootImage( "calculator.png" ), new GreenfootImage( "photos.png" ), new GreenfootImage( "videos.png" ), }; /** * ScreenImages Constructor * * @param arrayIndex A parameter */ public ScreenImages( int arrayIndex) { this .arrayIndex = arrayIndex; setImage(appImages[arrayIndex]); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public void screenItems() { ScreenImages player1 = new ScreenImages( 0 ); addObject(player1, 229 , 398 ); ScreenImages player2 = new ScreenImages( 1 ); addObject(player2, 105 , 205 ); ScreenImages player3 = new ScreenImages( 2 ); addObject(player3, 190 , 205 ); ScreenImages player4 = new ScreenImages( 3 ); addObject(player4, 271 , 205 ); if (Greenfoot.mouseClicked(player2)) { Greenfoot.setWorld( new NextWorld() ); } } |