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

2021/11/23

stops movement

Tlax Tlax

2021/11/23

#
my actor is moving in y direction. But i want it to stop moving when i press it with the mouse. This is the code i have, but it does nothing to the movement. public void gelijkmetlijn() { balk balk = new balk(); if (Greenfoot.mouseClicked(balk)); { direction=0; }
danpost danpost

2021/11/23

#
Tlax wrote...
public void gelijkmetlijn()
{
    balk balk = new balk();
    if (Greenfoot.mouseClicked(balk));
    {
        direction=0;
    }
}
Line 3 creates a new balk object and assigns the variable balk to refer to that object. Line 4 checks for a click on that same balk object; however, that object has never been placed into the world. Furthermore, if you insert a line to add it into the world between lines 3 and 4, it could never have been already clicked on. Line 3 should be outside the method and the object needs to be added into the world before a click can be detected on that object.
Tlax Tlax

2021/11/23

#
oh okay thnx a lot now when i pres mouse the 'balk' stops moving, but how do i manage to start balk moving again when i pres the mouse again?
danpost danpost

2021/11/23

#
Tlax wrote...
how do i manage to start balk moving again when i pres the mouse again?
Change line 6 to:
direction = (direction+1)%2;
You need to login to post a reply.