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

2019/2/18

"Hit the arrow key at the right time for two players" doesn't work

Tommes Tommes

2019/2/18

#
Currently I'm trying to make a game, in which arrows fall down and the player have to hit the right key in the moment the arrow touches the bar. If the player presses the button to early he loses 1 point. If the player doesn't press anything, he loses a point, because the arrow touches the bottom of the screen. So far it worked perfectly. But now I added the second player by copying every class and setting new variables --> nothing works.... Would be great if someone could help me fixing this problem. My World:
public class Tanzwelt extends World
{
    public int p = 0;
    private Counter zaehler1;
    private Counter zaehler2;
    private int z=0;
    private int x=0;
    private int y=0;
    /**
     * Constructor for objects of class Tanzwelt.
     * 
      */
    public Tanzwelt()
    {    
        super (800, 500, 1);
        Leiste l = new Leiste();
        addObject(l, 250, 330);
        zaehler1 = new Counter();
        zaehler2 = new Counter();
        addObject(zaehler1, 700, 50);
        addObject(zaehler2, 100, 50);
        if (z == 0)  {
            RandomPfeil2();
        }
        if (p == 0){
            RandomPfeil();
        }
    }
    public void RandomPfeil() {
        p=Greenfoot.getRandomNumber(4);
        if (p==0) {
            Pfeil_rechts pfeil_r = new Pfeil_rechts();
            addObject(pfeil_r, 550, 100);
        }
            else
                if (p==1) {
                    Pfeil_links pfeil_l = new Pfeil_links();
                    addObject(pfeil_l, 600 , 100);
                }
                else
                    if (p==2) {
                        Pfeil_oben pfeil_o = new Pfeil_oben();
                        addObject(pfeil_o, 650, 100);
                    }
                    else
                        if (p==3) {
                            Pfeil_unten pfeil_u = new Pfeil_unten();
                            addObject(pfeil_u, 700, 100);
                        }
    }
    public void RandomPfeil2()  {
        z=Greenfoot.getRandomNumber(4);
        if (z==0) {
            Pfeil_rechts2 pfeil_r2 = new Pfeil_rechts2();
            addObject(pfeil_r2, 100, 100);
        }
            else
                if (z==1) {
                    Pfeil_links2 pfeil_l2 = new Pfeil_links2();
                    addObject(pfeil_l2, 150, 100);
                }
                else
                    if (z==2) {
                        Pfeil_oben2 pfeil_o2 = new Pfeil_oben2();
                        addObject(pfeil_o2, 200, 100);
                    }
                    else
                        if (z==3) {
                            Pfeil_unten2 pfeil_u2 = new Pfeil_unten2();
                            addObject(pfeil_u2, 250, 100);
                        }
    }
    public void P_null() {
        p = 0;
        RandomPfeil();
    }
    Counter gibZaehler1()  {
        return zaehler1; 
    }
    Counter gibZaehler2()  {
        return zaehler2; 
    }
    public void Z_null()  {
        z = 0;
        RandomPfeil2();
    }
    public void x_plus()  {
        x=x+1;
    }
    public void y_plus()  {
        y=y+1;
    }
    public void x_minus()  {
        x=x-1;
    }
    public void y_minus()  {
        y=y-1;
    }
    private void Gewinnen()  {
        if (x==20)  {
            Ping_Pong_Welt ping = new Ping_Pong_Welt();
            Greenfoot.setWorld(ping);
        }
        if (y==20)  {
            Ping_Pong_Welt ping = new Ping_Pong_Welt();
            Greenfoot.setWorld(ping);
        }
    }
}
One Arrow of Player1:
public class Pfeil_links extends Actor
{
    private boolean v = false;
    private boolean b = false;
    /**
     * Act - do whatever the Pfeil_links wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY()+2);
        if (isAtEdge() == true){
            ((Tanzwelt)getWorld()).P_null();
            if (((Tanzwelt)getWorld()).gibZaehler1().getValue() > 0)  {
                ((Tanzwelt)getWorld()).gibZaehler1().add(-1); 
                ((Tanzwelt)getWorld()).x_minus();
            }
            getWorld().removeObject(this);
        }
            else  {
                PfeilUndLeiste();
            }
    }  
    public void PfeilUndLeiste()  {
         if (isTouching(Leiste.class)) {
              v=true;
         }
            else  {
                v=false;
            }
         if ("left".equals(Greenfoot.getKey())){
            b=true;
         }
            else  {
                b=false;
            }
         if ( v==true && b==true)  {
            ((Tanzwelt)getWorld()).gibZaehler1().add(1);
            ((Tanzwelt)getWorld()).x_plus();
            ((Tanzwelt)getWorld()).P_null();
            getWorld().removeObject(this);
         }
            else  {
                if (b==true && v==false && 
                ((Tanzwelt)getWorld()).gibZaehler1().getValue() > 0)  {
                    ((Tanzwelt)getWorld()).gibZaehler1().add(-1);  
                    ((Tanzwelt)getWorld()).x_minus();
                }
            }
    }
}
One arrow of Player2: public class Pfeil_links2 extends Actor { private boolean v = false; private boolean b = false; /** * Act - do whatever the Pfeil_links2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY()+2); if (isAtEdge() == true){ ((Tanzwelt)getWorld()).Z_null(); if (((Tanzwelt)getWorld()).gibZaehler2().getValue() > 0) { ((Tanzwelt)getWorld()).gibZaehler2().add(-1); ((Tanzwelt)getWorld()).y_minus(); } getWorld().removeObject(this); } else { PfeilUndLeiste(); } } public void PfeilUndLeiste() { if (isTouching(Leiste.class)) { v=true; } else { v=false; } if ("a".equals(Greenfoot.getKey())){ b=true; } else { b=false; } if ( v==true && b==true) { ((Tanzwelt)getWorld()).gibZaehler2().add(1); ((Tanzwelt)getWorld()).y_plus(); ((Tanzwelt)getWorld()).Z_null(); getWorld().removeObject(this); } else { if (b==true && v==false && ((Tanzwelt)getWorld()).gibZaehler2().getValue() > 0) { ((Tanzwelt)getWorld()).gibZaehler2().add(-1); ((Tanzwelt)getWorld()).y_minus(); } } } } My bar is called "Leiste" and it's nothing written in it.
Tommes Tommes

2019/2/18

#
Player2 arrow as a code:
public class Pfeil_links2 extends Actor
{
    private boolean v = false;
    private boolean b = false;
    /**
     * Act - do whatever the Pfeil_links2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY()+2);
        if (isAtEdge() == true){
            ((Tanzwelt)getWorld()).Z_null();
            if (((Tanzwelt)getWorld()).gibZaehler2().getValue() > 0)  {
                ((Tanzwelt)getWorld()).gibZaehler2().add(-1); 
                ((Tanzwelt)getWorld()).y_minus();
            }
            getWorld().removeObject(this);
        }
            else  {
                PfeilUndLeiste();
            }
    }  
    public void PfeilUndLeiste()  {
         if (isTouching(Leiste.class)) {
              v=true;
         }
            else  {
                v=false;
            }
         if ("a".equals(Greenfoot.getKey())){
            b=true;
         }
            else  {
                b=false;
            }
         if ( v==true && b==true)  {
            ((Tanzwelt)getWorld()).gibZaehler2().add(1);
            ((Tanzwelt)getWorld()).y_plus();
            ((Tanzwelt)getWorld()).Z_null();
            getWorld().removeObject(this);
         }
            else  {
                if (b==true && v==false && 
                ((Tanzwelt)getWorld()).gibZaehler2().getValue() > 0)  {
                    ((Tanzwelt)getWorld()).gibZaehler2().add(-1);  
                    ((Tanzwelt)getWorld()).y_minus();
                }
            }
    }
}    
danpost danpost

2019/2/18

#
The getKey method can only be used once per act cycle (aka. scenario frame). If called twice during the same act cycle, the second call will almost assuredly return a null value (meaning it will work for the first actor that uses it, but not for the second). In both cases, you should use the isKeyDown method along with boolean fields to track the state of the keys. For example:
// field
private boolean leftDown;

// in act
if (leftDown != Greenfoot.isKeyDown("left")) // if change in key state
{
    leftDown = ! leftDown; // track key state
    if (leftDown) // if key was just pressed
    {
        if (isTouching(Leister.class)) ...
        else ...
    }
}
There is no need for the b and v boolean variables.
Tommes Tommes

2019/2/19

#
Thank you for your help :)
You need to login to post a reply.