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

2015/11/3

How to i make bullets spawn with a moving actor?

Flynn1124 Flynn1124

2015/11/3

#
Hi I'm new to programming and i am stuck while coding that my actor is moving edge to edge while randomly shooting bullets. I have the moving figured out and i saw a code on how to make objects spawn randomly at a certain location but i want them to spawn where my actor is at that moment. Can you help me out? Thanks!
private int spawnCounter = 0;

That's the code for spawning objects at a random time but how to i replace the (X, Y) with the coordinates my actor is ? 

public void act() {
    if (spawnCounter > 100) {
        spawnCounter = 0;
        addObject(new YourObject(), (X, Y);
    }
    spawnCounter++;
}
Super_Hippo Super_Hippo

2015/11/3

#
When spawning the actor which will shoot the bullets, keep a reference to it.
private Actor shooter;

//when adding
shooter = new NameOfTheClass();
addObject(shooter, /*where-ever you add it*/);
Then you can replace the 'X' and 'Y' with 'shooter.getX()' and 'shooter.getY()'.
danpost danpost

2015/11/3

#
If you place the code to spawn the bullets in the class of the actor that shoots them, it would be a lot easier. Actually, the act of shooting would be a behavior of that actor and, as such, that is where the code to shoot should go.
You need to login to post a reply.