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

2017/2/10

How to make actor stop

Nooooob Nooooob

2017/2/10

#
Does anyone know how to make an actor stop moving when it touched another actor or itself? I tried wait(); but it makes everything stop. Thanks in advance!
BrownDwarf BrownDwarf

2017/2/10

#
use the syntax provided on "greenfoot API". Basically you can create an if statement, if one object is touching another, then set its speed (or whatever variable you have making it move), to 0. Post your code so I can be more specific.
Nooooob Nooooob

2017/2/11

#
BrownDwarf wrote...
Post your code so I can be more specific.
I wanted to make it similar to Curve Fever. That's how it moves:
 public void act() 
    {
       move(3);
         if(Greenfoot.isKeyDown("left"))
       { 
           turn(-2);
       }
         if(Greenfoot.isKeyDown("right"))
       { 
           turn(2);
       }
    }   
BrownDwarf wrote...
then set its speed (or whatever variable you have making it move), to 0
That's exactly what I don't know how to do.
 if(isTouching(x.class))
        {
            //What's supposed to be in here?
        }
danpost danpost

2017/2/11

#
Instead of:
move(3);
use a field for the speed:
private int speed = 3;
and then use:
move(speed);
Then, when touching x class:
speed = 0;
will set the speed to zero and stop the actor.
Nooooob Nooooob

2017/2/11

#
danpost wrote...
Instead of:
move(3);
use a field for the speed:
private int speed = 3;
and then use:
move(speed);
Then, when touching x class:
speed = 0;
will set the speed to zero and stop the actor.
Nice but what does this mean?
danpost danpost

2017/2/11

#
It means that it does not like that line there. Move it up -- outside the 'act' method (but, still within the class).
Nooooob Nooooob

2017/2/11

#
danpost wrote...
It means that it does not like that line there. Move it up -- outside the 'act' method (but, still within the class).
I see. Many thanks for your input !
You need to login to post a reply.