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

2022/3/2

Spawning a raining of bombs

jdkdoen05 jdkdoen05

2022/3/2

#
Hello, we are in the process of programming a game with fighter jets. We are currently working on ensuring that the 3rd level has a certain special feature. We came up with the idea of ​​raining bombs from the sky. However, we're not sure how to spawn them, since we have the scroll world in our game and so "addObject(new Bomb, x,y)" doesn't work. Anyone have any ideas?
danpost danpost

2022/3/2

#
jdkdoen05 wrote...
since we have the scroll world in our game and so "addObject(new Bomb, x,y)" doesn't work.
What is the problem? That line will add a bomb into your world and you can have it fall. Scrolling will have them appear to fall normally. I do not see a problem.
jdkdoen05 jdkdoen05

2022/3/3

#
Alright, now it works (why not before). However, I have just 2 more things where I could need help. So: 1. I just don't know how best to spawn the bomb. Because if I'm too far to the right with the plane, I can't spawn the bombs directly at him if I say: addObject(new Bomb, x,y). Now to the question, do you have an idea how I program that the bomb falls from the sky over the plane in a certain period of time? 2. The second problem is that I don't know how to program how the bomb falls straight from top to bottom.
danpost danpost

2022/3/3

#
jdkdoen05 wrote...
I have just 2 more things where I could need help.
(1) I presume the bomb are spawned from your world class (which does control game play). In world act method, call the following method to spawn a bomb:
private void spawnBomb()
{
    if (getObjects(Plane.class).isEmpty()) return;
    Actor plane = (Actor)getObjects(Plane.class).get(0);
    int planeDirection = plane.getDirection(); // return 1 for right and -1 for left
    int spawnDistance = plane.getY()-50;
    addObject(new Bomb(), plane.getX()+planeDirection*spawnDistance, plane.getY()-spawnDistance);
}
Adjust as needed. (2)So, you want them to fall without scrolling? In the Bomb class, keep this:
public void setLocation(int x, int y) {}
and to fall, use "super.setLocation(...".
jdkdoen05 jdkdoen05

2022/3/3

#
(1) now i added this and adapted it to my fighter jet. However, now there are two errors. 1. undeclared method: "fighterjet.getDirection()" and 2. undeclared variable "fighterjetDirection"
jdkdoen05 jdkdoen05

2022/3/3

#
(2) The bomb doesn't fall down, but teleports to this coordinate. Is it possible for her to move there fluidly?
jdkdoen05 jdkdoen05

2022/3/3

#
maybe to make it easier, is it possible to send you our game privately and you take a look? Especially because we have several discussions and maybe that simplifies everything a bit.
danpost danpost

2022/3/3

#
jdkdoen05 wrote...
(1) now i added this and adapted it to my fighter jet. However, now there are two errors. 1. undeclared method: "fighterjet.getDirection()" and 2. undeclared variable "fighterjetDirection"
You will need to add a method to return the left/right direction that the jet is flying in.
danpost danpost

2022/3/3

#
jdkdoen05 wrote...
(2) The bomb doesn't fall down, but teleports to this coordinate. Is it possible for her to move there fluidly?
The act method of bomb should have something like:
super.setLocation(getX(), getY()+4);
Adjust speed ('4') as needed.
jdkdoen05 jdkdoen05

2022/3/4

#
how exactly do I have to declare the fighterjet.getDirection() method?
jdkdoen05 jdkdoen05

2022/3/4

#
a second problem is that the game ends if the bomb is spawned in the world. I have no idea why this is because there is nothing written about it anywhere
danpost danpost

2022/3/6

#
jdkdoen05 wrote...
how exactly do I have to declare the fighterjet.getDirection() method?
Unknown -- without fighterjet codes.
danpost danpost

2022/3/6

#
jdkdoen05 wrote...
a second problem is that the game ends if the bomb is spawned in the world. I have no idea why this is because there is nothing written about it anywhere
Only time a game will end (exit a running state) is (1) an error occurred, in which case you will get some type of error message in the terminal console; or (2) a "Greenfoot.stop();" command was encountered.
You need to login to post a reply.