0
\$\begingroup\$

I ran into a problem coding my game in which when I click the play button on the newly implemented menu, it also creates a bullet from my player. The player and the menu both rely on the mouse input. How would I make it so that this doesn't occur? Perhaps how would I delay input from the mouse for a second until after the game starts?

Thank you.

public static boolean pressed = false;

public void mousePressed(MouseEvent e)
{
    int mx = (int) e.getX();
    int my = (int) e.getY();


    if(Game.state == STATE.MENU) {
        if(Menu.playButton.contains(mx, my))
        {
            // Pressed Play Button          
            pressed = true;
            Game.setState(Game.STATE.GAME);
        }


        for (int i = 0; i < handler.object.size(); i++)
        {
            GameObject tempObject = handler.object.get(i);

            if(tempObject.getId() == ID.Player && pressed == true)
            {
                handler.addObject(new Bullet(tempObject.getX()+16,
                        tempObject.getY()+24, ID.Bullet, handler, mx, my, game));
                bullets_shot += 1;

            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I removed the link to your github project because it will change over time, reducing the correlation between this question and what's actually pointed by it. \$\endgroup\$ – Vaillancourt Mar 7 '18 at 14:05
0
\$\begingroup\$

Just setting up a flag that indicates if the menu is open should be enough.

The player should check the flag state, and in case it's true, don't shoot.

EDIT:

I updated your function adding an else condition to the check on the game state:

public void mousePressed(MouseEvent e)
{
    int mx = (int) e.getX();
    int my = (int) e.getY();
    pressed = true;

    if(Game.state == STATE.MENU) {
        if(Menu.playButton.contains(mx, my))
        {
            // Pressed Play Button
            Game.setState(Game.STATE.GAME);
        }
        if(Menu.helpButton.contains(mx, my))
        {
            JOptionPane.showMessageDialog(null, "W: up, "
                + "S: down, D: right, A: left, "
                + "ESC: Pause ");
        }
        else if(Menu.quitButton.contains(mx,my)) 
        {   
            // Pressed Quit Button
            System.exit(1);
        }
    }
    else{ //<--- Added this else
        for (int i = 0; i < handler.object.size(); i++)
        {
            GameObject tempObject = handler.object.get(i);

            if(tempObject.getId() == ID.Player)
            {
                handler.addObject(new Bullet(tempObject.getX()+16,
                        tempObject.getY()+24, ID.Bullet, handler, mx, my, game));
                bullets_shot += 1;
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I edited it above to show how I set it up. I have the boolean variable set up but it still shoots when I press the play button. \$\endgroup\$ – Mina Saleeb Mar 6 '18 at 22:47
0
\$\begingroup\$

At this point in your code:

for (int i = 0; i < handler.object.size(); i++)
    {
        GameObject tempObject = handler.object.get(i);

        if(tempObject.getId() == ID.Player)
        {
            handler.addObject(new Bullet(tempObject.getX()+16,
                    tempObject.getY()+24, ID.Bullet, handler, mx, my, game));
            bullets_shot += 1;

        }
    }

You do the bullet stuff while in the Game.state == State.MENU state. You need some other state like State.PLAY And only be able to create bullets while actually in the game. Not the menus. Hope this hopes! :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.