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

2013/10/27

mouseClicked() to new world

dc2-typer dc2-typer

2013/10/27

#
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.
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]);
    }  
}
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
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() );
        }
    }
JetLennit JetLennit

2013/10/27

#
I think that this should work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import greenfoot.*;
 
public class ScreenImages extends Actor 
    private int arrayIndex;  
   
    // An array to store all images 
    String[] appImages = {  
            "music.png", "calculator.png", "photos.png", "videos.png"
        };   
   
    /**
     * ScreenImages Constructor
     *
     * @param arrayIndex A parameter
     */ 
    public ScreenImages(int arrayIndex)   
    {   
        this.arrayIndex = arrayIndex;   
        setImage(new GreenfootImage(appImages[arrayIndex]));  
    }    
}
But I wrote this in browser and it may not work (and I began writing this before you edited)
dc2-typer dc2-typer

2013/10/27

#
Hi JetLennit, Thank you for the reply. This works for the images also and is much neater so thank you. :) My problem is actually when I want to click the images to go to new worlds it won't allow me.
JetLennit JetLennit

2013/10/27

#
I just found the problem..... try 1. Removing the if statement in screen items, 2. Moving all the in-method actor declarations (ScreenImages player1 = new ScreenImages(0);, ect.) to normal actor declarations 3.Add this method in the world
1
2
3
4
public void act()
{
     if(Greenfoot.mouseClicked(player2)) Greenfoot.setWorld(new NextWorld() );
}
dc2-typer dc2-typer

2013/10/27

#
Can I just ask you quickly about part 2 Where do I move these in-method actor declarations to? They are currently declared in the world in the screenItems() method? Thanks again : )
JetLennit JetLennit

2013/10/27

#
Put them somewhere outside of a method
danpost danpost

2013/10/27

#
I think what you want is something like this:
1
2
3
4
5
6
7
8
9
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        if (arrayIndex == 0) Greenfoot.setWorld(new MusicWorld());
        if (arrayIndex == 1) Greenfoot.setWorld(new CalculatorWorld());
        // etc.
    }
}
dc2-typer dc2-typer

2013/10/27

#
Thanks for the replies. @danpost - I have tried putting the above code in the actor class, it compiles and there is no errors etc, but when I click on the image nothing happens. When I put it in the World subclass I get an error saying it can't find variable arrayIndex
danpost danpost

2013/10/27

#
It supposed to be in the ScreenImages class. And it should work, unless some other code or actor(s) in your project is preventing the proper detection.
dc2-typer dc2-typer

2013/10/27

#
@danpost You are right, I had another actor stopping it from working for some reason. Thank you danpost and JetLennit for the help I really appreciate it. :)
You need to login to post a reply.