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

2017/5/7

Player 1 and Player 2 same code but only Player 1 works

Lan31 Lan31

2017/5/7

#
Player 1 and Player 2 have the same code but only Player 1 can shoot the other one doesn't shoot. Player 1:
public class Player1 extends Actor
{
    public void act() 
    {    
      if (Greenfoot.isKeyDown("w")) 
        {
          setLocation(getX(), getY() - 10);
        }
        
      if (Greenfoot.isKeyDown("s"))
        {
          setLocation(getX(), getY() + 10);
        }
      if("c".equals(Greenfoot.getKey()))  
        {
          Schoot();
   
        } 
    }
    private void Shoot()       
        {
            Bullet1 bullet1 = new Bullet1();
            getWorld().addObject(kugel1, getX(), getY());
            bullet1.move(40);
        }
   
}
Bullet1:
public class Bullet1 extends Actor                                                                                                                                           
{
    public void act() 
    {
      setLocation(getX() + 1, getY());   
      checkContaktPlayer2();
      bulletDisappear();
      
    }
    private void bulletDisappear() {  
        if (getX() >= 999) 
        {    
            getWorld().removeObject(this);
        }
    }
    private void checkContaktPlayer2()
        {
            Actor player2 = getOneIntersectingObject(Player2.class);
            if (player2 !=null) 
            {
                getWorld().removeObject(player2);
            }
        }
}
and now Player2:
public class Player2 extends Actor
{ 
    public void act() 
    {
        if (Greenfoot.isKeyDown("up"))
        {
          setLocation(getX(), getY() - 10);
            }

      if (Greenfoot.isKeyDown("down"))   
        {
          setLocation(getX(), getY() + 10);
        }    

      if("space".equals(Greenfoot.getKey()))  
        {
          Shoot();
        } 
    }
    private void Shoot()   
        {
            Bullet2 bullet2 = new Bullet2();
            getWorld().addObject(new Bullet2(), getX(), getY());
            bullet2.move(-40);
        }
}
Bullet2:
public class Bullet2 extends Actor                                                                                                                                           
{
    public void act() 
    {
      setLocation(getX() - 1, getY());   
      checkContaktPlayer1();
      bulletDisappear();
      
    }
    private void bulletDisappear() {  
        if (getX() >= 1) 
        {    
            getWorld().removeObject(this);
        }
    }
    private void checkContaktPlayer1()
        {
            Actor player1 = getOneIntersectingObject(Player1.class);
            if (player1 !=null) 
            {
                getWorld().removeObject(player1);
            }
        }
}
Why Player2 doesn't shoot?
Kamichalik Kamichalik

2017/5/7

#
In player 2 the code that creates the bullet says "getWorld().addObject(new Bullet2(), getX(), getY());" change the new Bullet2() to bullet2.
Lan31 Lan31

2017/5/7

#
@Kamichalik Still doesn't work. If I type it in there is an error "cannot find symbol"
danpost danpost

2017/5/7

#
The problem is the use of 'getKey'. You are using it for two different things within one act step. The only time the second call to it returns the same as the first is when it returns 'null'. Otherwise, when the first time does return a key, the second time will return either 'null' or a different key (if more than one was registered before a call to 'getKey' was able to get the first). You will need to track the state of the keys ("c" and "space") in their respective classes and determine when the state of each changes (using either the press or the release of the key as the condition to shoot. This will require a boolean field (to maintain a record of the last state of the key) along with the use of the 'isKeyDown' method (as opposed to using 'getKey'):
// instance field (outside of act method)
boolean cDown; // state of "c" key (true if key is down, otherwise false)

// in act method (for shooting)
if (cDown != Greenfoot.isKeyDown("c")) // change in state?
{
    cDown = !cDown;  // record new state
    if (cDown) Shoot(); // shoot, if new state is down
}
You need to login to post a reply.