Sprites and Animation - How to Make a 2D Game in Java #3

  Рет қаралды 227,081

RyiSnow

RyiSnow

Күн бұрын

Пікірлер: 598
@RyiSnow
@RyiSnow 2 жыл бұрын
To see how to read images in IntelliJ, please check this video: kzbin.info/www/bejne/h2rNpZpmpMh6ndE It seems quite a few people are struggling with "java.lang.IllegalArgumentException: input == null!" error. In case of this video, the error happens when the program fails to read sprite images. First, *make sure to check if your res folder has been registered as a source folder* Especially if you are not using Eclipse since the registration/resource reading process in other IDEs can be different from what I did in the video. Also, check if your file names/paths are identical to what you typed in the getPlayerImage method. Check for any unnecessary space, underscore, or other characters. *The file names and paths need to be completely identical.* If you cannot find any discrepancy, try using the same sprites in the video (the link is in the description). If you could display the images, the issue was caused by your modification so you can narrow it down. You can check if image loading is done properly or not by inserting these lines in the getPlayerImage method: System.out.println("Image loading started"); // load sprite images.... // load sprite images.... System.out.println("Image loading ended"); The loading was done properly if the second text was printed on the console. If only the first text was printed out, something went wrong during the loading and the program stopped there.
@unity_with_timoteo
@unity_with_timoteo 2 жыл бұрын
In my case i am doing it on vs code. This error occurred. I solve it by changing the path, in my case the path is: "./../src/player/boy_right_2.png", for example.
@unity_with_timoteo
@unity_with_timoteo 2 жыл бұрын
@Jackson Hello man. Look my answer below, it could help you!
@arelyezra6339
@arelyezra6339 2 жыл бұрын
@@unity_with_timoteo did you type this "./../src/player/boy_right_2.png" only or not
@arelyezra6339
@arelyezra6339 2 жыл бұрын
@Jackson did you can compile it?
@shinyspy850
@shinyspy850 2 жыл бұрын
you should pin this comment, it was very helpfull.
@organista03
@organista03 2 жыл бұрын
for IntelliJ users: 1) create "res" folder: - right mouse button on main project folder -> New -> Directory -> "res" 2) mark "res" folder as Sourses Root: - right mouse button on "res" folder -> mark directory as -> Sources Root 3) create "player" folder inside "res" folder: - right mouse button on "res" folder -> new -> Package -> "player" 4) paste images in "player" folder 5) you have to add .getClassLoader() between getClass() and .getResourceAsStream() OLD: up1 = ImageIO.read(getClass().getResourceAsStream("player/boy_up_1.png")); NEW: up1 = ImageIO.read(getClass().getClassLoader().getResourceAsStream("player/boy_up_1.png"));
@darmoforeelz5857
@darmoforeelz5857 2 жыл бұрын
This comment right here is pure gold, thank you so much ❤❤
@ogaz_
@ogaz_ 2 жыл бұрын
I had to add a try catch to getPlayerImage() try { getPlayerImage(); }catch (Exception e){ System.out.println("getPlayerImage() is not working"); }
@seamus9898
@seamus9898 2 жыл бұрын
there's several solutions posted for IntelliJ, but somehow NOTHING works for me... always get NullPointerException I give up
@seamus9898
@seamus9898 2 жыл бұрын
omg I just forgot to add setDefaultValues(); getPlayerImage();" I'm so annoyed at myself :((((
@landon9889
@landon9889 2 жыл бұрын
Thank you!
@michaelharrington5860
@michaelharrington5860 Жыл бұрын
My son and I finished episode 3 of your series tonight. You do an excellent job explaining everything and teach at the perfect pace. We were both so excited when the little blue haired boy started walking around. Will continue the series tomorrow with episode 4. Thank you!
@RyiSnow
@RyiSnow Жыл бұрын
It warms my heart to hear that! I hope you and your son will keep enjoying coding :)
@yasthatsme
@yasthatsme 3 жыл бұрын
You deserve more viewers! I have learned so much from you
@RyiSnow
@RyiSnow 3 жыл бұрын
Glad it helped! Thank you for the comment.
@thenolife1957
@thenolife1957 3 жыл бұрын
Well most ppls don't play java games no more except for me
@faced2404
@faced2404 2 жыл бұрын
@@thenolife1957 minecraft is originally made in java
@thenolife1957
@thenolife1957 2 жыл бұрын
@@faced2404 except from that
@thenolife1957
@thenolife1957 2 жыл бұрын
Heheeee
@lvdv4j
@lvdv4j 2 жыл бұрын
If anyone is having issues where they are getting no errors but nothing is drawn be sure to add the getPlayerImage(); method to the player constructor in the Player class. (Just under setDefaultValues ();)
@easypeezy9142
@easypeezy9142 Жыл бұрын
thx
@lvdv4j
@lvdv4j Жыл бұрын
@@easypeezy9142 Glad I could help 😄
@RealSenn
@RealSenn Жыл бұрын
I was stuck for 2 hours. Thank you very much!
@lvdv4j
@lvdv4j Жыл бұрын
@@RealSenn You're welcome 😄 I'm glad I could help!
@scusmiwhat
@scusmiwhat Жыл бұрын
TYSM!!!!
@snarpking
@snarpking 2 жыл бұрын
This series is so good. I've been interested in making actual games for a long time, but I've had a lot of struggle learnign unity, and I was about ready to give up, but now I've this AMAZING series to help me use the language I'm really familiar with to make real games. I've made some pretty cool console based games before, like this whole tower defense game which was displayed to the player exclusively in text, but games like that have obviously an incredibly niche appeal. That's why I'm so excited to really branch out like this. Please keep making these amazing videos.
@Ashokkumar31082
@Ashokkumar31082 2 жыл бұрын
wut this aint unity lol
@kira890
@kira890 Жыл бұрын
@@Ashokkumar31082 I am a little late but he probably meant that he struggled with Unity, but now he found this tutorial to NOT use Unity to create his games, so that he doesn't struggle so much with making games. I hope I could enlighten you.
@okay1296
@okay1296 3 жыл бұрын
I now have ca. 3 or 4 years of programming experience in Java and Python. But sometimes I simply dont know how to implement somthing clean. Sure I could do it, but it will look like shit. You helped me a lot, how to implement all the stuff objectoriented. And a lot of times I think to abstract. Also I like your tutorials because you write all the stuff from scratch, only with the default java libarys. There are a lot of youtubers out there that would simply download Unity for 2D game development - im ok with that. But its hard to find good tutorials how to programm a 2D game engine from scratch. Also a lot of people is hating on Java, I cant understand that. Im intrested where you learned java. Keep it up, really good work and videos.
@RyiSnow
@RyiSnow 3 жыл бұрын
Yes, I also feel nothing against using engines such as Unity. If making a game is your sole purpose, using engine or library is a lot easier. That said developing it from scratch gives you deeper understanding on how game programs work and programming itself. Also, creating from scratch is fun! I'm glad you liked the videos. Always good to meet a fellow Java developer!
@marrokyoung4159
@marrokyoung4159 2 жыл бұрын
I came across your tutorials just a few days ago, but they are helping tremendously with making a space invaders esque game. Thank you so much Ive been learning a LOT
@crackrokmccaib
@crackrokmccaib 2 жыл бұрын
You're so incredibly talented at what you do. I hope you have a job that appreciates your skill and pays you generously for what you can do. I hope to someday be even 1/4 the developer that you are.
@RyiSnow
@RyiSnow 2 жыл бұрын
I'm just a mediocre programmer. I'm sure you can catch up :)
@cripz1436
@cripz1436 3 жыл бұрын
I'm just happy I got my sprite character to move, since started this tutorial 2 days ago its fun learning game dev again 😁 Thanks RyiSnow
@RyiSnow
@RyiSnow 3 жыл бұрын
Glad to hear that and welcome back to game dev :D
@5nsou
@5nsou 2 жыл бұрын
Man. you are actually a legend. You deserve like more than 1 mil subscribers. Youre the greatest!
@gopnik7234
@gopnik7234 2 жыл бұрын
Great Tutorial! Ive been loving it so far! Please keep uploading! For anyone who uses another IDE than RyiSnow and can't fix the "java.lang.IllegalArgumentException: input == null!" error here are solutions for VS Code and Intellij that worked for me: For Intellij: 1. Create the "res" folder as a new package in the src folder/section 2. Create the player folder in res as player.res so that it is a package inside of the "res" package 3. Add /res to the file path in the getPlayerImage-method so that it becomes "/res/player/boy....." 4. enjoy For VS Studio: 1. Create the "res" folder in the src folder/section 2. Create the "player" folder in the "res" folder 3. Add /res to the file path in the getPlayerImage-method so that it becomes "/res/player/boy....." 4. enjoy The error most likely occurs because only the "src" folder is saved in the project source folder
@kalebwilliams4681
@kalebwilliams4681 Жыл бұрын
THANKYOU SO MUCH
@gCoder_
@gCoder_ 9 ай бұрын
it happens to me in eclipse, i don't know why, could you please help me?
@mrshadow7399
@mrshadow7399 Ай бұрын
@@gCoder_ same issue, did you find a solution?
@gCoder_
@gCoder_ Ай бұрын
@@mrshadow7399 unfortunately my game stopped working and i gave up 😆 i didn't find a solution, i am so sorry 🙏 Keep going and good luck, you'll eventually find the solution..
@Alex-chesspenguin
@Alex-chesspenguin 8 ай бұрын
I’m an elementary student, this is very easy to understand, thanks!
@Asdpire
@Asdpire Жыл бұрын
If your thinking led to the same question as mine : "Wait a minute, we already tested the upPressed, downPressed etc. Why test it again just to update the sprite?" You can also just update the spritecounter in the if slot of the update method, like so : public void update(){ if(keyH.upPressed) { direction = "up"; y -= speed; spriteCounter++;} I just think it looks cleaner as a whole. Now it accomplishes the same thing with less nesting, see which you like better. I sincerely hope I'm not offending the author with my suggestions. Thank you for the instructive series!
@mathisseifert4073
@mathisseifert4073 7 сағат бұрын
Only problem is, if you press all keys at the same time your character animation is 4 times faster but thanks :)
@coldblade8795
@coldblade8795 Жыл бұрын
Thank you sir, I have always understood concepts of java but never really understood when to use them. Using your tutorial i was able to get a basic hold of 2d programming I hope you become a more popular teacher cause your teaching techniques are excellent and as always Thank You!
@lamagiadiele8879
@lamagiadiele8879 Жыл бұрын
Can i say it? YOU ARE A THE BEST TEACHER THAT I EVER HAD! Thank you very much, fantastic tutorial.
@108mmbb
@108mmbb 2 жыл бұрын
Thank you for this tutorial. This has been very easy to follow and learn from! I'm a beginner and didn't know a thing before I started this series.
@RyiSnow
@RyiSnow 2 жыл бұрын
Glad it was helpful!
@K-GamingSkyblock
@K-GamingSkyblock Жыл бұрын
For any of those who is only getting one sprite character, make sure to add break at the end of each case in the switch statement
@benn125m
@benn125m 2 жыл бұрын
Hello there. I just started learning a few days ago the beautiful language "java"..I am very glad I found your channel, a few video and already helped me understand more than just reading 📚. You deserve more views for sure. Keep up the good work. Wishing you a lot of success 🙌🏼
@RyiSnow
@RyiSnow 2 жыл бұрын
Thank you for your kind words!
@droidlycodes
@droidlycodes 2 жыл бұрын
Your art skills are great, spent a few hours last night messing around with converting your guide to Android so far I have an un animated sprite that can move around the screen smoothly. I'm going to see what I can learn tonight, I feel so new lol.
@droidlycodes
@droidlycodes 2 жыл бұрын
Also I enjoy that you explain things easily, plus everything makes sense from someone that does have java experience! With your permission I may do guides on this while I'm following along to convert to Android 😁
@moonlightmacky9055
@moonlightmacky9055 3 жыл бұрын
Your tutorials are amazing man, keep it up!!!
@RyiSnow
@RyiSnow 3 жыл бұрын
Thanks, will do!
@Tercer2
@Tercer2 2 ай бұрын
Its been years since this came out and it still hold true today!! Thanks for teaching me java!
@kanhchanaly6445
@kanhchanaly6445 3 жыл бұрын
Thank you so much for making time to share this tutorial with us! I’m really grateful!
@RyiSnow
@RyiSnow 3 жыл бұрын
No problem :) I'm glad you liked it!
@Talight-db9hj
@Talight-db9hj 5 ай бұрын
Бро, ты лучший❤ Ты единственный на кого я подписался не из вежливости, а из уважения к твоему труду и твоим знаниям!
@EM0920-u7j
@EM0920-u7j 2 ай бұрын
Gotta love forgetting to add break at the end of my case statements and spending like 5 minutes trying to figure out why only the left sprite is rendering. Love the Tutorials so far.
@daviddashen4070
@daviddashen4070 2 жыл бұрын
I have the solution for those of you, who are struggling with the error pinned in the comments and using VS Code (but you can give it a try in your IDE). I put the pictures in a single folder called player and then I moved the folder in the src folder. And then wrote this: File f1 = new File("./src/player/ninja_up_1.png"); File f2 = new File("./src/player/ninja_up_2.png"); File f3 = new File("./src/player/ninja_down_1.png"); File f4 = new File("./src/player/ninja_down_2.png"); File f5 = new File("./src/player/ninja_left_1.png"); File f6 = new File("./src/player/ninja_left_2.png"); File f7 = new File("./src/player/ninja_right_1.png"); File f8 = new File("./src/player/ninja_right_2.png"); up1 = ImageIO.read(f1); up2 = ImageIO.read(f2); down1 = ImageIO.read(f3); down2 = ImageIO.read(f4); left1 = ImageIO.read(f5); left2 = ImageIO.read(f6); right1 = ImageIO.read(f7); right2 = ImageIO.read(f8);
@AverageLowElo69
@AverageLowElo69 2 жыл бұрын
I swear you are god in disguise. Thank you so much! I was going to give up since I didn’t know what to do.
@pabloa1312
@pabloa1312 2 жыл бұрын
Thank you so much! omg I literally spent HOURS looking for a solution the other way. LIFE SAVER!
@matheusguedes8432
@matheusguedes8432 Жыл бұрын
it worked very well in intellij, thank you very much!
@killian9843
@killian9843 Жыл бұрын
I use VSCode and this worked hella good. thank u very much
@brendacchebet
@brendacchebet Жыл бұрын
I've been trying to fix this error for like days. Thankkssss
@jarrodmcevoy
@jarrodmcevoy 3 жыл бұрын
Can't wait for the background tiles :)
@flow2035
@flow2035 6 ай бұрын
Thank you mate! This is the best series in KZbin...keep going!!
@001archimedes
@001archimedes 2 жыл бұрын
I'm following along your tutorial, and I can say I'm having a lot of fun creating this game. Thank you for putting this together and for posting the resources needed to create the game. Your explanations are very easy to follow and does not take too long.
@RedlikMusic
@RedlikMusic 2 жыл бұрын
you can also use arrays to storage the images and then iterate through the values c:
@Team6OWG
@Team6OWG 2 жыл бұрын
How????? I am confused becasue we store BufferedImage objects, so we can create one 1 arrayList called images, that has all of the, but are we still creating up1, up2, down1, down2? or dont and just do image.add(ImageIO.read(getClass....etc ? But how do you get the string to change for "/player/boy_left1... or /player/boy_right2"???????? Can you show what you did?
@r4c_h3ll
@r4c_h3ll 2 жыл бұрын
@@Team6OWG @REDLIK share dude!! ((:
@ghostek7792
@ghostek7792 2 жыл бұрын
I love that you did this. you're really great at teaching it srsly dude don't underestimate your talent. I greatly appreciate your videos, I'm doing this as a summer project and I make edm so I'm going to be making full tracks for different settings and looking forward to drawing little characters
@alitahboub493
@alitahboub493 Жыл бұрын
Your explanations are very clear and straight forward! Great video! (I subscribed).
@hizokadarkwolf
@hizokadarkwolf 2 жыл бұрын
Incredible. What about storing the sprites inside an array? And better, having one single sprite sheet file and changing the position? For performance/memory. I think I will add these if it's not done on the videos to my version on my repository. Thanks a lot. Very fun tutorial.
@themasterrogerdelgado
@themasterrogerdelgado 11 ай бұрын
Enjoying this series a lot. Thank you.
@rokasvidziunas4396
@rokasvidziunas4396 Жыл бұрын
thank you so much for the amazing help
@SlothfulSage285
@SlothfulSage285 2 жыл бұрын
I was trying to limit myself to one video a night, but I could not help it and decided to do another! :)
@teodorairina0404
@teodorairina0404 2 жыл бұрын
You are doing a great job ! I will tell my friends about you !!!
@ncthatsme
@ncthatsme 2 жыл бұрын
Thank you so much. This has been really fun and very informative.
@mistiical3033
@mistiical3033 2 жыл бұрын
Hello, in class Player when you import Gamepanel and Keyhandler you get option to do main.Gamepanl etc i do not get these options when i type it in myself I get an error saying the import main cannot be resolved. any solutions, Thanks.
@mikemazanetz4183
@mikemazanetz4183 9 ай бұрын
thank you very much for these wonderful video tutorials. An excellent resource !
@mamapuci
@mamapuci 2 жыл бұрын
Im here bro. Ehehe. Really, lot learning from you. Thanks again bro
@neon-mask5171
@neon-mask5171 Жыл бұрын
i dont know what is happening my player sprite is not showing its showing the white rectangle only even though i have cut out the code for it. please help.
@dovydasbalciunas5002
@dovydasbalciunas5002 Жыл бұрын
Hey man! I'm trying to master java right now and I got a question. Were you just like messing around or is creating games your passion? Are you a game developer?
@LouShunt
@LouShunt 3 жыл бұрын
Hey I love your videos. But i got a question about the animation. The update method is based on the number of FPS. Which means that with the system you made, a person with 30 fps on the game will have 2x slower animation and a person with 120 fps 2x faster. Is there a way to have the same animation speed regardless of fps?
@RyiSnow
@RyiSnow 3 жыл бұрын
Yes, I think you can set the speed based on fps. Example: int speed = 240/FPS; This returns 4 if fps is 60 and returns 8 if fps is 30 so the player speed stays the same. That said if you set fps 241 or more, the speed becomes 0 (since int doesn't accept decimal) so player won't move at all in that case. So probably you'll need to change it to double and arrange the code a little bit. Like counting decimal behind the scene and when it hits 1, apply the movement or something.
@LouShunt
@LouShunt 3 жыл бұрын
@@RyiSnow Great idea! Thank you I will try it
@RyiSnow
@RyiSnow 3 жыл бұрын
Hi, I just realized that you were asking about the animation, not the player's movement speed. Maybe you've already figured it out but you can also set the sprite counter based on fps so the interval between sprite 1 and 2 stays the same.
@PappuAnsari-mv6ys
@PappuAnsari-mv6ys Ай бұрын
How to create a Source folder in BlueJ IDE.Please kindly help .
@kylellsuperman
@kylellsuperman 7 ай бұрын
I'm really enjoying this series so far. it's getting me back on my feet with a lot of my coding knowledge. i just noticed that you're putting a lot of your boolleans into your if statements with a equivalency check when boolleans are automatically true or false values. I just wanted to point out you could write it as "if(keyH.upPressed)" instead of "if(keyH.upPressed == true)" to save some space. I hope this can help someone out there. (Again, this only works for boollean variables.)
@smartestpotato2127
@smartestpotato2127 2 жыл бұрын
I’ve been following your tutorials to the t so far, but when after shifting the key input stuff to the player class and after clicking run, the square doesn’t move at all, do you have any clue what the problem could be? no errors are popping up, it’s just not getting my key inputs for some reason edit - never mind, I forgot to put the player update inside the game panel update
@ochkoochkeevich9123
@ochkoochkeevich9123 Жыл бұрын
Hello from Russia!!! You deserve more viewers)
@BigManBand22
@BigManBand22 2 жыл бұрын
You are a great teacher man! Arigato!
@playLaF
@playLaF 3 ай бұрын
Hi @RyiSnow I have a question, is it possible for the sprites to move diagonally? Thanks!
@davidbijaksantoso-085
@davidbijaksantoso-085 Жыл бұрын
is there a way to make another image animation (I want to add an idle animation when not pressing any keys)?
@IseOnCrack
@IseOnCrack 2 жыл бұрын
Wow, that's a really good and in depth tutorial, good job! I'm following the tutorial but im trying to do some things in "my way", for exemple, why dont you use entity as an abstract class? Instead of the 4 if you used for sprite number, my spriteNum grows every 10 spriteCounter (like yours), but for select the image in the switch i use if(spriteNum%2==0) , i find it a bit more elegant. Pls dont use keyHandler==true XD. Good job again!
@TamLe-yh3eg
@TamLe-yh3eg 11 ай бұрын
for anyone want the character to move diagonally, just replace all else if conditions in update method to all if conditions
@Madworld130
@Madworld130 24 күн бұрын
Thank you for this! Exactly what I needed
@thehandleisuseless
@thehandleisuseless 2 жыл бұрын
crazy to think that Minecraft was made like this, wow
@juandebenedetti2884
@juandebenedetti2884 2 жыл бұрын
What do you mean that MC was made like that? Minecraft was made with LWJGL to be able to draw using OpenGL, this being a hardware representation. This tutorial uses Swing, and Swing uses software rendering. The difference between the two is that hardware rendering has countless benefits. But I'm not saying that learning Swing to make video games is bad, don't get me wrong. RyiSnow teaches us the basics of 2D game development, which is essential if you want to get into something deeper like OpenGL.
@apro8723
@apro8723 Жыл бұрын
bro u mean the og legend of zelda bro.... dont get ur games mixed up.
@kyletran6160
@kyletran6160 Жыл бұрын
bro's good at drawing, i cant
@anicepineapple9067
@anicepineapple9067 Жыл бұрын
For anyone wondering how to resolve the error in VS code. Just right click on the res folder and select "add folder to java source path." this worked for me.
@ivangamer2888
@ivangamer2888 9 ай бұрын
I'm making a game as a final project (the game is Tron the one of the movie, the motorcycles that leave a wall of light behind them), the thing is that I don't know how to make that wall and I've been searching a lot and I can't find how to do it. I don't know if you or someone from the community could help me, thank you very much in advance.
@selimsmajlovic7871
@selimsmajlovic7871 Жыл бұрын
I require help. When I try to import "main.GamePanel" and "main.KeyHandler" nothing shows up. When I write the imports manualy, it is still an error, like it does not recognise the imports or something. Does anyone have an answer to this?
@idkwhoami9986
@idkwhoami9986 Жыл бұрын
not sure if you still need help but.. i had a similar problem, turns out it was because my GamePanel and KeyHandler files were not in a "main" folder. when I added them to a folder called "main", it worked as it did in the video
@silviuzlota6458
@silviuzlota6458 2 жыл бұрын
Cool video , I learned a lot from you.Thanks
@noahbarger1
@noahbarger1 2 жыл бұрын
If the sprite you made has more than 2 images, and when it stops animating you want it to go back to the first image, then after the "if" statement, put: if (keyH.upPressed || keyH... else { spriteNum = 1; }
@Madworld130
@Madworld130 24 күн бұрын
I know this is two years later, but thank you! This is what I was looking for because my sprite has 4 images per direction.
@brinklebros7136
@brinklebros7136 9 ай бұрын
I LOVE YOUR VIDEOS!. I made a few tweaks to this code, there I made use of arrays for my animation frames since i had 4 frames per walk cycle. I also made it so I could tweak the timing per frame more easily, and a few other things. its functionally the same but i love this engine your making and im learning a ton.
@NikitaBomba112
@NikitaBomba112 2 жыл бұрын
Thanks you so much for this video!
@suiz137
@suiz137 Жыл бұрын
5:14 can tell me why my line 'Player player = new Player(this.keyH);' is error constructor undefined ?
@solareclipsedudefinale9026
@solareclipsedudefinale9026 Жыл бұрын
Instead of "this.keyH", do "this, keyH" because we want to pass 2 arguments to the constructor
@lordkuuba3517
@lordkuuba3517 2 жыл бұрын
Hi! I have some problem with animation how to add more frames (4) ?
@maximhanzely1988
@maximhanzely1988 2 жыл бұрын
18:57 u can try adding something like this : else if (spriteNum == 2) { spriteNum = 3; } else if (spriteNum == 3) { spriteNum = 4; } before he adds this: else if (spriteNum == 8) { spriteNum = 1; } it worked for me idk if its good but hey it works
@badkinson5459
@badkinson5459 5 ай бұрын
at 14:50 my sprite has a white background how do you get yours to have a black background?
@badkinson5459
@badkinson5459 5 ай бұрын
I see now i just didn't commit out the two lines that's why my character had a white background in the square
@ganesh9952
@ganesh9952 3 ай бұрын
Bro I did comment out the two lines but it's still Showing the white box 😭 any fix for this ?
@S1mplector
@S1mplector 4 ай бұрын
Hey, RyiSnow! I have been following your tutorial and this has been amazing so far. I am excited to go through all of the videos on this series, thank you so much for the effort! I wanted to add something to the character walking animation. I updated the Player class slightly so it switches between walking sprites when a movement key is held and displays a static sprite when the key is released, by using a boolean. package entity; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import main.GamePanel; import main.KeyHandler; public class Player extends Entity { GamePanel gp; KeyHandler keyH; public Player(GamePanel gp, KeyHandler keyH) { this.gp = gp; this.keyH = keyH; setDefaultValues(); getPlayerImage(); } public void setDefaultValues() { x = 100; y = 100; speed = 4; direction = "down"; } public void getPlayerImage() { try { // static images up1 = ImageIO.read(getClass().getResourceAsStream("/player/up.png")); down1 = ImageIO.read(getClass().getResourceAsStream("/player/down.png")); left1 = ImageIO.read(getClass().getResourceAsStream("/player/left.png")); right1 = ImageIO.read(getClass().getResourceAsStream("/player/right.png")); // animations up2 = ImageIO.read(getClass().getResourceAsStream("/player/up2.png")); up3 = ImageIO.read(getClass().getResourceAsStream("/player/up3.png")); down2 = ImageIO.read(getClass().getResourceAsStream("/player/down2.png")); down3 = ImageIO.read(getClass().getResourceAsStream("/player/down3.png")); left2 = ImageIO.read(getClass().getResourceAsStream("/player/left2.png")); left3 = ImageIO.read(getClass().getResourceAsStream("/player/left3.png")); right2 = ImageIO.read(getClass().getResourceAsStream("/player/right2.png")); right3 = ImageIO.read(getClass().getResourceAsStream("/player/right3.png")); } catch(IOException e) { e.printStackTrace(); } } public void update() { boolean moving = false; if (keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) { moving = true; if (keyH.upPressed) { direction = "up"; y -= speed; } else if (keyH.downPressed) { direction = "down"; y += speed; } else if (keyH.leftPressed) { direction = "left"; x -= speed; } else if (keyH.rightPressed) { direction = "right"; x += speed; } spriteCounter++; if (spriteCounter > 12) { if (spriteNum == 1) { spriteNum = 2; } else if (spriteNum == 2) { spriteNum = 1; } spriteCounter = 0; } } if (!moving) { spriteNum = 1; // Reset to the static sprite when not moving } } public void draw(Graphics2D g2) { BufferedImage image = null; switch (direction) { case "up": if (keyH.upPressed) { image = (spriteNum == 1) ? up2 : up3; } else { image = up1; } break; case "down": if (keyH.downPressed) { image = (spriteNum == 1) ? down2 : down3; } else { image = down1; } break; case "left": if (keyH.leftPressed) { image = (spriteNum == 1) ? left2 : left3; } else { image = left1; } break; case "right": if (keyH.rightPressed) { image = (spriteNum == 1) ? right2 : right3; } else { image = right1; } break; } g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null); } } I have already learned so much, thank you so much for it again!
@Naturellement-w2y
@Naturellement-w2y Ай бұрын
I tried to put my sprite made on piskel (that I saved as .png) and it doesn't work. But when I tried it again with your sprite that I downloaded I worked perfectly. Any advice or help please ? Also very good tutorial
@NLogSpace
@NLogSpace 2 жыл бұрын
Nice video, I really enjoy your content! I also like to develop 2D tile-based games in Java, this series seems to be a good resource for me! Do you also use the entity component system (ECS) pattern for any of your games? I recently found out about it and I think it is a great way to structure code for this kind of games.
@naokhie9373
@naokhie9373 Жыл бұрын
why cant I import player in the game panel class? thanks
@bluestreak711
@bluestreak711 2 жыл бұрын
God is good and I am getting a little better in troubleshooting. the mistake I made that should be avoided on this video was that I did not include the line of code, "import java.awt.image.BufferedImage; " right after the " package entity;" in the Entity class. To all my peers, I would encourage you to make sure all your imports are added. This will solve a lot of your problems. To our teacher @RyiSnow, for future tutorials, please call more attention to when you import something or click to import something. Sometimes you click so fast and if I blink, I miss where you click to have Eclipse automatically import something. Sometimes, I do not ever see it, but I know it is there. So far, I have been able to use Eclipse to track it down. Other Input: MSpaint and Gimp both are good alternatives to make Pixel art. The only problem with MSpaint is that it is difficult to make it 16x16px, but if you make your draw area close to square and draw your images, you can use Gimp to both resize your image to 16x16px and a few simple clicks will make the white background transparent. You should be able to do everything inside of Gimp, but I am not yet well versed with Gimp as I am MsPaint as of yet. Gimp is almost as powerful as Photoshop and is completely free and opensource, so it is worth learning. Fun Thought: I have considered taking photos of myself, and other people, in all of the same poses as your little boy sprite and inserting that into the game. Yes, I could probably do animated pixel art also, but I am talking about just unedited pictures except for the 16x16px resizing and making the background transparent.
@megha9307
@megha9307 Жыл бұрын
Hey man I keep getting this error and I think it has something to do with imports you think you can take a look ---------------------------------- package entity; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import main.GamePanel; import main.KeyHandler; public class Player extends Entity{ GamePanel gp; KeyHandler keyH; public Player(GamePanel gp, KeyHandler keyH) { this.gp = gp; this.keyH = keyH; setDefaultValues(); getPlayerImage(); } public void setDefaultValues() { x = 100; y = 100; speed = 4; direction = "down"; } public void getPlayerImage() { try { down1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_1.png")); down2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_2.png")); up1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_1.png")); up2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_2.png")); right1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right_1.png")); right2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right_2.png")); left1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_1.png")); left2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_2.png")); }catch (IOException e) { e.printStackTrace(); } } public void update() { if (keyH.upPressed == true) { direction = "up"; y -= speed; } else if(keyH.downPressed == true) { direction = "down"; y += speed; } else if(keyH.leftPressed == true) { direction = "left"; x -= speed; } else if(keyH.rightPressed == true) { direction = "right"; x += speed; } } public void draw(Graphics2D g2) { BufferedImage image = null; switch(direction) { case"up": image = up1; break; case"down": image = down1; break; case"right": image = right1; break; case"left": image = left1; break; } g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null); } }
@megha9307
@megha9307 Жыл бұрын
The error that im getting is The type java.io.IOException cannot be resolved. It is indirectly referenced from required type java.awt.Font
@bluestreak711
@bluestreak711 Жыл бұрын
​@@megha9307 I am going to take a wild guess at what your problem is, but without seeing all of your code and not just your player class, it is hard to tell, but I am going to give you a few things to look for and there is a good chance you will find it on your own and be a better programmer for it. First, see if it helps to add import javax.imageio.ImageIO; to your entity class for reasons I explain below. If that doesn't work and if you are using eclipse like Ryisnow and me, read the error line and see exactly what it reads. It will give you a lot of clues. Remember that your player class extends the entity class. You pasted your code for the Player class. If you happen to forget an import in your Entity class, it can cause an error on your Player class and cause it not to work. Be watchful of each time Ryi uses a variable or attribute from another class. You might be working in the Player class and think you should import something there when you should import something to the Entity class because that is where that item in question originates. The same goes for making variables public when you call them from a different class than where they were declared. In a video coming up there is an instance where Ryi forgets to make a certain variable public and has to go back and make it public, so the program will work. I do not remember if this applies to both folders and packages, but this definitely applies to packages. Each time Ryi has you create a new package or a new folder, you have to import your main class or some other class to that new class you just created or vice versa, so it is not isolated and can talk to the rest of your program. Eclipse will often do this with a couple of clicks and Ryisnow will click so fast to generate that import automatically that if you blink, I assure you will miss it. I have also had a few instances where Eclipse offered to generate that import line of code for Ryisnow, but it wouldn't do it for me, so I had to read the error details very carefully and figure out which import line of code I needed to add and add it manually. Good luck and comment back when you figure it out because these comments help other people with their code also!
@bluestreak711
@bluestreak711 Жыл бұрын
@@megha9307 Libbania Reyes had some good suggestions of a few things to check in a comment below also.
@LEALCUTSS
@LEALCUTSS 2 жыл бұрын
Thanks, very informative video!
@zitrix7848
@zitrix7848 2 жыл бұрын
*my sprites are not drawing to the screen, i used the same sprites from video description everything loads in without any error but the sprites won't load to screen please help*
@lvdv4j
@lvdv4j 2 жыл бұрын
Did you add the getPlayerImage(); method to the player constructor in the Player class? I had the same problem and this was my solution
@tito-kz1fm
@tito-kz1fm 2 жыл бұрын
Hey, the character only appearing when going D key, WAS keys won't show character? can you help me with that?
@JCIgaming
@JCIgaming 2 жыл бұрын
In case you haven't figured it out yet, you are probably missing some breaks in switch statement. It's such a stupid mistake but it slipped by me and hopefully its the same solution for you.
@weptashka
@weptashka 2 жыл бұрын
You make awesome tutorials! Thank you!
@Zuko0847
@Zuko0847 Жыл бұрын
1:39 for some reason it doesn't show me the import option, any idea why? btw thanks a lot for the video, you're doing an amazing job!
@idkafknhandlebrah
@idkafknhandlebrah Жыл бұрын
it doesnt give me import player option at 2:38
@idkafknhandlebrah
@idkafknhandlebrah Жыл бұрын
nvm fixed it
@Lazullien
@Lazullien 2 жыл бұрын
this might be a little late but if i switch to bufferedimage, not even drawing the images, just assigning the pngs, my key binding seems to break, whenever i stop calling the method that assigns the pngs it goes back to normal, if i add any other functions with the images on they work just fine, but the key binding breaks, i'm not sure how keybinding focus works but it still seems quite awful
@Lazullien
@Lazullien 2 жыл бұрын
nevermind i'm leaving this comment here for people with the same problem, key binding's default focus is just as crappy as keylistener's, put JComponent.WHEN_IN_FOCUSED_WINDOW inside the getInputMap()'s parameter to fix this
@TulhoKay
@TulhoKay Жыл бұрын
when you created the player, you passed this, here you don't accept it at all, it keeps asking for cast. I do it to later give an error of creating local variable.
@TheG60528XiJinPing
@TheG60528XiJinPing 7 ай бұрын
for me its only showing one direction of image
@doxuantruong04
@doxuantruong04 6 ай бұрын
i use netbeans IDE and i don't play program, can use help me?
@SouryadiptoSamadritaMazumdar
@SouryadiptoSamadritaMazumdar 25 күн бұрын
i'm trying but the rect is not moving even though everything's fine
@Magnezone962
@Magnezone962 10 ай бұрын
I wanted to add 2 images so that AWSD key are player 1 and the arrow keys are player 2
@CanePazzoHD
@CanePazzoHD 2 жыл бұрын
Dear Ryisnow, thank you for the immense work you are doing. I have recently started to follow your tutorials step by step. I’m having a great time and you’re very clear in your explanation. I have a problem I can’t solve, can you help me? When I press start, the character moves in the direction I have indicated. At some point, however, it freezes and does not move anymore despite me pressing the command on the keyboard. Shortly after it starts moving again and so on it stops again. The FPS are fixed at 60, it seems to me that everything I marked on the KeyHandler is correct... what can it be? Thank you very much for your availability!
@pauldhartley
@pauldhartley 3 ай бұрын
If you are going to use the keyPress data in the braw method, why have a direction variable at all! I did enjoy your video very much and is helping me to understand sprites etc. Thank you
@brayanavilacaravaca4964
@brayanavilacaravaca4964 2 жыл бұрын
i have a problem, the class KeyHander no works for some reason.. And I did it the same of the video... Any sugestions?
@jannfrancisjuson6229
@jannfrancisjuson6229 Жыл бұрын
Why can't my character move down? Even though I followed it correctly
@alex_tremo255
@alex_tremo255 7 ай бұрын
3k like. Thanks for your tutorials man
@sugarycashew3035
@sugarycashew3035 Жыл бұрын
I get to the 7:10 test and the rectangle no longer moves :( I left the old if loop location updater in the GamePanel class and that box still moves. But the new box will not budge. Advice? I've got the new loop in the Player class, as well as updated all the x's and y's.
@marinasalonso2716
@marinasalonso2716 3 жыл бұрын
Hello, I have a problem with my getPlayerImage() method, when I start the program an error occurred and it says "Caused by: java.lang.IllegalArgumentException: input == null!", I tried to change the path of my pictures but nothing changed. I have the same code as you but the only difference is the path for the pictures.
@RyiSnow
@RyiSnow 3 жыл бұрын
Sounds like the program is failing to load your image. Are you using png file? Also, check if you have spelled the file names right such as not forgetting to type underscore, back slash etc... that often happens.
@marinasalonso2716
@marinasalonso2716 3 жыл бұрын
@@RyiSnow To test the method I used your sprites so the image isn't a problem, I think. And I thought that the path was wrong too, but I checked several times and nothing
@RyiSnow
@RyiSnow 3 жыл бұрын
Can I see your Player class? You can paste it here.
@marinasalonso2716
@marinasalonso2716 3 жыл бұрын
@@RyiSnow I sent like 5 times a message with my git depository, cause I couldn't write my code on commentary of this video, I don't know why, so is there another way to show you the code ?
@RyiSnow
@RyiSnow 3 жыл бұрын
@@marinasalonso2716 You cannot paste external link in KZbin comment. It most likely gets regarded as spam. You cannot directly copy & paste your code here? If it still doesn't work, you can contact me via twitter.
@luklukfamous175
@luklukfamous175 9 ай бұрын
i have question do you recomend some kind of books?
@pietrosica5265
@pietrosica5265 2 жыл бұрын
I have this problem : i can move the rectangle so i guess in the key listener class and update method there s no error but when i import the images (i just copied like in the video) and run the program i can see my player in the defaultValues position and direction so the sprites are loaded but i can’t move it (i tried to comment the getPlayerImage(); and the g2.drawImage(x,y,gp.tileSize,gp.tileSize,null) and i wrote again g2.fillRect(x,y,gp.tileSize, ,gp.tileSize) , so i can move again the rectangle , but with te image loaded i can’t move the player and the program runs with no error
@natesannatesan2220
@natesannatesan2220 Жыл бұрын
For me too
@samirunlu9900
@samirunlu9900 Жыл бұрын
@@natesannatesan2220 I used a different key instead (VK_W,A,D,X OR SOMETHING 😊) , the arrow up, down, left, right (VK_UP,DOWN,LEFT,RIGHT) and it worked 😊. had the same problem hopefully it helped.
@miguelmalini2635
@miguelmalini2635 Жыл бұрын
same
@JZSNooB
@JZSNooB 3 жыл бұрын
Ah, so I see you are using 8 different images for the player sprite. Would you ever optimize the animation system to support reflections, such that you only use 4 unique sprites? Like "Boy_down_2" is just "Boy_down_1" but flipped on the y-axis, and left_1 and left_2 are just right_1 and right_2, but reflected over y, and so on.
@RyiSnow
@RyiSnow 3 жыл бұрын
That sounds like an interesting idea (never thought about it) but probably won't fit this game because not every character is symmetrical. For example, if a character is holding a sword on their right hand, flipping the sprite will produce a strange result (the character will be holding the sword with his left hand). So I think I will stick to the current system for the time being. Thank you for the suggestion though :)
@JZSNooB
@JZSNooB 3 жыл бұрын
@@RyiSnow Understandable. In that sword senario, that definitely wouldn't work, but I just figured that if you're going for a retro style graphics, that working within some artistical limitations like a limited color palette (or in this case a limited number of keyframes) might spark some creativity or just be a fun extra challenge. Ofcourse though it is your project, and I'll watch the series regardless!
@XtoInf
@XtoInf 3 жыл бұрын
My sprite seems to only change to the right and up sprite’s
@chochi7962
@chochi7962 Ай бұрын
I have a problem but not with the code. I cant make my image with transparent background. Can someone help me.
@krishmangat3224
@krishmangat3224 8 ай бұрын
I am having trouble with the movement(I have followed all the directions properly but I think that the movement logic is wrong in the tutorial) -When I press right then while right is still pressed, I press up or down, the sprite will move up or down and when I let go of up or down (remember right is still pressed), the sprite will continue to go right. - The problem is that this does not work for when I am going up and then I want to go left or right. - There is something wrong with the if statements, we check for up before down,left and right so anything below in the if statement is overruled by everything above. Is is there any way I can fix this bug?
@s1moni336
@s1moni336 Жыл бұрын
isn't it better just to make a walk class? so we can use it for other characters too?
@captinspud
@captinspud 6 ай бұрын
it doesn't run anymore and it is a repaint error. i have no clue what to do
@miarochelle5863
@miarochelle5863 5 ай бұрын
Hi! This is an old video but I am trying to resolve an error I get at 9:32. package entity; import java.awt.image.BufferedImage; public class Entity { public int x, y; public int speed; public BufferedImage up, down, right, left; public String direction; } It gives me the error BufferedImage cannot be resolved. Does anyone know why? I'm using Eclipse.
@suanlatudousi
@suanlatudousi 2 ай бұрын
Same issue for me too, BufferedImage cannot be resolved. also it mentions that BufferedImage is already indirectly referenced from required type java.awt.Graphics2D
@wen2790
@wen2790 10 ай бұрын
Hello, I have an error at 2:23 when you type -Player player = new Player(this.KeyH){}, here Player is at the beginning and Player after new I have an error and it says like this - Player cannot be resolved to a type , help me how can I fix it?
@jackzytt
@jackzytt 5 ай бұрын
I have a question, why is the character way smaller for me?
@Noogai
@Noogai Жыл бұрын
Hi! So I've been following your videos almost line by line but when I had to instantiate the Player class in the GamePanel class, it gives me an error that says "The constructor Player(KeyHandler) is undefined". I manually imported "entity.Player" because my quick fix suggestion didn't have one and I've double checked the codes and I think everything is the same with your code. (edit: okay I'm sorry, I didn't check hard enough, I saw it immediately after checking once again. Turns out that what you put was (this,keyH) using a comma, and not (this.keyH) using a period. Just changed it and it's all good now. Also thanks for the tutorials! Great vids and very helpful explanations)
Drawing Tiles - How to Make a 2D Game in Java #4
27:26
RyiSnow
Рет қаралды 154 М.
10 Minute Pixel Sprite TUTORIAL for COMPLETE BEGINNERS
10:18
Eli Cuaycong
Рет қаралды 288 М.
Thank you Santa
00:13
Nadir Show
Рет қаралды 43 МЛН
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 26 МЛН
Game Loop and Key Input - How to Make a 2D Game in Java #2
34:48
I Made a Graphics Engine
6:42
Zyger
Рет қаралды 261 М.
6 Years of Blender in 13 Minutes
13:01
Kaizen
Рет қаралды 341 М.
Spritesheets | Coding a 2D Game Engine in Java #14
22:57
GamesWithGabe
Рет қаралды 14 М.
Making a Game With C++ and SDL2
8:14
PolyMars
Рет қаралды 1,7 МЛН
The First Game Done (UI) - How to Make a 2D Game in Java #10
34:50
Collision Detection - How to Make a 2D Game in Java #6
26:44
RyiSnow
Рет қаралды 100 М.
How I Beat The Password Game
39:53
Bog
Рет қаралды 3,1 МЛН
Thank you Santa
00:13
Nadir Show
Рет қаралды 43 МЛН