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

2014/1/4

Player1 can shoot, but Player2 can't

DaveFenste DaveFenste

2014/1/4

#
I have game, when 2 people are playing in same time, but when I have def. shooting for one it works, but when I def shooting for player 2 and it have same code as what has Player1. Player1 code
public void act() 
    {

        if (Greenfoot.isKeyDown("right")) {
            move(5);
            setImage(right);
        }
        else if (Greenfoot.isKeyDown("left")) {
            move(-5);
            setImage(left);
        }
        else if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY()-5);
            setImage(up);
        }
        else if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY()+5);
            setImage(down);
        }
        if ("space".equals(Greenfoot.getKey()))  
        {  
            fire();  
        } 

    } 

    private void fire() {
        Bullet bullet = new Bullet();  
        getWorld().addObject(bullet, getX(), getY());  
        if (getImage().equals(left)) bullet.setRotation(180);  
        else if (getImage().equals(down)) bullet.setRotation(90);  
        else if (getImage().equals(up)) bullet.setRotation(270);  
        else bullet.setRotation(0);  
    }
Player 2 code
public void act() 
    {
        if (Greenfoot.isKeyDown("D")) {
            move(5);
            setImage(right1);
        }
        else if (Greenfoot.isKeyDown("A")) {
            move(-5);
            setImage(left1);
        }
        else if (Greenfoot.isKeyDown("W")) {
            setLocation(getX(), getY()-5);
            setImage(up1);
        }
        else if (Greenfoot.isKeyDown("S")) {
            setLocation(getX(), getY()+5);
            setImage(down1);
        }
        if ("C".equals(Greenfoot.getKey()))  
        {  
             fire1();
        } 
    }
    private void fire1() {
        Bullet1 bullet1 = new Bullet1();  
        getWorld().addObject(bullet1, getX(), getY());  
        if (getImage().equals("A")) bullet1.setRotation(180);  
        else if (getImage().equals("S")) bullet1.setRotation(90);  
        else if (getImage().equals("W")) bullet1.setRotation(270);  
        else bullet1.setRotation(0); 
    }
danpost danpost

2014/1/4

#
Try lowercase keys for player2 ("w", "a", "s", and "d").
DaveFenste DaveFenste

2014/1/4

#
Player2 can move, but can't shoot...
danpost danpost

2014/1/5

#
Did you try changing "C" to lowercase?
DaveFenste DaveFenste

2014/1/5

#
I tried it and nothing and when I change Player2 to shooting on "space" what works before, now does not work. Today I have deadline...
danpost danpost

2014/1/5

#
I see two problems here. The first is that the keystroke is being consumed by one player and will not be available for the second (using 'getKey'). The other is that in your 'fire1' method, you are comparing images to Strings (not to images). Change the Strings ("w", "a", etc.) to the image names (left1, right1, etc.). For the first problem, you will either have to use 'isKeyDown' with a boolean to track the state of the key or use the world class act method to 'getKey' and have the proper player fire a bullet when appropriate. Using the first way would not be hard at this point and the 'feel' of the game would be more to the liking.
// with instance boolean field
private boolean cDown;
// code to detect firing
if (!cDown && Greenfoot.isKeyDown("c"))
{
    fire1();
    cDown = true;
}
if (cDown && !Greenfoot.isKeyDown("c")) cDown = false;
Do similar for space in player one (using 'spaceDown').
DaveFenste DaveFenste

2014/1/5

#
It works, thanks very much! :)
You need to login to post a reply.