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

2015/12/15

Testing if mouseClicked on one class from another class

dkwondo dkwondo

2015/12/15

#
Hello. I've been having this one issue with my code for quite some time. I'm trying to make a matching game, in which the first match to be made is a kangaroo. From one class, I want if the kangaroo is clicked, to respond by removing it, and playing an audio recording saying congratulations(these two parts aren't an issue, I know they aren't in my code). However, my class simply does not respond when the kangaroo is clicked. Right now I can click anywhere, and the kangaroo will still disappear(obviously not what I want). Any feedback would be greatly appreciated!.
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
public class goButton extends Actor
{
   public void act()
    {
        kangaroo();
   }
   public void kangaroo()
   {
       Actor kangaroo;
       kangaroo = getOneIntersectingObject(Kangaroo.class);
        if (Greenfoot.mouseClicked(this))
        {
            Greenfoot.playSound("kangaroo.wav");
        }
        if ( Greenfoot.mouseClicked(kangaroo))
        {
            getWorld().removeObjects(getWorld().getObjects(Kangaroo.class));
            // play congrats sound here
        }
        else
        {
            // play try again sound here
        }
    }
}
Super_Hippo Super_Hippo

2015/12/15

#
If the 'goButton' does not intersect with a Kangaroo object, then 'kangaroo' is 'null' (line 10). So line 15 is true for every click and line 17 removes all kangaroos from the world. You could change line 15 to this:
1
if (kangaroo != null && Greenfoot.mouseClicked(kangaroo))
One thing to line 22. If you play a sound there, then this sound will be played immediately after the goButton object is added to the world and will be played every act-cycle.
dkwondo dkwondo

2015/12/15

#
Super_Hippo wrote...
If the 'goButton' does not intersect with a Kangaroo object, then 'kangaroo' is 'null' (line 10). So line 15 is true for every click and line 17 removes all kangaroos from the world. You could change line 15 to this:
1
if (kangaroo != null && Greenfoot.mouseClicked(kangaroo))
One thing to line 22. If you play a sound there, then this sound will be played immediately after the goButton object is added to the world and will be played every act-cycle.
Thank you for responding so quickly(also thank you for your line 22 bit, I would've been so confused on that). Your line 15 solution did not solve. It compiled, but when I went to click on the Kangaroo, nothing happened.
danpost danpost

2015/12/16

#
I think the main problem is due to the use of 'getOneIntersectingObject(Kangaroo.class)'. With it, the kangaroo must be overlapping the goButton object. I do not believe that is what you want (remove lines 9 and 10). What I think you need to do is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (Greenfoot.mouseClicked(null))
{
    Actor mouseActor = Greenfoot.getMouseInfo().getActor();
    if (mouseActor instanceof Kangaroo)
    {
        getWorld().removeObject(mouseActor);
        // play congrats sound here
    }
    else if (mouseActor == this)
    {
        Greenfoot.playSound("Kangaroo.wav");
    }
    else
    {
        // play try again sound here
    }
}
dkwondo dkwondo

2015/12/16

#
danpost wrote...
I think the main problem is due to the use of 'getOneIntersectingObject(Kangaroo.class)'. With it, the kangaroo must be overlapping the goButton object. I do not believe that is what you want (remove lines 9 and 10). What I think you need to do is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (Greenfoot.mouseClicked(null))
{
    Actor mouseActor = Greenfoot.getMouseInfo().getActor();
    if (mouseActor instanceof Kangaroo)
    {
        getWorld().removeObject(mouseActor);
        // play congrats sound here
    }
    else if (mouseActor == this)
    {
        Greenfoot.playSound("Kangaroo.wav");
    }
    else
    {
        // play try again sound here
    }
}
That actually worked out perfectly!! Thank you very much :))
You need to login to post a reply.