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

2017/9/1

How to use mouse clicks on an object

DankBoi DankBoi

2017/9/1

#
I'm trying to create a player selection world for a game I'm creating. The character chosen by clicking should be the one added to the main world class (Camp). The problem is that I can't figure out how to program the mouse clicking, as most other posts seem to be based on... something else? In one of the example scenarios I did with my class this summer, click detection was as simple as using Greenfoot.mouseClicked(this); but apparently it can be far more complex? Camp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Camp extends World
{
 
    GreenfootSound backgroundMusic = new GreenfootSound("Lobo Loco.mp3");
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public Camp ()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         
        Greenfoot.setWorld(new PlayerSelection());
        prepare();
        setPaintOrder(Tent.class, Block.class);
        setPaintOrder(Flag.class, Tent.class, Block.class);
        backgroundMusic.playLoop();       
    }
 
    public void stopped()
    {
      backgroundMusic.setVolume(0);  //change bg to what you declared the file as
    }
     
    public void started()
    {
       backgroundMusic.setVolume(100);    //change bg to what you declared the file as
    }
     
    public void act()
    {
        showPic();
    }
     
    public void showPic()
    {
        GreenfootImage KalasImage;
        KalasImage= new GreenfootImage("TransparentKalas.png");
        KalasImage.drawImage(KalasImage, 300, 200);
    }
     
    public void playSoundtrack()
    {
        Greenfoot.playSound("Lobo Loco.mp3");
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Kalas kalas = new Kalas(50,75);
        addObject(kalas,394,234);
        Tent TheTent = new Tent();
        addObject(TheTent,126,5);
        TheTent.setLocation(130,19);
        Block block = new Block(25, 200);
        addObject(block,373,113);
        block.setLocation(278,93);
        Block block2 = new Block(100, 25);
        addObject(block2,275,310);
        block2.setLocation(217,181);
        Block block3 = new Block(100, 25);
        addObject(block3,280,269);
        block3.setLocation(55,178);
        Flag flag = new Flag();
        addObject(flag, 345,135);      
    }
}
PlayerSelection import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class PlayerSelection here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PlayerSelection extends World
{
    boolean KalasChosen;
    boolean MailfistChosen;
         
    /**
     * Constructor for objects of class PlayerSelection.
     *
     */
    public PlayerSelection()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        makeCharacters();
        showLabels();
    }
     
    public void showLabels()
    {
        showText("CHOOSE YOUR PLAYER BOI", getWidth()/2, 50);
        showText("Chiseled Pecs...", 175, 100);
        showText("Or Chiseled Abs?", 500, 100);
    }
     
    public void makeCharacters()
    {
        Kalas kalas = new Kalas(150, 225);
        addObject(kalas, 175, 250);
        Mailfist mailfist = new Mailfist(150, 225);
        addObject(mailfist, 500, 250);
    }
     
    public void selectCharacter()
    {
        if (Greenfoot.mouseClicked(Kalas.class))
        {
            KalasChosen = true;
            Greenfoot.setWorld(new Camp());
        }
         
        if (Greenfoot.mouseClicked(Mailfist.class))
        {
            MailfistChosen = true;
            Greenfoot.setWorld(new Camp());
        }   
    }
}
Agent40 Agent40

2017/9/1

#
I'd recommend checking if the click is registering first if you haven't already done so. It may just be that the click is registering but the Boolean value for each character isn't activating.
danpost danpost

2017/9/1

#
A couple of things strike me funny here. Line 23 in the Camp class will have a PlayerSelection world run every time a Camp world is created. That means every time you create a new Camp world from the PlayerSelection world, another PlayerSelection world will run (an endless looping). Second thing is that the 'Greenfoot.mouseClicked' method requires an Actor or World object (something within your scenario that can actually be clicked on) for its argument, not a class (as you have in lines 43 and 49 of the PlayerSelection class). You can remove line 23 in the Camp class and manually create a PlayerSelection world object by right clicking on the class and selecting 'new PlayerSelection()'. The should stop the looping without altering the visible order of things. In the PlayerSelection class, move lines 35 and 37 outside of the method and change the classes in lines 43 and 49 to 'kalas' and 'mailfist'. Finally, replace lines 46 and 52 with the following:
1
2
Camp camp = new Camp();
Greenfoot.setWorld(camp);
and follow them with code to add the appropriate character into the new Camp world. For example:
1
camp.addObject(new Kalas(50, 75), 394 234);
You will probably need to remove lines 63 and 64 of the Camp class. Oh, and you can remove lines 11, 12, 45 and 51 from the PlayerSelection class also.
danpost danpost

2017/9/1

#
Another thing is that in the Camp class, lines 40 through 50 don't do anything. The 'showPic' method creates an image, draws a part of its image on itself (if the image is at least (300, 200) in size and then does nothing more with it (no change to your world, actors or field values). Each time the 'showPics' method is done executing, the image it created is lost. Having the 'act' method do that every act step is just wasteful. You can remove lines 40 through 50 without changing anything.
DankBoi DankBoi

2017/9/2

#
Thanks for the help. I implemented the changes you mentioned, but the clicks still don't do anything. How can I check that it's being registered?
danpost danpost

2017/9/2

#
DankBoi wrote...
Thanks for the help. I implemented the changes you mentioned, but the clicks still don't do anything. How can I check that it's being registered?
Show what codes you now have in the Camp and PlayerSelection classes.
You need to login to post a reply.