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

2016/4/21

Using getX & getY in world?

3
4
5
6
7
8
idk1234 idk1234

2016/5/7

#
Sorry for not explaining it properly but It doesn't actually display at all. As in it doesn't do anything.. at all
SPower SPower

2016/5/7

#
As I asked you, do you call the method getKey somewhere else in your code? Because the getKey method works that, once a key is pressed, it 'remembers' that until someone calls that method. Once it is called, it forgets that key again and will return null from that point on, until a new key is pressed. That's the only thing I can think of now that might cause this.
idk1234 idk1234

2016/5/7

#
That's the only place where I call the getKey method so I'm not quite sure what's going on
danpost danpost

2016/5/7

#
I have a feeling (without checking) that this may be a "case" problem. Try using:
if ("v".equals(Greenfoot.getKey()))
with a lowercase 'v'. I believe the getKey method returns the specific case sent to it, where the 'isKeyDown' method always sets the same case for both the key pressed and the key asked for (the press of the key could be for either case).
idk1234 idk1234

2016/5/8

#
You were right it works now, thanks!
idk1234 idk1234

2016/5/8

#
Well now I have ran into a problem as I've realised that it only works with 1 player, how would I only allow 1 bullet to fire with 2 players?
danpost danpost

2016/5/8

#
idk1234 wrote...
Well now I have ran into a problem as I've realised that it only works with 1 player, how would I only allow 1 bullet to fire with 2 players?
SPower wrote...
As I asked you, do you call the method getKey somewhere else in your code? Because the getKey method works that, once a key is pressed, it 'remembers' that until someone calls that method. Once it is called, it forgets that key again and will return null from that point on, until a new key is pressed.
What this means is that you will need to use the other keyboard input detection method -- the 'Greenfoot.isKeyDown' method. With it, you will also need to include a boolean field to track the state of the key so that you can detect when the state changes between down and up (instead of just detecting the current state):
// instance field
private boolean shotKeyDown;

// in act method or a method it calls
if (shotKeyDown != Greenfoot.isKeyDown(shotkey)) // shotkey = "v" for player one
{
    shotKeyDown = !shotKeyDown; // save changed state
    if (shotKeyDown) // changing to down state?
    {
        // shoot code here
    }
}
idk1234 idk1234

2016/5/8

#
Thanks, so I had to use both methods, gotcha
danpost danpost

2016/5/8

#
idk1234 wrote...
Thanks, so I had to use both methods, gotcha
Actually, you should be using 'isKeyDown' with a boolean field for both players. I would reserve the 'getKey' method for when input is required, not when is it optional.
idk1234 idk1234

2016/5/9

#
I am trying to create respawn methods for both virus (VirusWASD & VirusARROW) after they are killed and am having some trouble. The current code I have, spawns a virus even if the equivalent survivor is not infected yet. The current code also only has VirusWASD, not quite sure how to add VirusARROW into the same method (if possible) Respawn Code:
    public void respawn()
    {
        if (getObjects(VirusWASD.class).size() == 0)
        {
            addObject(new VirusWASD(),173 ,429);
        }
    }
Reminder of Infect code:
    private void infect()
    {
        if (Greenfoot.mouseClicked(twoPlayer_Button)) // countdown starting
        {
            countdown = 16; //2276
        }
        if (countdown == 0) return; // countdown already completed
        countdown--;
        if (countdown == 0) // countdown just completed
        {
            setPaintOrder(InfectionHasStarted.class, GameTimerGif.class, TimerBackground.class);
            addObject(new InfectionHasStarted(), 695, 393);
            addObject(new TimerBackground(), 691, 47);
            addObject(new GameTimerGif(), 687, 48);
            Actor[] viruses = new Actor[] { new VirusWASD(), new VirusARROW() }; // must be same order as 'survivors'
            int chosen = Greenfoot.getRandomNumber(2);
            addObject(viruses[chosen], survivors[chosen].getX(), survivors[chosen].getY());
            removeObject(survivors[chosen]);
            respawn();
            //removeObject(twoPlayer_Button); // optional
        }
Thanks in advance :) P.s How would I be able to spawn it in different defined locations (not random)?
danpost danpost

2016/5/9

#
These "different defined locations", are they to be used in order as defined, or randomly among those defined? (it is unclear as to how "(not random)" is being used here). At any rate, the locations can be defined in a double int array:
// for example
int[][] locs = { { 173, 429 }, { 252, 357 }, { 211, 499 } };
// a random location choice would be
int rand = Greenfoot.getRandomNumber(locs.length);
int x = locs[rand][0];
int y = locs[rand][1];
addObject(new VirusWASD(), x, y);
For cycling through the locations, you will need an int field that is increments and reset to zero when all locations have been used for the index in the array instead of the 'rand' value.
idk1234 idk1234

2016/5/10

#
Yeah, I meant defined locations that are random among those defined. How would I make sure the Virus only respawns if the equivalent Survivor is infected?
danpost danpost

2016/5/10

#
idk1234 wrote...
How would I make sure the Virus only respawns if the equivalent Survivor is infected?
Since you are removing the survivor and replacing it with a virus with the same control keys, I would think that either:
if (getObjects(SurvivorWASD.class).isEmpty())
// or
if (!getObjects(VirusWASD.class).isEmpty())
would be what you would use for respawning the WASD controlled virus.
idk1234 idk1234

2016/5/10

#
I understand but this would result in 2 viruses spawning(if infected) or a random virus spawning when its not mean to
danpost danpost

2016/5/10

#
idk1234 wrote...
I understand but this would result in 2 viruses spawning(if infected) or a random virus spawning when its not mean to
Maybe I am not understanding that which you are wanting. Could you describe what you want the respawn to do -- exactly? Because, I though my response to this:
idk1234 wrote...
How would I make sure the Virus only respawns if the equivalent Survivor is infected?
was accurate; yet, you are having problems with it. Or, were you using both of those lines in my last code post at the same time? (which was not intended -- hence the '// or' between them on line 2).
There are more replies on the next page.
3
4
5
6
7
8