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

2020/3/8

constant movement and speed

AnitaAleksic AnitaAleksic

2020/3/8

#
Hi, can someone please tell me how to make an actor move constantly and set it's speed?
danpost danpost

2020/3/8

#
To move in a straight line and stop at an edge:
public void act()
{
    move(1);
}
which is equivalent to:
public void act()
{
    int speed = 1;
    move(speed);
}
In both the above, the speed will be fixed and will not change unless the code itself is changed. By moving the variable outside the method:
int speed = 1;

public void act()
{
    move(speed);
}
it will be possible to have other code modify the value of the variable. For example, a call to the following added method would do just that by allowing a new given value be assigned to speed:
public void setSpeed(int spd)
{
    speed = spd:
}
AnitaAleksic AnitaAleksic

2020/3/11

#
Thank you so much!!!
You need to login to post a reply.