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

2019/3/20

Individual Shots

t3mp3st_0 t3mp3st_0

2019/3/20

#
I have made the rocket game on green foot for two players and both rockets have their own individual counters and controls...How do can I make an individual bullet/shot for each rocket?
danpost danpost

2019/3/20

#
t3mp3st_0 wrote...
I have made the rocket game on green foot for two players and both rockets have their own individual counters and controls...How do can I make an individual bullet/shot for each rocket?
How do you want them to be different (explain all)? Also, what do you have for a class (or classes) for your bullets?
Erasedsword Erasedsword

2019/3/20

#
what i would do is use this code to make it so the the bullets shot on a key release, currently its set up for the spacebar, so just change it depending on what you want to use it for i would put it where your controls are this as an instance variable
    //Checks if the spacebar is down
    private boolean spaceDown;

and this where your controls are
        if (spaceDown != Greenfoot.isKeyDown("space"))
        {
            spaceDown = ! spaceDown;
//this is only when the spacebar is down
            if ( ! spaceDown)
            {
//This is for when the key is released
            }
        }
i hope this heps
danpost danpost

2019/3/20

#
Erasedsword wrote...
        if (spaceDown != Greenfoot.isKeyDown("space"))
        {
            spaceDown = ! spaceDown;
//this is only when the spacebar is down
            if ( ! spaceDown)
            {
//This is for when the key is released
            }
        }
Line 4 is not where the spacebar is only down. Its state is not determined until after line 5. An else clause following the if block (lines 5 thru 8) would be used for key down state. Line 4 is only for a unknown change in the state of the spacebar.
You need to login to post a reply.