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

2020/2/12

Continuously moving upon mouse click

Vishnu19 Vishnu19

2020/2/12

#
Hello, I would like some help with the method I created below. I want the actor to start moving continuously upon a single mouse click, but currently it only moves 1 pixel per mouse click - and spam clicking is not what I'm trying to do.
public void clickStart()
    {
        if(Greenfoot.mouseClicked(this))
        {
            turnAtEdges(getImage().getWidth()/2,getImage().getHeight()/2);
            move(-1);
        } 
    }    
thx
Super_Hippo Super_Hippo

2020/2/12

#
When clicking, save that you clicked in a variable. For example set it to 1 (or true) when you clicked the first time and back to 0 (or false) when you click another time. Then use that variable (instead of the actual click) to check whether or not it should move.
danpost danpost

2020/2/13

#
The thing that will change is the speed of the actor (-1 or 0). So, it would make sense to use a variable for the speed and just change its value when clicked. The move command should not be inside the "if mouse clicks this" block of code.
You need to login to post a reply.