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

2014/12/9

mouse clicked

Korina Korina

2014/12/9

#
I want to change this code: if(this.getOneIntersectingObject(AnnoyingPig.class) != null) instead of intersecting object it will be mouse clicked. Can anyone help me with this? Thank you!
danpost danpost

2014/12/9

#
Since the object that 'this' refers to will no longer be involved, you should probably remove the code from 'this' class to the AnnoyingPig class and test for mouseclick on 'this' there.
Korina Korina

2014/12/9

#
I am so sorry but how will I code that?
danpost danpost

2014/12/9

#
Korina wrote...
I am so sorry but how will I code that?
From within the AnnoyingPig class:
1
if (Greenfoot.mouseClicked(this))
Korina Korina

2014/12/9

#
Oh yes yes I used that. Here:
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
public void actions()
    {
        greenfoot.MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(this))
        {
            setLocation(0,0);
            checkForPig();
        }
 
        if(Greenfoot.mouseClicked(this))
        {
            setLocation(1,0);
            checkForPig();
        }
 
       if(Greenfoot.mouseClicked(this))
        {
            setLocation(2,0);
            checkForPig();
        }
             
        if(Greenfoot.mouseClicked(this))
        {
            setLocation(0,1);
            checkForPig();
        }
             
       if(Greenfoot.mouseClicked(this))
        {
            setLocation(1,1);
            checkForPig();
        }
             
        if(Greenfoot.mouseClicked(this))
        {
            setLocation(2,1);
            checkForPig();
        }
             
        if(Greenfoot.mouseClicked(this))
        {
            setLocation(0,2);
            checkForPig();
        }
             
        if(Greenfoot.mouseClicked(this))
        {
            setLocation(1,2);
            checkForPig();
        }
             
        if(Greenfoot.mouseClicked(this))
        {
            setLocation(2,2);
            checkForPig();
      
        }
     }
     
    /**
     * Checks to see if a mole is present where the hammer is.
     * If it is then it is a hit.
     */
    public void checkForPig()
    {
        if(this.getOneIntersectingObject(AnnoyingPig.class) != null)
        {
            playHit();
        }
         
        else
        {
            playMiss();
        }
    }
But how about in the checkForPig
danpost danpost

2014/12/9

#
In lines 4 through 57, if the first 'if' is true, then all will be and the final result of all that code would be the location (2, 2) action. Actually, you do not need ANY of the code posted above in your last post. The pig can check for clicks on them and do whatever (play sound, add to score, etc) and the world can check for misses and do whatever (play sound, deduct from score, etc). All the hammer needs to do is follow the mouse.
danpost danpost

2014/12/9

#
NVM, if the hammer is following the mouse, then the hammer will get clicked on -- not the pigs. Still the code you posted last will not work as you might expect. Maybe the mouse clicks should be all tested within the world class. The world can see if a pig is at a clicked location easily enough and perform what actions needs done for both hits and misses, as well as keep score (if there is one). You would detect any click (using 'null' instead of an object in the 'mouseClicked' command), get a non-null MouseInfo object to extract its X and Y click coordinates. With those coordinates, the world can 'getObjectsAt' to see if any pig is at the clicked location. The rest is dependent on the results of that check.
Korina Korina

2014/12/9

#
Okay I get it. Now I can click the actor . But the problem is the ouch image appears even though I am not clicking.
1
2
3
4
5
6
7
8
public void ouchIfHit()
    {
        greenfoot.MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(this))
        {
            setImage("ouch.png");
        }
    }
Korina Korina

2014/12/9

#
And sometimes the mouse doesn't click. Why? On my Start Menu I also used if(Greenfoot.mouseClicked(this)) and it works perfectly. Is it because the actor are moving?
danpost danpost

2014/12/9

#
Maybe it would be best to replace the clicked pig with a different actor to display the ouch image. If the pig remains in the world it could get clicked on again with unwanted results. So, when the world detects a click on a pig, it should remove the pig from the world and add an Ouch actor that will remove itself after some number of act cycles which would need to be counted using an int counter field in the Ouch class.
danpost danpost

2014/12/9

#
Korina wrote...
And sometimes the mouse doesn't click. Why? On my Start Menu I also used if(Greenfoot.mouseClicked(this)) and it works perfectly. Is it because the actor are moving?
If the hammer is following your mouse, then you most probably will be clicking on the hammer and not the pig. At least, that will be the case if you have 'setPaintOrder' in your world with Hammer.class first (which is ok).
You need to login to post a reply.