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

2021/11/21

move actors

Tlax Tlax

2021/11/21

#
hello, I am trying to make a game where 15 vertical rectangles (with a hole in the middle ) are moving vertical, each with a different starting time(not sync). I created the actors with a string. So automaticly 15 actors spawn, but how do i make them move seperated so not sycnronised? thnx
Tlax Tlax

2021/11/21

#
int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber (getHeight()); Verti verticalebalk= new Verti(); addObject(verticalebalk,300,200); balk balk1 = new balk (); for (int i=0; i<1; i++) { addObject(new balk(),90+i*30,200); }
danpost danpost

2021/11/21

#
Tlax wrote...
hello, I am trying to make a game where 15 vertical rectangles (with a hole in the middle ) are moving vertical, each with a different starting time(not sync). I created the actors with a string. So automaticly 15 actors spawn, but how do i make them move seperated so not sycnronised? thnx
Either add them into the world at different times or add a randomized delay timer for the instances of the actor class that creates the wanna-be non-synced actors.
Tlax Tlax

2021/11/21

#
first thnx for the anwser, how can i put a delay timer ? The goal is to have the objects move vertical non-synced and when they are in the screen the player will have to pres space to stop the actor, and this then 15 times.
Tlax Tlax

2021/11/21

#
when they are in the middle of the screen **
danpost danpost

2021/11/21

#
Tlax wrote...
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber (getHeight());
Verti verticalebalk= new Verti();
addObject(verticalebalk,300,200);
balk balk1 = new balk ();
for (int i=0; i<1; i++)
{
    addObject(new balk(),90+i*30,200);
}
In code provided, x and y are not used; so lines 1 and 2 can be removed. Also, balk1 is not used, so line 5 can also be removed. The for loop only executed once, so with no real looping, lines 6, 7 and 9 can be removed. That leaves the following:
addObject(new Verti(), 300, 200);
addObject(new balk(), 90, 200);
Obviously, this is deficient for your needs.
danpost danpost

2021/11/21

#
Tlax wrote...
The goal is to have the objects move vertical non-synced and when they are in the screen the player will have to pres space to stop the actor, and this then 15 times.
Referring to "when they are in the screen", does this mean that they start off-screen? If so, are you using an unbounded world where actors can actually go off-screen?
how can i put a delay timer ?
You may not need to use a delay timer. You may only have to place the actors randomly off-screen so that there will be no synchronization among them.
Tlax Tlax

2021/11/21

#
first i added every 'balk' separately, but this took al lot of time. addObject(balk1, 90,200); balk balk2 = new balk (); addObject(balk2, 120,200); balk balk3 = new balk (); addObject(balk3,150,200); balk balk4= new balk (); addObject(balk4,180,200); balk balk5= new balk (); addObject(balk5, 210,200); balk balk6 = new balk (); addObject(balk6,240,200); balk balk7= new balk (); addObject(balk7,270,200); balk balk8= new balk (); addObject(balk8, 300,200); balk balk9 = new balk (); addObject(balk9,330,200); balk balk10= new balk (); addObject(balk10,360,200); balk balk11= new balk (); addObject(balk11, 390,200); balk balk12 = new balk (); addObject(balk12,420,200); balk balk13= new balk (); addObject(balk13,450,200); balk balk14= new balk (); addObject(balk14, 480,200); balk balk15 = new balk (); addObject(balk15,510,200); How do i make each 'balk' move different (not sync) in the balk editor?
danpost danpost

2021/11/21

#
import greenfoot.*;

public class Balk extends Actor
{
    static boolean stopped;
    
    int dir = 1-2*Greenfoot.getRandomNumber(2);
    int speed = 1; // adjust as needed (always positive)
    
    public void act()
    {
        if (stopped)
        {
            // possible code to restart (set stopped to false again)
            return;
        }
        setLocation(getX(), getY()+dir*speed);
        int worldHeight = getWorld().getHeight();
        int height = getImage().getHeight();
        if ((getY() < height/2 && dir == -1) || (getY() > worldHeight-height/2 && dir == 1) dir = -dir;
    }
}
In world:
for (int i=0; i<15; i++)
{
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(new Balk(), 90+i*30, y);
}
Player stops Balk objects with:
Balk.stopped = true;
danpost danpost

2021/11/21

#
With delay timer field, you would have:
import greenfoot.*;

public class Balk extends Actor
{
    static boolean stopped = true;
    
    int dir = 1-2*Greenfoot.getRandomNumber(2);
    int speed = 1; // adjust as needed (always positive)
    int delay = Greenfoot.getRandomNumber(400/speed);

    public void act()
    {
        if (stopped)
        {
            delay--;
            if (delay == 0) stopped = false;
            return;
        }
        setLocation(getX(), getY()+dir*speed);
        int wh = getWorld().getHeight();
        int h = getImage().getHeight();
        if ((getY() < h/2 && dir == -1) || (getY() > wh-h/2 && dir == 1) dir = -dir;
    }
}
In world:
for (int i=0; i<15; i++) addObject(new Balk(), 90+30*i, 200);
Tlax Tlax

2021/11/21

#
thank you very much
You need to login to post a reply.