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

2022/2/17

Boss

Ginopinosulpino Ginopinosulpino

2022/2/17

#
Hi i am pretty new and i need a code to meke my boss move alone whitout pressing any key can someone help
danpost danpost

2022/2/17

#
Ginopinosulpino wrote...
meke my boss move alone whitout pressing any key
The basic code would be determined by how you want it to move. However, as opposed to key conditional movement, which requires key input checking, you want unconditional movement. So, first thing is to remove all key checking. Now, you still may want some conditions placed on movement to determine when to move in which direction or how fast to move (or both) or, even, when to move at all.
Ginopinosulpino Ginopinosulpino

2022/2/18

#
and what is the code for unconditional movement i dont know where to find it. And i also need the boss to fire bullets with the space bar how can i do that.
danpost danpost

2022/2/18

#
Ginopinosulpino wrote...
and what is the code for unconditional movement i dont know where to find it.
How do you want the boss to move? However, most basic left to right would be:
public void act()
{
    move();
    shoot();
}

private void move()
{
    move(1);
}

public void shoot()
{
    // code omitted (see below)
}
And i also need the boss to fire bullets with the space bar how can i do that.
Multiple discussions involving "space" key to shoot. Search "shoot space key".
Ginopinosulpino Ginopinosulpino

2022/2/19

#
i wanted him to move up and down costantly
danpost danpost

2022/2/19

#
Ginopinosulpino wrote...
i wanted him to move up and down costantly
Then:
int yDir = 1;
int speed = 1; // adjust as required

public void act()
{
    move();
    turnAround();
    shoot();
}

private void move()
{
    setLocation(getX(), getY()+speed*yDir);
}

private void turnAround()
{
    int halfHeight = getImage().getHeight()/2;
    if ((getY() < halfHeight && yDir < 0) || (getY() > getWorld().getHeight()-halfHeight && yDir > 0)) yDir = -yDir;
}

// etc.
Ginopinosulpino Ginopinosulpino

2022/2/20

#
thx
Ginopinosulpino Ginopinosulpino

2022/2/22

#
on a second tought i would like to make the boss shoot randomly and whitout pressing any key how can i make that
danpost danpost

2022/2/22

#
Ginopinosulpino wrote...
on a second tought i would like to make the boss shoot randomly and whitout pressing any key how can i make that
Use an int timer field to count down time between shots. It can be given the same initial value to count down from or a random value (or it can be a combination of both -- a constant minimum time plus a random additional amount).
Ginopinosulpino Ginopinosulpino

2022/2/22

#
do you know where i can find that code
You need to login to post a reply.