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

2019/4/25

Mouse click only does action once

tqqdqq tqqdqq

2019/4/25

#
Hello, I'm having some trouble making a mouse click trigger an entire action, rather than a single loop of the actions. I'll attach my programming along with a list of what I've tried/am currently trying.
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
public class Opponent extends Actor
{
    private boolean clicked = false;
    /**
     * Act - do whatever the Opponent wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        hitWall();
        battleRoyale();
        {
            if(Greenfoot.mouseClicked(null))
            {
                clicked=true;
            }
        }
    }   
 
    public Opponent() //scales the image
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = myImage.getHeight()/3;
        int myNewWidth = myImage.getWidth()/3;
        myImage.scale(myNewWidth, myNewHeight);
    }
 
    public void hitWall()
    {
        if (isAtEdge())
        {
            turn(180);
        }
    }
 
    public void battleRoyale()
    {
        if (clicked=true)
        {
            move(5);
            if (isTouching(Shephard.class))
            {
                if (Greenfoot.getRandomNumber(100) > 50)
                {
                    removeTouching(Shephard.class);
                }
            }  
 
        }
    }
}
I'm not sure why the boolean value isn't working and isn't triggering the method. I've previously tried using mouseClicked to call a method, but it only moves the characters through one rotation/moves them once and then requires another click to move. Right now when I run the program either nothing happens when I click or the opponents just start to move and clicking does absolutely nothing. I'm quite new to Greenfoot, and I'm not sure what I could be doing wrong. I'd appreciate any help. Thank you!
danpost danpost

2019/4/25

#
One big problem is in line 38, where you are setting clicked to true, thus the condition is always true. Use '==' (two equal signs) to compare for equality. In this case, you can just use:
1
if (clicked)
since clicked is a boolean value as is.
danpost danpost

2019/4/25

#
Any Shephard object will most probably be removed when touched, as there will be a 50/50 chance of removal many times in a row during a crossing. For example, if your Shephard object does not move at all and if the minimum dimensional size of either actor is 20, which is pretty small, the chance of shephard removal will be 99.21875% ( = 127/128 ) or greater.
tqqdqq tqqdqq

2019/4/26

#
Thank you so much! That fixed it. And wow, thank you for the statistics, I'd just set the value randomly while troubleshooting this so I most likely would've fixed it, but it's interesting to know the exact percentage nonetheless. For the record though, it does move and it also has the same percent chance of removing the "opponent" actor, so it's interesting to see which one "wins." Regardless, thank you for the help.
You need to login to post a reply.