A tutorial on how to add music and sound effects to our game would be awesome!
@nshusa999 жыл бұрын
If you're having problems with the STATE saying state cannot be resolved to a variable you can move the enum STATE from the game class to its own class. That's what I did and it worked.
@RogueChessPiece8 жыл бұрын
+nshusa99 Fucking hell, thank you. This was driving me nuts for the better part of an hour.
@araeneangela7 жыл бұрын
Bless you son, live long and prosper
@nikhilkarnam486 жыл бұрын
YES THANK YOU MY SAVIOR. YOU ARE GOD. I OWE YOU MY LIFE. No, literally I was about to commit suicide
@dominosisbetter6 жыл бұрын
or just add "Game." before STATE. Well I use intellij, so it automatically adds "Game.".
@tanishtyagi13014 жыл бұрын
@@dominosisbetter genius
@glenglen38459 жыл бұрын
Doing good man, keep it up. I've altered my game so much from just the knowledge you have showed me. If people would stop needing someone to point them in a direction and just use some critical thinking they would quit complaining, critical thinking is something so many people don't have, you've given us the tools and the knowledge that's all we need. Keep it up dude.
@gabrielsanchez84083 жыл бұрын
If you are getting "Exception in thread "Thread-0" java.lang.NullPointerException" when you want to set the color in the render method in menu, just import java.awt.Color; and java.awt.Graphics; instead of java.awt*. Or you can also move the menu = New Menu(); above the hud and spawner initializers. This is located in the Game() method
@andyrockism6 жыл бұрын
IF YOU ARE HAVING ISSUSE WITH THE "STATE" I ALSO DID BUT I FIX IT BY PUTTING ALL OF MY CLASSES IN THE PACKAGE HE MADE IN VID #1 AND THAT FIX IT>
@xhappybunnyx2 жыл бұрын
Literally fist pumped when you said you were doing a menu for this video. After last one's intro I was worried I'd have to look for someone else's tutorial!
@elbozo57236 жыл бұрын
God bless your soul for introducing me to debug mode
@gloop94004 жыл бұрын
nice name
@AbdulaMaher6 жыл бұрын
For anyone still having problem with the STATE saying it cannot be resolved to a variable, the most easy fix is to create a void method called changeState() in your game class, where you change the STATE, and just call is from the mouseClicked method in the Menu class.
@AbdulaMaher6 жыл бұрын
Or another really easy method is to use a "public int state" instead of an enum
@jobliar9374 жыл бұрын
I coded it so when you hover your mouse over an option it turn orange I will paste my entire Menu class if you wanna check it out. Two notes are 1) my Game class is titled Main 2) you have to write "this.addMouseMotionListener(menu);" in your Game constructor under the mouse listener 3) I had to change the render() method to static for some reason 4) I made a setGameState(STATE) method import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; public class Menu extends MouseAdapter{ private static boolean play=false; private static boolean help=false; private static boolean quit=false; private static boolean back=false; private static Main game; private Handler handler; Random r = new Random(); public Menu(Main game, Handler handler){ this.game = game; this.handler =handler; } public void mousePressed(MouseEvent e){ if(game.gameState==STATE.Menu) { if (play) { game.setGameState(STATE.Game); handler.addObject(new Player(game.WIDTH / 2 - 32, game.HEIGHT / 2 - 32, ID.Player, handler)); handler.addObject(new BasicEnemy(r.nextInt(game.WIDTH), r.nextInt(game.HEIGHT), ID.BasicEnemy, handler)); play = false; } else if (quit) System.exit(1); else if (help) { game.setGameState(STATE.Help); help = false; } } else if(game.gameState==STATE.Help) if (back) { game.setGameState(STATE.Menu); back=false; } } public void mouseReleased(MouseEvent e){ } public void mouseMoved(MouseEvent e) { int mx = e.getX(); int my = e.getY(); int q=200; int j=64; int w=Main.WIDTH; int h = Main.HEIGHT; if(game.gameState==STATE.Menu) { if (mouseOver(mx, my, w / 2 - q / 2, h / 2 - (j * 3 / 2), q, j)) play = true; else play = false; if (mouseOver(mx, my, w / 2 - q / 2, h / 2, q, j)) help = true; else help = false; if (mouseOver(mx, my, w / 2 - q / 2, h / 2 + (j * 3 / 2), q, j)) quit = true; else quit = false; } else if(game.gameState==STATE.Help){ if(mouseOver(mx,my,w / 2 - q / 2, h - (j * 2), q, j)) back=true; else back=false; } } private boolean mouseOver(int mx, int my, int x, int y, int w, int h){ if(mx > x && mx < (x + w)){ if(my > y && my < y + h) return true; } return false; } public static void tick(){ } public static void render(Graphics g){ Font fnt = new Font("arial", 1, 50); Font fnt2 = new Font("arial", 1, 30); int q = 200; int j = 64; int w = Main.WIDTH; int h = Main.HEIGHT; if(game.gameState==STATE.Menu) { g.setFont(fnt); g.setColor(Color.white); g.drawString("Menu", w / 2 - 75, 100); g.setFont(fnt2); if (play) g.setColor(Color.orange); else g.setColor(Color.white); g.drawRect(w / 2 - q / 2, h / 2 - (j * 3 / 2), q, j); g.drawString("Play", w / 2 - 30, h / 2 - j + 10); if (help) g.setColor(Color.orange); else g.setColor(Color.white); g.drawRect(w / 2 - q / 2, h / 2, q, j); g.drawString("Help", w / 2 - 30, h / 2 + (j / 2) + 10); if (quit) g.setColor(Color.orange); else g.setColor(Color.white); g.drawRect(w / 2 - q / 2, h / 2 + (j * 3 / 2), q, j); g.drawString("Quit", w / 2 - 30, h / 2 + j * 2 + 10); } else if(game.gameState==STATE.Help){ g.setFont(fnt2); if(back) g.setColor(Color.orange); else g.setColor(Color.white); g.drawRect(w / 2 - q / 2, h - j * 2, q, j); g.drawString("Back", w / 2 - 40, h - j*3/2 + 10); g.setFont(fnt); g.setColor(Color.white); g.drawString("Help", w / 2 - 60, 80); g.setFont(fnt2); g.drawString("Lick my tiny penis",w/2-130, 120); } } }
@MotherTurf4 жыл бұрын
When you say you made a method for game state, what does that look like? Thank you!
@NoahtheEpicGuy3 жыл бұрын
@@MotherTurf it's quite simple, actually. Like any other setter method: public void setGameState(STATE state) { this.gameState = state; } See? Even fits in a one-liner.
@kjbhappy1239 жыл бұрын
I love the tutorials, really helpful. I have one question though. Will you be showing us animation with pictures of stuff, so like instead of squares we have a little dude walking around?
@GPlayerHD9 жыл бұрын
YOU'RE ALIVE!!!!!!!
@millo50147 жыл бұрын
If you want a good help menu use this: else if (game.gameState == STATE.Help){ Font fnt3 = new Font("arial", 1, 15); Font fnt2 = new Font("arial", 1, 30); Font fnt = new Font("arial", 1, 50); g.setFont(fnt); g.setColor(Color.white); g.drawString("Help", 240, 70); g.setFont(fnt3); g.drawString("Use the 'WASD' keys to move around in this fast phased game.", 100, 90); g.drawString("W = Up, A = Left, S = Down, D = Right.", 100, 110); g.drawString("In this game your goal is to dodge enemies and get as many", 100, 130); g.drawString("points as possible. Every 10 levels there is a boss enemy.", 100, 150); g.setFont(fnt2); g.setColor(Color.blue); g.drawString("ENJOY!", 250, 190); g.setColor(Color.white); g.setFont(fnt2); g.drawString("Back", 270, 390); g.drawRect(210, 350, 200, 64); }
@17Haxor179 жыл бұрын
You should get more into organizing classes and packeges :)
@DemonicGaming159 жыл бұрын
I drramt that this happened last night, seriously. Can't wait to fully watch it, still catching up on episode 7.
@MrGhettoCraft9 жыл бұрын
Will we be seeing a conclusion to the series soon? Or at least a consistency of videos? I want to start the series, but I'm afraid I'll get to the last video and the tutorial won't be complete. What do you recommend?
@RealTutsGML9 жыл бұрын
MrGhettoCraft More episodes are coming I can assure you of that.
@kidjr959 жыл бұрын
Love the tutorials! If possible, could you go back to the platformer series and finish it (enemies, menu, etc.)? Much appreciated!!!
@SAVVYAYT8 жыл бұрын
i need help when i press play nothing happens and i checked about 7 times and still couldnt find it..... can someone pls help????? im not getting and error or anything it just doesnt work....
@SAVVYAYT8 жыл бұрын
nvm i fixed it by instead making my own code...instead of this.addmouseListener(menu)....i put this.addMouseListener(new Menu(this,handler));
@daveshn8 жыл бұрын
Get this to the top, because I was having that same problem and it fixed it. Also, for others reading, this.addMouseListener(new Menu(this, handler)); has a comma and a space between 'this' and 'handler'. The way Trang wrote it does make it look like a period, but it's a comma and space.
@jayrom249 жыл бұрын
Yey! I can now continue on my game project! =) Thanks man. Cant wait to watch this
@xxninj4boyxx9 жыл бұрын
For me the help state doesn't display anything but I can still use the "back" button as if it is there?! I have set the color to white but it still doesnt show anything, any ideas?!
@dankfart7 жыл бұрын
The exact same thing is happening to me now, gonna remove it if it doesn't work soon
@alexbozic61427 жыл бұрын
A bit late, but for future reference; make sure that in Game class you put "|| gameState == STATE.Help" within render method and not tick method. This fixed the problem for me.
@theorangeapple58515 жыл бұрын
@@alexbozic6142 not meh
@GuilhermeOliveira-se1th7 жыл бұрын
nice explanation with paint. very cool
@TheEJskater8 жыл бұрын
EDIT : Just watch video to end My help button didn't work so I did this : //Help button if(game.gameState == STATE.Menu){ if(mouseOver(mx, my,220, 200, 200, 64)){ game.gameState = STATE.Help; return; } } ( got different positions of x, y ) and it works now .
@manfredwurst87694 жыл бұрын
Wow thank you so much! This was drivin me crazy. Can you explain to me why it works? Thx anyways!!!!
@jordoncalder15549 жыл бұрын
the menu was a brilliant idea instead of using actual Jbutton (Y) thumbs up.
@JoltPvP8 жыл бұрын
I can't make my STATE import work for some reason. Exception in thread "main" java.lang.Error: Unresolved compilation problems: The import Game cannot be resolved STATE cannot be resolved to a variable at Menu.(Menu.java:7) at Game.(Game.java:35) at Game.main(Game.java:148)
@JoltPvP8 жыл бұрын
+Jolтϟ Never mind, I fixed the issue by putting all of my classes into a package within the default package.
@jayrom249 жыл бұрын
RealTutsGML Hey, so what will be the next topic? When are you going to continue the tutorial? Avid fan! =)
@djn3m3s1s12009 жыл бұрын
Love the tutorials!! I have run into an issue tho, when I select "play" from the menu the game starts but the menu particles don't disappear. Anything I should double check?
@ryanshirvani4 жыл бұрын
Add the menu on top of everything else, so that it comes first 👍
@xaviermarch39686 жыл бұрын
My help, play and quit buttons work well but my back button isnt working
@im_jope4 жыл бұрын
If you are working in the default package, the STATE thing won't work. Create a new package under src called com.tutorial.main and move all of youre classes there
@blockminecentral74579 жыл бұрын
Thank you SOOOOOO much! This helped a lot! So do your other videos!
@brandonbyrd42399 жыл бұрын
Does any one one have the source code I have done everything even this episode but tried to make a game over window but it didn't work now I can't figure out has happen to my code errr
@jilljessurun28972 жыл бұрын
Hi, i have a question, hope that someone knows an answer. How do you solve the problem when you use velocity to move the object, the object keeps renderinng on old positions? When you remove the background and move the object you can see exactly what i mean. For small programs this aint a problem, but when you get like 100 objects in the screen the FPS keeps dropping to the point the program doesnt work anymore. Is there a way to first remove the rendering of the old positions of the object? Thanks in advance.
@IamCanadianBeast9 жыл бұрын
For Some reason i am not getting the GUI/Interface. When i press the Run Button it does not Pop up the interface. Do you think you can help?
@jerboalimitless34619 жыл бұрын
When I try "import com.tutorial.main.Game.STATE;" I get an error message saying "package com.tutorial.main.Game does not exist". Also, when I try adding "package com.tutorial.main;" to the Game class, the error message says, "You have changed the package statement to a package which does not exist in this project. If you wish to move this class to another package you must create the destination package first. The package statement has been reverted back to its original form." Help?
@SiamBlade5 жыл бұрын
My buttons aren't working, do I need something in the tick method?
@RetryThisPro7 жыл бұрын
Isnt it better to put the mouse code inside of the mouseReleased function so it works smoother?
@richhobo12168 жыл бұрын
Why do I get a NullPointerException at 'menu.render(g);' in my Game class? It doesn't even happen all the time, I'd say it errors about 25% of the time or so, but still, it's very annoying.
@gabrielsanchez84083 жыл бұрын
same, did you managed to solve it?
@DemonicGaming159 жыл бұрын
I can't get rid of this error. Any help? Exception in thread "Thread-2" java.lang.NullPointerException at com.ban5hee.main.Game.render(Game.java:120) at com.ban5hee.main.Game.run(Game.java:80) at java.lang.Thread.run(Unknown Source)
@finalsightgaming94919 жыл бұрын
The error is happening because something isn't able to get rendered. Most likely one of the enemy or player objects is getting numbers of a type the program isn't expecting. For instance, BasicEnemy( float x, float y, ID id, Handler handler) is the constructor that takes two float values, an ID, and handler. You have to call a constructor any time you make a new basic enemy, and you have to give it the same types of stuff it is expecting for it to work. Here's the issue: Float data are not Integers! We have to be careful what math and other numbers we use with them, too. The random number generator used in the video is giving us Integers, so you have to be careful with casting or making sure the whole calculation uses the same type to make it easier. handler.addObject(new BasicEnemy( (Game.WIDTH / 2) + 16.0f, (Game.HEIGHT/2) + 16.0f, ID.BasicEnemy, handler)); I get the same error when messing around with adding new enemies while using random numbers in the initial positioning if I'm not careful. Hopefully this helps.
@finalsightgaming94919 жыл бұрын
Also, if you are using random numbers, check to see if you initialized the random number before using it.
@willness42979 жыл бұрын
+DemonicGaming15 Initialize the Game Class right above the HUD.
@willness42979 жыл бұрын
+DemonicGaming15 Initialize the Game Class right above the HUD.
@LPDan20019 жыл бұрын
If you want to simpify the mouse listener there is a .contains() method for rectangles.
@DudeJoeTeamDudeJoe9 жыл бұрын
Thanks man now i have it working and its fine :)
@RenzoIsHereYT7 жыл бұрын
WAIT!!!! 26:34 can you help me make it so when the person hits the HELP button they are put into the help menu and in the background the enmy are moving and so is your white chrather so you can actually prtaice before the game starts on how to move him ???? just a small thing I wanna add but I don't know how to :D
@Ozterkvlt8 жыл бұрын
I have a weird problem, if you click the play button after a second, the game loads up, but if you wait longer than a couple of seconds, nothing inside the if loop checking the STATE.game loads up. The game state things are really messing up how my game is running. EDIT: OK so i think i fixed it, just gotta instantiate everything that was inside the if(STATE.game) blah blah when you click the play inside menu. Sure doesn't seem like a smart way to do it but...doesnt seem to run well unless it's like that
@shaheenmoosa21469 жыл бұрын
Ur tutorials r awesome tanx for all the gudWork() while i can appreciate how hard it must b to create and code on the fly like that if i may make a suggestion that you have ur current videos code already planned out and compiled b4 u start the tutorial as it is extremly difficult to keep up with all the things u decide to change esp wen those changes are cut from here paste there. becomes a little confusing for new coders such as myself
@ASCENDANTGAMERSAGE8 жыл бұрын
+shaheen moosa Grammar Nazi inside me .... must....resist But on a more serious note you should just pause the video and go back to where you were. I am not new to java but I have a hard time following him to and this is what I do.
@cybirdo60634 жыл бұрын
If I wanted to use a jpg image for the menu instead that I have in a separate folder within the project folder, how would I implement that code into the Menu class.
@beastmodewb29099 жыл бұрын
My play button and quit button works but when I press help button nothing shows up
@meykilim9 жыл бұрын
+BeastmodeWB verufy your render on Game public void render(){ BufferStrategy bs = this.getBufferStrategy(); if(bs == null){ this.createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.black); g.fillRect(0, 0, WIDTH, HEIGHT); handler.render(g); if(gameState == STATE.Game){ hud.render(g); }else if(gameState == STATE.Menu || gameState == STATE.Help){ menu.render(g); } g.dispose(); bs.show(); }
@MagnusJ5689 жыл бұрын
how would i make the game stop when the hp bar reaches 0 i can make it display a game over screen but i couldnt find a way to make the player and enemies go away
@jayrom249 жыл бұрын
Magnus Jensaas create a new function in handler class public void clearEverything() { ListIterator x = this.object.listIterator(0); while (x.hasNext()) { x.next(); x.remove(); } } and call this function in an event where you would want to remove all objects
@joshua.lau_9 жыл бұрын
Does anyone know what is wrong? After i typed the "handler.addObject" thingy for the play button, the player doesn't appear. anyone can help pls?
@MCtomgie9 жыл бұрын
I was the 34,000 Subscriber :D
@ichhabsdrauf96799 жыл бұрын
Why doesn't my menu appear on the sceen, after I created the Help thing.
@codingisheaven99023 жыл бұрын
The play button isn't working what should i do
@sathvikmalla05173 жыл бұрын
I am also having the problem for STATE saying state cannot be resolved to a variable and so far none of the methods shown in the comments work for me. does anyone have any other idea?
@jabber515 жыл бұрын
How would you add a text field where you can input a name to name your character? I'm having trouble getting JTextField to work on a menu state and have it show up with all the other visual things created in the render() method. Any help is appreciated
@bernstv24204 жыл бұрын
Hello mister, Can i ask for an advice because handler.clearEnemys(); keeps running some wierd errors...
@Hayward_Jablomi9 жыл бұрын
So right at 7:55 I keep getting and exception and the menu screen won't load for me, it brings me back to the menu.render(g); and says it conflicts with the If(running){ render()} loop in the run method. can anyone help? The menu loads once in a while but usually i get the exception.
@Hayward_Jablomi9 жыл бұрын
Ok I just had to change the tick() and render() methods in Menu to static and then i changed the menu.render(g) to Menu.render(g) and menu.tick() to Menu.tick(), everything works smoothly now!!!
@finalsightgaming94919 жыл бұрын
***** A lot of people are getting the same issue, but if you continue adding in the rest of the code from the video it should work out. Also, making sure to use float numbers (r.nextInt() isn't a float unless you cast it correctly) if the objects you are rendering take float for x and y, etc. Also, you can have an int + a float, but it helps a lot to use the #.## f format for floats (e.g. new BasicEnemy( (Game.WIDTH / 2) + 16.0f, (Game.HEIGHT/2) + 16.0f, ID.BasicEnemy, handler) ).
@finalsightgaming94919 жыл бұрын
***** Also, if you are using random numbers, check to see if you initialized the random number before using it.
@Hayward_Jablomi9 жыл бұрын
DemonicGaming15 it's the only way that Ive found to fix it, but I havent worked on this project for a few weeks due to work.
@finalsightgaming94919 жыл бұрын
***** Using static can be dangerous, because there is only one copy for ALL menu objects. For a small game this shouldn't cause problems, but larger ones will need them to be non static most likely.
@ScorpioGameKing8 жыл бұрын
So I have a question. I am consistently getting a NullPointerException in Thread [Thread-2]. The first error is referring to BufferStrategy bs = this.getBufferStrategy(); which is in private void render. It also is giving me one at if(running) render(); frames++ which is in public void run. I haven't been able to figure this out so if anyone can help that would be great. Also if there are any typos in the code that's because I'm on mobile while typing this. I should also mention that the game ran perfectly fine until I put in the menu, even with the errors.
@ScorpioGameKing8 жыл бұрын
I also forgot to mention that it gives me an error in Thread.run() line 745 public void run() { if (target != null) { target.run(); } } specifically target.run() The debugger says [local variables unavailable]. I don't know if this will help solve issue.
@ScorpioGameKing8 жыл бұрын
No idea what happened but it works just fine now.
@gameofcharging4 жыл бұрын
If You're having a problem with STATE Please Change STATE.[yourState] to [yourMainScript].STATE.[yourState]
@mrtopramen8 жыл бұрын
I made a game...how do I share it with the world? :D lol
@wingedboy839 жыл бұрын
I've got a question: I want to control the player with my mouse. would i create a new class for the mouseadapter/mouseListener like we did for the keyAdaptor? thanks
@wingedboy839 жыл бұрын
Also, what would need to be added to the other classes/methods to make the game update where the mouse is?
@spongewent14793 жыл бұрын
Play button doesn't work
@hairhairgames52369 жыл бұрын
Can next be shooting and blocks or barriers
@jamillejeez44044 жыл бұрын
remove import java.lang.Thread.State; Java automatically places it in the menu folder when you type State thus giving you the error.
@SoyDusti4 жыл бұрын
dude my game broke (the boss don't work and every button i hit it spawns the enemy and the player) i cant w java anymore, i like ur tutorials but for some reason my project decided to stop working
@RealTutsGML4 жыл бұрын
Don't give up. You've got this
@SoyDusti4 жыл бұрын
@@RealTutsGML Im lazy xd
@jonathanmaynard25517 жыл бұрын
The people who are having issues with the STATE error, just create a new enum class and name it STATE then put Menu, Game; and it will work just fine
@jaylenjones91669 жыл бұрын
Why are static variables bad when making a game like this?
@im_jope4 жыл бұрын
How to make your boss look like Steve from Minercaft: public void render(Graphics g) { Color skin = new Color(255,205,148); Color brown = new Color(101, 67, 33); Color royalBlue = new Color(65,105,225); Color clay = new Color(217, 117, 87); g.setColor(skin); g.fillRect((int)x, (int)y, 96, 96); g.setColor(brown); g.fillRect((int)x, (int)y, 96, 26); g.fillRect((int)x, (int)y, 15, 40); g.fillRect((int)x+81, (int)y, 15, 40); g.setColor(Color.white); g.fillRect((int)x+15, (int)y+50, 20, 10); g.fillRect((int)x+61, (int)y+50, 20, 10); g.setColor(royalBlue); g.fillRect((int)x+25, (int)y+50, 10, 10); g.fillRect((int)x+61, (int)y+50, 10, 10); g.setColor(clay); g.fillRect((int)x+35, (int)y+60, 26, 15); g.setColor(brown); g.fillRect((int)x+25, (int)y+75, 10, 21); g.fillRect((int)x+61, (int)y+75, 10, 21); g.fillRect((int)x+25, (int)y+83, 46, 13); }
@speto28 жыл бұрын
Can anyone help my menu and play button worn but when I use the help button I get a black screen. I checked spelling color and position of the words is there some way to fix this nothing appears on the screen
@speto28 жыл бұрын
Nvm fixed it
@twochipgames89677 жыл бұрын
How did you fix it im getting the same error
@twochipgames89677 жыл бұрын
nvm
@theorangeapple58515 жыл бұрын
@@twochipgames8967 HOW DID U FIX IT. PLS TELL ME
@theorangeapple58515 жыл бұрын
@Spanish Sucks HOW
@НиколайБеляков-ш2л8 жыл бұрын
I found the menu too messy...I would do it using another JFrame with JButtons
@CybrNight9 жыл бұрын
When I run the game I click on the play button and the game loads, but the spawner isn't working, it's as if that spawner.tick isn't being called
@CybrNight9 жыл бұрын
nevermind, I forgot I set the spawner timer back up to 1000
@not_woowoo8 жыл бұрын
Can you use a switch statement in the tick and render methods of the Game class to test the state instead of elseif?
@RealTutsGML8 жыл бұрын
+woowoo sure you could do that
@jayrom249 жыл бұрын
Love it! So, what will you be teaching us for the next episode? =) Anyway, I actually need help. Please help me guys. I'm recreating this game and making it for 2 players, which means there are two player rectangles, a couple of pixels apart in the middle of the screen. And what I wanted to happen is that, if these player rectangles collide with each other, I want them as if they hit the game screen border, meaning they will just stop moving against each other even if the movement key is being pressed towards each other. Thanks!
@LPDan20019 жыл бұрын
Use the 'Clamp' method he created in his main class
@jayrom249 жыл бұрын
DanPlayzHD Can you show me how should I use the clamp method with this problem? I tried using it but I can't seem to find the right code for it. Mine does not provide the result I'm looking for.
@suriyatasmimdisha67906 жыл бұрын
My menu isn't working while I pressed mouse. I've read the comment about the fix but couldn't understand. Anyone please help.
@hamzarabhi31506 жыл бұрын
same problem
@MrLingon5 жыл бұрын
Great video my man, literally probably saved me and my friends asses xDD
@jsonwakefield62845 жыл бұрын
Im having an issue with a nullpointerexception for my handler.render(g); in my game class. What is it that could be going wrong
@real_evin5 жыл бұрын
You may have forgotten to add either private Handler handler; at the start of the Game.java file or handler = new Handler(); just inside the Game() constructor
@vv6boymon8 жыл бұрын
Only my PLAY Button works, but barely... does anyone wanna share their code to help me out?
@ronanmateer7 жыл бұрын
Help please buttons not working.
@SuperThomas14158 жыл бұрын
It takes a while for my graphics to load into the window when i run the program. Any ideas why this is happening? It takes around 5 seconds before the background and player colours are displayed
@predator4125697 жыл бұрын
Hey did you ever solve this problem, I'm having the same issue!
@SuperThomas14157 жыл бұрын
Hey man, I could never figure it out. I think its because of the way the scene is built. If you step through your program you'll see that a lot happens before your objects are actually displayed on the screen. I'm sure there is a fix but I just coped with it as i am relatively new to Java!
@kirilraykov35199 жыл бұрын
Thank you!
@ShaggyLunchCake9 жыл бұрын
This was pretty bad in terms of programming conventions. You made the MouseInput work only inside a specific class instead of making it its own class which you did do for the KeyInput. Inconsistent, tsst tsst. Other than that I really like your explanation, mostly fast paced and clear. Good job.
@jonesbbq3077 жыл бұрын
I was playing the game once and in level 2 or 3, instead of creating two basic enemies, I got two players. It only happened once which is really weird. Any idea how that happened?
@xhappybunnyx2 жыл бұрын
4 years is a while but w/e--I had this issue in my tweaked version when I (1) made M the menu button for mid-game access and (2) had 'new Player' under what happens when Play was clicked. Made it kinda more fun though, ngl
@williewilwil18 жыл бұрын
I've been comparing the code in the video with my code, and can't see something that should lead to problems. Yet in my Menu class it says every "STATE" is a variable STATE which it can't find. Does somebody know how I can fix this?
@williewilwil18 жыл бұрын
Menu class: package game; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; /** * * @author Wilco */ public class Menu extends MouseAdapter { private Game game; private Handler handler; private Random r = new Random(); public Menu(Game game, Handler handler) { this.game = game; this.handler = handler; } public void mousePressed(MouseEvent e) { int mx = e.getX(); int my = e.getY(); if (game.gameState == STATE.Menu) { //Back button for help if (game.gameState == STATE.Help) { if (mouseOver(mx, my, (Game.WIDTH / 2) - 100, 350, 200, 64)) { game.gameState = STATE.Menu; return; } } } //Play button if (mouseOver(mx, my, (Game.WIDTH / 2) - 100, 150, 200, 64)) { game.gameState = STATE.Game; handler.addObject(new Player(Game.WIDTH / 2 - 32, Game.HEIGHT / 2 - 32, ID.Player, handler)); handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler)); } //Help button if (mouseOver(mx, my, (Game.WIDTH / 2) - 100, 250, 200, 64)) { game.gameState = STATE.Help; } //Quit button if (mouseOver(mx, my, (Game.WIDTH / 2) - 100, 350, 200, 64)) { System.exit(1); } } public void mouseReleased(MouseEvent e) { } private boolean mouseOver(int mx, int my, int x, int y, int width, int height) { if (mx > x && mx < x + width) { if (my > y && my < y + height) { return true; } else { return false; } } else { return false; } } public void tick() { } public void render(Graphics g) { if (game.gameState == STATE.Menu) { Font fnt = new Font("arial", 1, 50); Font fnt2 = new Font("arial", 1, 30); g.setFont(fnt); g.setColor(Color.white); g.drawString("Menu", 255, 70); g.setFont(fnt2); g.drawRect((Game.WIDTH / 2) - 100, 150, 200, 64); g.drawString("Play", 290, 190); g.drawRect((Game.WIDTH / 2) - 100, 250, 200, 64); g.drawString("Help", 289, 290); g.drawRect((Game.WIDTH / 2) - 100, 350, 200, 64); g.drawString("Quit", 290, 390); } else if (game.gameState == STATE.Help) { Font fnt = new Font("arial", 1, 50); Font fnt2 = new Font("arial", 1, 30); Font fnt3 = new Font("arial", 1, 20); g.setFont(fnt); g.setColor(Color.white); g.drawString("Help", 289, 290); g.setFont(fnt3); g.drawString("Use WASD keys to move player", 10, 200); g.setFont(fnt2); g.drawRect((Game.WIDTH / 2) - 100, 350, 200, 64); g.drawString("Back", 290, 390); } } }
@williewilwil18 жыл бұрын
Game class: package game; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.util.Random; /** * * @author Wilco */ public class Game extends Canvas implements Runnable { private static final long serialVersionUID = 1550691097823471818L; private Thread thread; private boolean running = true; private Random r; private Handler handler; private HUD hud; private Spawn spawner; private Menu menu; public enum STATE { Menu, Help, Game }; public STATE gameState = STATE.Menu; public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9; public Game() { handler = new Handler(); this.addKeyListener(new KeyInput(handler)); this.addMouseListener(menu); new Window(WIDTH, HEIGHT, "Let's play a game :D", this); hud = new HUD(); spawner = new Spawn(handler, hud); menu = new Menu(this, handler); r = new Random(); if (gameState == STATE.Game) { handler.addObject(new Player(WIDTH / 2 - 32, HEIGHT / 2 - 32, ID.Player, handler)); handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler)); } } public synchronized void start() { thread = new Thread(this); thread.start(); } public synchronized void stop() { try { thread.join(); running = false; } catch (Exception e) { e.printStackTrace(); } } public void run() { this.requestFocus(); long lastTime = System.nanoTime(); double amountOfTicks = 60.0; double ns = 1000000000 / amountOfTicks; double delta = 0; long timer = System.currentTimeMillis(); int frames = 0; while (running) { long now = System.nanoTime(); delta += (now - lastTime) / ns; lastTime = now; while (delta >= 1) { tick(); delta--; } if (running) { render(); } frames++; if (System.currentTimeMillis() - timer > 1000) { timer += 1000; //System.out.println("FPS: " + frames); frames = 0; } } stop(); } private void tick() { handler.tick(); if (gameState == STATE.Game) { hud.tick(); spawner.tick(); } else { if (gameState == STATE.Menu) { menu.tick(); } } } private void render() { BufferStrategy bs = this.getBufferStrategy(); if (bs == null) { this.createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.black); g.fillRect(0, 0, WIDTH, HEIGHT); handler.render(g); if (gameState == STATE.Game) { hud.render(g); } else { if (gameState == STATE.Menu) { menu.render(g); } } g.dispose(); bs.show(); } public static float clamp(float var, float min, float max) { if (var >= max) { return var = max; } else if (var
@williewilwil18 жыл бұрын
Nevermind, I fixed it (Don't ask me what I did... I would like to know that as well). Now I just have the problem of the buttons not working...
@bryanbergman42414 жыл бұрын
Java swing is so much more primative and complicated than JavaFx. I can see why the java community switched
@simonxie83469 жыл бұрын
I literally did what u did and mine couldn't compile!! In Game class, menu(this, handler) couldn't find the constructor. game.gameState = STATE.Game; getting error. This is so f**king frustrating!
@KaushikTare6 жыл бұрын
This is happening to me too. Any idea why?
@Bushler5 жыл бұрын
This may be a little late, but I found out that if you remove; public enum STATE { Menu, Game }; from the Game class, and create a separate Enum like we did with ID for players, enemies etc, called STATE, and add only Game and Menu, it worked. I dont know why, it should be the same, but this fixed it for me. public enum STATE { Menu, Game; }
@johnmichael9462 жыл бұрын
How to debug like that ?
@GPlayerHD9 жыл бұрын
I realized that even IN GAME the box of "Play" button still there invisible, like, if you press in exactly same place where the button is, the game will start over and over, it's like there's a invisible play button while the game is running. How can I solve that?
@jayrom249 жыл бұрын
GPlayerHD how come is that? you should look at the codes inside this if statement if (gameState == STATE.Menu){ " codes here " } if (gameState == STATE.Game){ " codes here " } make it sure that the game interface is inside the STATE.Game, or make it sure the codes do not overlap within these states. Well this is just what's in my mind.
@GPlayerHD9 жыл бұрын
Jayrom Javier I've done exactly him, look: This is the Menu Class: pastebin.com/PdSYreC0 This is the Game class:pastebin.com/HW83FcVG Can you help me?
@RealTutsGML9 жыл бұрын
GPlayerHD Based on your code I see you didn't read what Jayrom Javier said and didn't watch the entire tutorial. I fix this problem at 26:30
@GPlayerHD9 жыл бұрын
RealTutsGML oh man, I'm sorry, I stopped watching before you found this bug, I read what he said but didn't understand what was wrong because I was following the part of the video you didn't fixed that. My bad, so sorry. And great tutorial, hope you update another video soon, you're helping me a lot! and sorry Jayrom Javier D:
@chunchunmaru51284 жыл бұрын
@@RealTutsGML Game completely broken. Every button is working properly, but when i click play it just loads the HUD and if i force the handler to create a new player, then the ID doesn't work. This is a very old video, i hope you'll see this XD
@suv277 жыл бұрын
question some people have a lot of update method in their games the way your using tick is that the same as update
@suv277 жыл бұрын
and same for the render are thise the same if i use the nam draw for all renders ?
@suv277 жыл бұрын
My health is decreasing every time an enemy passes by my through the player x axis but is not even hitting me and my player is not starting in the middle i do have (width/2 - 32), (height/2 -32) in the game class and the menu play button please help
@chupapimunanyo25965 жыл бұрын
When I'm running debug mode and change values, it does not change on my gui :(( help pls
@paulatkins1235 жыл бұрын
depends on the value you are changing, but eclipse will usually prompt you if the update will not take effect.
@uyiosaiyekekpolor77724 жыл бұрын
Whenever you make a change make sure to save (Ctrl s) and it should update
@huangalex20634 жыл бұрын
8:20 why can't my changes be shown immediately in my window?
@huangalex20634 жыл бұрын
@@boymetevil5403 I did
@userunp4 жыл бұрын
you need to launch the game in debug mode, its that one bug icon besides the run icon
@KaaBockMehr7 жыл бұрын
I actually prefer to have my game state in an integer. so i can use switch(gamestate){ case 0: .....}
@guigui21696 жыл бұрын
You cans switch with an enum as well switch (gameState){ case STATE.Menu : ....; return; case STATE.Help : ...; return; [...]
@hamzarabhi31506 жыл бұрын
can someone help me, I did everything like in the vid but it seems like my mouse doesn't work; I press on the play button but nothing happens
@hamzarabhi31506 жыл бұрын
the only button that works is my help button but the other 3 don't
@hamzarabhi31506 жыл бұрын
this is my code from the menu class; package main; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import main.Game.STATE; public class Menu extends MouseAdapter{ private Game game; private Handler handler; private Random r = new Random(); public Menu(Game game, Handler handler) { this.game = game; this.handler = handler; } public void mousePressed(MouseEvent e) { int mx = e.getX(); // mx = x-coordinaat van de muis int my = e.getY(); // my = y-coordinaat van de muis //play-knop if(mouseOver( mx, my, 215, 150, 200, 64) == true) { game.gameState = STATE.Game; handler.addObject(new Player(Game.WIDTH/2-32, Game.HEIGHT/2-32, ID.Player, handler)); handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH-32), r.nextInt(Game.HEIGHT-32), ID.BasicEnemy, handler)); } //quit-knop if(mouseOver(mx, my,420, 250, 200, 64) == true) { System.exit(1); } //help-knop if(mouseOver(mx, my, 10, 250, 200, 64) == true) { game.gameState = STATE.Help; } //Back-knop in Help state if(game.gameState == STATE.Help) { if(mouseOver(mx, my, 215, 175, 200, 64) == true ) { game.gameState = STATE.Menu; return; } } } public void mouseReleased(MouseEvent e) { } //mouseOver checkt of de muis zich op de rechthoek bevindt private boolean mouseOver( int mx, int my, int x, int y, int width, int height) { if (mx >x && mx< x+width) { if (my >y && mx< y+height) { return true; } else return false; } else return false ; } public void tick() { } public void render(Graphics g) { if(game.gameState == STATE.Menu) { g.setColor(Color.green); Font font = new Font("ComicSansMS", 1 , 50); g.setFont(font); g.drawString("Ultra Snakes Hunt", 85, 50); g.drawString("Menu", 240, 100); g.drawRect(215, 150, 200, 64); g.drawString("Play", 265, 200); g.drawRect(10, 250, 200, 64); g.drawString("Help", 60, 300); g.drawRect(420, 250, 200, 64); g.drawString("Quit", 470, 300); } else if(game.gameState == STATE.Help) { g.setColor(Color.green); Font font = new Font("ComicSansMS", 1 , 50); g.setFont(font); g.drawString("Help", 240, 100); g.drawRect(215, 175, 200, 64); g.drawString("Back", 265, 225); } } }
@Hababa26 жыл бұрын
I have the same problem but all of the buttons dont work :(
@thomassidebottom79236 жыл бұрын
Don't know if you've caught it yet, but I noticed you have an error in your mouseOver Method. Your code: private boolean mouseOver( int mx, int my, int x, int y, int width, int height) { if (mx >x && mx< x+width) { if (my >y && mx< y+height) { //error is here! return true; } else return false; } else return false ; } if you change the line with the error to read: if (my > y && my < y+height) { you may get better results. As for why your "Help" button works, coding miracle? \_(O.o)_/
@thomassidebottom79236 жыл бұрын
@Zachary Peterson I had a small logic error in mine hidden in my mouseOver Method that caused all of my buttons to not function. I had to gut the entire Class and piece it back together one block of code at a time. While I got frustrated and went to a bit of an extreme, a line by line trace of the Class may help you find whatever is causing your buttons to misbehave. If you wanted to, you could also take a look at the Java API entries for MouseAdapter and MouseEvent. Looking at those helped me understand the logic used in this video and may help you as well.
@cartmanorginal8 жыл бұрын
It says that "STATE cannot be resolved to a variable" if(mouseOver(mx, my, 210, 150, 200, 64)){ game.gameState = STATE.Game; pls help im new to programming. i have reed every comment about this problem and none have worked for me im on the verge to smash my computer with a baseball bat.
@odaghast7 жыл бұрын
ive got the same problem as well, and my STATE enum is public
@hairhairgames52369 жыл бұрын
Can you do shooting next
@Loanewolfs9 жыл бұрын
So I followed along exactly, but when I got around to the 7:50 mark, I launch the game with menu.render(g); and I get a Null Pointer pointing to menu.render(g); and render(); can anyone help?
@antwanschreiner50739 жыл бұрын
same
@mikefda128 жыл бұрын
+Filly Skippy did you declare new menu in the game constructor?
@Loanewolfs8 жыл бұрын
I fixed it c:
@michaelg40238 жыл бұрын
+Filly Skippy how did you fix it? im stuck on this part too
@antwanschreiner50738 жыл бұрын
im pretty sure i changed the name of something. i cant remember i found it in the comments. but it ended up screwing me over in the long run so i just stopped making the game lol
@kamilsuhak38474 жыл бұрын
Please someone say they have a version of this... i just broke mine and cant fix it i beg you... even a finished project will do but please!!!!
@BTDDrewBTDwithDREW4 жыл бұрын
wdym you broke it lol
@kamilsuhak38474 жыл бұрын
@@BTDDrewBTDwithDREW i tried to implement more states, like pause, gameover etc and something caused the game to have a massive fps dropdown. i already fixed it
@CybrNight9 жыл бұрын
Can we please have the code
@longb19138 жыл бұрын
How do I break lines using g.drawstring?
@tylereldred6248 жыл бұрын
I believe . If that doesn't work, tell me.
@spicyfrank18 жыл бұрын
That actually doesn't work for the drawString() method. I believe the only way to display the text on different lines is just breaking up the string how you want and calling drawString() multiple times for each line.
@tylereldred6248 жыл бұрын
Agrippa I actually created a method that will draw the string for however long I want specified in a Length variable. Look it up, the method is called DrawStringMultiline.
@EnergixCoding4 жыл бұрын
6:57 - A stack of height.
@areg71829 жыл бұрын
i gave a like before watching the video :3
@asherater2238 жыл бұрын
Edgy :3
@asherater2238 жыл бұрын
I did too
@josephlouwerse21054 жыл бұрын
Warning to all of you who have gone off of this tutorial to make your own version of the game and you have a mouseListener already!! Do NOT make menu handle your mouse input, leave that in your established mouse handler already or it will give you a big headache.
@tnt75888 жыл бұрын
if you are in game and u press the location of the play button u get a second character
@pvp2survive938 жыл бұрын
haha lol
@piuslee34467 жыл бұрын
For the back button it just exits me out
@GameTechExpert5 жыл бұрын
same here!! any fixes found?
@GeraldoLopez9 жыл бұрын
STATE can't be resolved to a variable!!! WHYYYYYY ???? WHYYYY????
@GeraldoLopez9 жыл бұрын
Geraldo Lopez i did everything the same as you!!!
@arsenicsupersonic19 жыл бұрын
+Geraldo Lopez Sorry I have the same issue with mx and my for no reason like yyy
@arsenicsupersonic19 жыл бұрын
+Geraldo Lopez I fixed my issue, and it was that i forgot to put it with the arguments
@arsenicsupersonic19 жыл бұрын
+Geraldo Lopez I fixed my issue, and it was that i forgot to put it with the arguments
@GeraldoLopez9 жыл бұрын
I resolved it some how. I was using this video to build another game. Changing states wasnt easy though. Have fun
@seifert159 жыл бұрын
why not jbutton?
@NuKemRRR9 жыл бұрын
+Luis Henrique Sima Seifert It's a game, not a Tool or software making 'custom' buttons would be a little bit batter
@odaghast7 жыл бұрын
u serious bro, i cant use STATE in Menu, u can because of your import com.tut thing, but i can't why ERGGGGGGGG!
@odaghast7 жыл бұрын
NVM, i figured it out, i had to type "Name of first class".STATE.Game, not something shown here
@angbuscus4 жыл бұрын
@@odaghast you have no idea how big of a hole this dug me out of thank you.