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

2019/5/24

End turn code not working

excitedbubbles excitedbubbles

2019/5/24

#
Hi, we are trying to implement an end turn feature in our game project, which lets you end the turn by pressing space and lets the enemies move forward. So far, we haven't been able to accomplish this at all. In this code we are trying to prevent movement if PlayerPhase is false (which would be set to false after space is pressed once). We found it only works when using Greenfoot.isKeyDown, however you have to hold the space bar down the entire time, which is not what we want.
    int state = 0;
    String key = Greenfoot.getKey();
    public boolean PlayerPhase = true;
    
    public void act() 
    {
        if (key != null && key.equals("space"))
        {
            PlayerPhase = false;
        }

        if (PlayerPhase != false)
        {
            if (state == 0)
            {
                if (Greenfoot.mousePressed(this) == true)
                {
                    state = 0+1;
                }
            }
            else if (state == 1)
            {
                movement();
                if (Greenfoot.mousePressed(this) == true)
                {
                    state = 0;
                }
            }
        }
    }
Thanks in advance.
Super_Hippo Super_Hippo

2019/5/24

#
Move line 2 at the beginning of the act method. Right now, it is only executed once when the object is created, so "key" will never change whatever you press later. But, getKey is best to be only used for writing text. You can do it with isKeyDown if you have a variable to save the current state whenever it changes. So you can execute whatever action when it was changed from not-pressed to pressed:
private boolean spaceDown = false;

//…

if (Greenfoot.isKeyDown("space") != spaceDown) //pressed or released
{
    spaceDown != spaceDown;
    if (spaceDown) //pressed
    {
        //action when space was pressed
    }
    else //released
    {
        //action when space was released
    }
}
excitedbubbles excitedbubbles

2019/5/27

#
Thank you, it works now, but we've encountered another problem. We want to reference the PlayerPhase variable in another class, but it doesn't return the updated variable and only returns the initial instance (which is "true"). We want it to return false after space is pressed, and also change the PlayerPhase variable back to true via the other class.
    //in class Player
    int state = 0;    
    public static boolean PlayerPhase = true;
    public void act() 
    {
        String key = Greenfoot.getKey();
        if (key != null && key.equals("space"))
        {
            PlayerPhase = false;            
        }
        
        if (PlayerPhase != false)
        {
            if (state == 0)
            {
                if (Greenfoot.mousePressed(this) == true)
                {
                    state = 0+1;
                }
            }
            else if (state == 1)
            {
                movement();
                if (Greenfoot.mousePressed(this) == true)
                {
                    state = 0;
                }
            }
        }        
    }
    public static boolean getPlayerPhase()
    {
        return PlayerPhase;
    }

//in class Enemy
public void movement()
    {
        if (Player.getPlayerPhase() == false)
        {
            setLocation(getX()-50, getY());
            Player.getPlayerPhase() = true;
        }
    }
Super_Hippo Super_Hippo

2019/5/27

#
Do you use 'getKey' ANYWHERE else? If yes, use "isKeyDown" instead as described above.
danpost danpost

2019/5/27

#
There should be no problems with using isKeyDown here. However, best would be to control turn phase in your World subclass, not only because it is a function of the game itself (not the individual players), but also because you will only require one field instead of two.
You need to login to post a reply.