Java Game Development with Slick – 15 – Making the Menu Buttons Work

Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –






43 responses to “Java Game Development with Slick – 15 – Making the Menu Buttons Work”

  1. Kaustubh Pandey Avatar

    just use this—–

    if(xpos>100&&xpos<playNow.getWidth()+100&&ypos>260-playNow.getHeight()&&ypos<260) {
    if(input.isMouseButtonDown(0)) {
    if(xpos>100&&xpos<exitGame.getWidth()+100&&ypos>160-exitGame.getHeight()&&ypos<160) {
    if(input.isMouseButtonDown(0)) {

  2. A Random Sheep Avatar

    I get Sat Oct 01 17:46:22 CEST 2016 INFO:Controllers not available
    Can someone help me?

  3. the Decoy Avatar

    if you dont want to guess the position, you can always make widht and hight static constants in the main game class. a lot easier in my opinion.Great toturial nontheless πŸ™‚

  4. Jamil Jalal Avatar

    You are the best Bucky!!

  5. WallbyTV Avatar

    Fun practice, try to make it so the if you resize the screen, the collision detection still works and the buttons will still be in the middle of the screen.

  6. Blackhawks .Hall Avatar

    that moment you realize that was a year ago

  7. aaron collins Avatar

    make sure the if statement is being runned. That was my problem. (check your formatting ( make sure it's outside of the first one ) )

  8. aaron collins Avatar

    you probebly forgot to change the function of the button, or you forgot to change the x and y it detects

  9. simosimo206 Avatar

    the best !!you helped me a lot !thank you
    and thank you bucky for this series !

  10. mistermaceroni Avatar

    I'm surprised I got a reply so fast, thanks! I did however get it to work by putting both the if statements for the mouse into 1 if statement.

    I don't know why it didn't work for me the way Bucky has it though.

  11. mistermaceroni Avatar

    Please help me. I dont know where or how to setup MouseOverArea. And buckys way of doing it doesnt work for me either. I have spent over 12 hours trying to fix this.

  12. mistermaceroni Avatar

    Then your really stupid. Never hurt your computer.

  13. mistermaceroni Avatar

    I fixed my problem, sort of. It was a stupid mistake(forgot a bracket). But I have a new one. It doesnt matter where I click it will always work. Please hel, this is getting very anoyying.

  14. mistermaceroni Avatar

    I cant make a button for some odd reason. I have everything right it seems, but then the clicking wont work. I have a button that should increase the score as you click it but when you click it it does nothing. The score does however increase when I click and hold my mouse anywhere on the window and hold W?(Im using WASD controls) Why W though. Its no different from ASD in my code. And why would it increase when I hold the mouse down and press w.

  15. lezvamp Avatar

    Yeah if you made your game screen 800 by 800 you'll need to recalculate the button positions. When capturing the mouses Y position its reversed to the way items are placed on the screen. Placing an image top = 0 bottom = 800, mouse pointer's y co-ord top is 800 bottom is 0.
    To get the top position for your button do:
    for the 2nd part of the posY bit
    screen max y – the y position of your object
    for the 1st number in the posY bit
    and then this number – the height of your button

  16. Catalysticgaming811 Avatar

    can you make a tutorial on making a simple combat system? I really like slick2d cuz seems simple so far

  17. William Gourley Avatar

    why do I get the feeling that there is something that we just keep missing, like chickens just pecking around a fat juicy worm that we just can't eat even though we want it so badly

  18. Filip Gnesin Avatar

    But if I click outside the buttons and drag to the buttons it automatically does what is programmed. What if I only want it to perform the action when I click it instead of also being able to drag?

  19. Doormat Producciones Avatar

    You mean gc.exit() in this case

  20. James Etheridge Avatar

    i guess its because i made it 800 by 800
    instead of what you used

  21. James Etheridge Avatar

    ok so very confused i followed along got no errors yet when i click the button doesnt work but when i click under it at like 3/4 way down the box it exits or switch states? what i am i doing wrong?

  22. Dara McMahon Avatar

    An Issue I have is that the co-ordinates for the position of the buttons is from the top left, but the co-ordinates for where to click is from the bottom left, and seemingly slightly off. Kind of annoying.

  23. Mike Long Avatar

    If need to find out the positions of your buttons. Go back to the tutorial that Bucky made where it displayed the co-ordinates of your mouse, and use those coords to see exactly where the button is on the screen. πŸ™‚

  24. Ashton Gilley Avatar

    thats the cordinates…….

  25. purewaterruler Avatar

    no math? i see math in line 3.

  26. purewaterruler Avatar

    well, in the render method, the Y coordinates start from the top left, but in the update method, it starts from the bottom left.

  27. Kailyou Avatar

    pretty could, but I got a question. The image playNow has a size of 211×51. That is why I dont understand why the y position has to be >209 and be <260. Could someone explain how to get these counts?

  28. Jonny Stewart Avatar

    Because in the Game class we instantiated the Play state with an ID of 1, so when we need to enter the Play state, we simply pass 1 as a parameter in the sbg.enterState() method. For example, if there was another state called HighScore and we set that id to 2, whenever we need to enter that state we write sbg.enterState(2)

  29. BrofistRyan Avatar

    Never mind I found the problem!

  30. BrofistRyan Avatar

    My System.exit(0); button doesn't work for me. I've checked everything and I'm not getting any errors… Someone help!

  31. Danny Hodge Avatar

    Or, you can just draw it out. Thats what i did πŸ™‚

  32. jensssen Avatar

    LoL I missed this tut haha

  33. joseuricponto Avatar

    Sure! It's very good to show for the player that this button works. Actually, you guys should do it.
    I don't know exactly why but I was thinking if we can use the JButton instead of set the position. I'll try.

  34. rhynoboy2009 Avatar

    In case anyone is confused about everything with the coordinates,try using the mouse position counter Bucky had us make before,makes everything a whole lot easier. THANKS BUCK!

  35. Derick1259 Avatar

    The value of "posY" is equal to the vertical position of the mouse. The two posY's in that IF statement is saying "Is the vertical position of the mouse correct so that the mouse is over the play button?"

  36. TheWii27776 Avatar

    the first Y is where is start and second to stop. So if posY is > 209 AND posY is <311,THEN do whatever is in the if statement.If either is false,the if never gets used. Hope it helps!

  37. R3VISION Avatar

    Heres a cool good looking trick guys. if you made your own image for play and exit, before you exit in photoshop or your program that youre using, Make a second Instance of the image, but add glow to it and save it then Import them in the project.

    then within the first If statement that checks if the mouse position is inside the image, add this : playNow = new Image("res/playselected.png");
    do the same for exit.

    and then add a else statement and write : playnow = new Image("res/playnow.png");

  38. Tennis Accolades Avatar

    I had a teacher for a HTML and JavaScript class that SUCKED compared to Bucky. Thanks Bucky for being better than my teacher. πŸ˜€

  39. deeptiVal Avatar

    When I press the exit button it doesn't exit instead it goes to the play state.
    Any help?

  40. Tanaka Avatar

    You can even use gc.exit();

    ( ^.^)b

  41. vividmario52 (gatsan's old channel) Avatar

    For the people that have trouble with the buttons and the X and Y co-ordinates, ( don't worry, I had them too πŸ™ )
    Just make it so that you don't need your mouse to choose the options, like I did. (Watch my video, and you'll see what I mean)

Leave a Reply

Your email address will not be published. Required fields are marked *