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

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น • 613

  • @RyiSnow
    @RyiSnow  3 ปีที่แล้ว +101

    To see how to read images in IntelliJ, please check this video:
    th-cam.com/video/Q9jtg0ocGgo/w-d-xo.html
    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 3 ปีที่แล้ว +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 3 ปีที่แล้ว

      @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 ปีที่แล้ว +1

      @Jackson did you can compile it?

    • @shinyspy850
      @shinyspy850 2 ปีที่แล้ว +2

      you should pin this comment, it was very helpfull.

  • @organista03
    @organista03 2 ปีที่แล้ว +238

    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 ปีที่แล้ว +14

      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 ปีที่แล้ว +6

      there's several solutions posted for IntelliJ, but somehow NOTHING works for me... always get NullPointerException
      I give up

    • @seamus9898
      @seamus9898 2 ปีที่แล้ว +6

      omg I just forgot to add
      setDefaultValues();
      getPlayerImage();"
      I'm so annoyed at myself :((((

    • @landon9889
      @landon9889 2 ปีที่แล้ว

      Thank you!

  • @michaelharrington5860
    @michaelharrington5860 ปีที่แล้ว +43

    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  ปีที่แล้ว +10

      It warms my heart to hear that! I hope you and your son will keep enjoying coding :)

  • @yasthatsme
    @yasthatsme 3 ปีที่แล้ว +121

    You deserve more viewers! I have learned so much from you

    • @RyiSnow
      @RyiSnow  3 ปีที่แล้ว +15

      Glad it helped! Thank you for the comment.

    • @thenolife1957
      @thenolife1957 3 ปีที่แล้ว +2

      Well most ppls don't play java games no more except for me

    • @faced2404
      @faced2404 2 ปีที่แล้ว +1

      @@thenolife1957 minecraft is originally made in java

    • @thenolife1957
      @thenolife1957 2 ปีที่แล้ว +1

      @@faced2404 except from that

    • @thenolife1957
      @thenolife1957 2 ปีที่แล้ว

      Heheeee

  • @lvdv4j
    @lvdv4j 2 ปีที่แล้ว +46

    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 ปีที่แล้ว +1

      thx

    • @lvdv4j
      @lvdv4j ปีที่แล้ว

      @@easypeezy9142 Glad I could help 😄

    • @RealSenn
      @RealSenn ปีที่แล้ว +1

      I was stuck for 2 hours. Thank you very much!

    • @lvdv4j
      @lvdv4j ปีที่แล้ว

      @@RealSenn You're welcome 😄 I'm glad I could help!

    • @scusmiwhat
      @scusmiwhat ปีที่แล้ว +1

      TYSM!!!!

  • @snarpking
    @snarpking 2 ปีที่แล้ว +23

    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 ปีที่แล้ว +1

      wut this aint unity lol

    • @kira890
      @kira890 ปีที่แล้ว +1

      @@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.

  • @marrokyoung4159
    @marrokyoung4159 2 ปีที่แล้ว +12

    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

  • @okay1296
    @okay1296 3 ปีที่แล้ว +18

    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 ปีที่แล้ว +14

      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!

  • @Alex-chesspenguin
    @Alex-chesspenguin 9 หลายเดือนก่อน +7

    I’m an elementary student, this is very easy to understand, thanks!

  • @5nsou
    @5nsou 2 ปีที่แล้ว +6

    Man. you are actually a legend. You deserve like more than 1 mil subscribers. Youre the greatest!

  • @gopnik7234
    @gopnik7234 2 ปีที่แล้ว +11

    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_ 10 หลายเดือนก่อน

      it happens to me in eclipse, i don't know why, could you please help me?

    • @mrshadow7399
      @mrshadow7399 2 หลายเดือนก่อน

      @@gCoder_ same issue, did you find a solution?

    • @gCoder_
      @gCoder_ 2 หลายเดือนก่อน

      @@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..

  • @Asdpire
    @Asdpire ปีที่แล้ว +5

    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 25 วันที่ผ่านมา

      Only problem is, if you press all keys at the same time your character animation is 4 times faster
      but thanks :)

    • @IGSparkew
      @IGSparkew 8 วันที่ผ่านมา

      @@mathisseifert4073with else if you can’t use 4 button at the same time so you can use spriteCounter++ in if and else if statement

  • @crackrokmccaib
    @crackrokmccaib 2 ปีที่แล้ว +15

    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 ปีที่แล้ว +4

      I'm just a mediocre programmer. I'm sure you can catch up :)

  • @cripz1436
    @cripz1436 3 ปีที่แล้ว +16

    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 ปีที่แล้ว +2

      Glad to hear that and welcome back to game dev :D

  • @108mmbb
    @108mmbb 3 ปีที่แล้ว +8

    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  3 ปีที่แล้ว +1

      Glad it was helpful!

  • @coldblade8795
    @coldblade8795 ปีที่แล้ว +1

    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!

  • @suiz137
    @suiz137 ปีที่แล้ว +1

    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

  • @badkinson5459
    @badkinson5459 6 หลายเดือนก่อน +1

    at 14:50 my sprite has a white background how do you get yours to have a black background?

    • @badkinson5459
      @badkinson5459 6 หลายเดือนก่อน +1

      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 4 หลายเดือนก่อน

      Bro I did comment out the two lines but it's still Showing the white box 😭 any fix for this ?

  • @lamagiadiele8879
    @lamagiadiele8879 ปีที่แล้ว +2

    Can i say it? YOU ARE A THE BEST TEACHER THAT I EVER HAD! Thank you very much, fantastic tutorial.

  • @EM0920-u7j
    @EM0920-u7j 3 หลายเดือนก่อน

    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.

  • @Sviskebisk
    @Sviskebisk 2 ปีที่แล้ว +2

    Thank you Ryi

    • @RyiSnow
      @RyiSnow  2 ปีที่แล้ว

      Thank you for your support!!! I hope you'll enjoy the series :)

  • @droidlycodes
    @droidlycodes 2 ปีที่แล้ว +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 😁

  • @K-GamingSkyblock
    @K-GamingSkyblock ปีที่แล้ว +5

    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

  • @RedlikMusic
    @RedlikMusic 2 ปีที่แล้ว +7

    you can also use arrays to storage the images and then iterate through the values c:

    • @Team6OWG
      @Team6OWG 2 ปีที่แล้ว +1

      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!! ((:

  • @Aksht_4D-i-D
    @Aksht_4D-i-D 7 วันที่ผ่านมา +1

    you what if we want to have a re images a 32 by 32 ?

  • @Tercer2
    @Tercer2 3 หลายเดือนก่อน

    Its been years since this came out and it still hold true today!! Thanks for teaching me java!

  • @mistiical3033
    @mistiical3033 2 ปีที่แล้ว +3

    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.

  • @moonlightmacky9055
    @moonlightmacky9055 3 ปีที่แล้ว +6

    Your tutorials are amazing man, keep it up!!!

    • @RyiSnow
      @RyiSnow  3 ปีที่แล้ว +4

      Thanks, will do!

  • @neon-mask5171
    @neon-mask5171 ปีที่แล้ว +2

    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.

  • @selimsmajlovic7871
    @selimsmajlovic7871 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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

  • @kanhchanaly6445
    @kanhchanaly6445 3 ปีที่แล้ว +2

    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!

  • @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.

  • @PappuAnsari-mv6ys
    @PappuAnsari-mv6ys 2 หลายเดือนก่อน +1

    How to create a Source folder in BlueJ IDE.Please kindly help .

  • @daviddashen4070
    @daviddashen4070 2 ปีที่แล้ว +43

    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

  • @ivangamer2888
    @ivangamer2888 10 หลายเดือนก่อน +1

    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.

  • @Zuko0847
    @Zuko0847 ปีที่แล้ว +2

    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!

  • @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.

  • @hizokadarkwolf
    @hizokadarkwolf 2 ปีที่แล้ว +5

    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.

  • @pietrosica5265
    @pietrosica5265 2 ปีที่แล้ว +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

  • @davidbijaksantoso-085
    @davidbijaksantoso-085 ปีที่แล้ว +1

    is there a way to make another image animation (I want to add an idle animation when not pressing any keys)?

  • @zitrix7848
    @zitrix7848 2 ปีที่แล้ว +1

    *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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

    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.

  • @rinesamorina7465
    @rinesamorina7465 3 ปีที่แล้ว +1

    I can get the image to show on the window but not move (and I can also get the rectangle to move but only if its a rectangle not the image). There doesn't seem to be any syntax errors so I don't know what I did wrong. Pls help

    • @RyiSnow
      @RyiSnow  3 ปีที่แล้ว

      Can I see your Player class?

    • @rinesamorina7465
      @rinesamorina7465 3 ปีที่แล้ว

      @@RyiSnow sure where do i send it

    • @RyiSnow
      @RyiSnow  3 ปีที่แล้ว

      You can paste it here.

    • @rinesamorina7465
      @rinesamorina7465 3 ปีที่แล้ว

      @RyiSnow actually now it doesnt work for the rectangle too, it just paints it but it doesnt move

    • @natesannatesan2220
      @natesannatesan2220 ปีที่แล้ว

      Same here

  • @lordkuuba3517
    @lordkuuba3517 2 ปีที่แล้ว +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

  • @miarochelle5863
    @miarochelle5863 6 หลายเดือนก่อน +1

    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 3 หลายเดือนก่อน

      Same issue for me too, BufferedImage cannot be resolved. also it mentions that BufferedImage is already indirectly referenced from required type java.awt.Graphics2D

  • @flow2035
    @flow2035 6 หลายเดือนก่อน

    Thank you mate! This is the best series in TH-cam...keep going!!

  • @Lazullien
    @Lazullien 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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

  • @ghostek7792
    @ghostek7792 2 ปีที่แล้ว +1

    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

  • @templeOfChrist1994
    @templeOfChrist1994 ปีที่แล้ว

    At 6:33 can you explain why you kept the tile size inside of "GamePanel" instead of moving it to the "Entity" class

    • @josepablo4575
      @josepablo4575 ปีที่แล้ว

      I think it could be because he wanted to implement entities bigger than the size of 1 GamePanel in later videos

  • @TheG60528XiJinPing
    @TheG60528XiJinPing 8 หลายเดือนก่อน +1

    for me its only showing one direction of image

  • @JuanPombo-nc5dp
    @JuanPombo-nc5dp ปีที่แล้ว

    you have nice tutorial, but i need help.
    for the character i have 3 pictures for up and down, and have 4 pictures for right and left. iam in 20:52 of the video and my character when he is gos de up and down show a moment when he dessapear because i have 4 spriteNumber and just 3 pictures for down and up. when hes go to the left or right hes looks normal but the problem is when hes go to up and down, hes disappear for a one frame!

  • @alitahboub493
    @alitahboub493 ปีที่แล้ว +1

    Your explanations are very clear and straight forward! Great video! (I subscribed).

  • @marinasalonso2716
    @marinasalonso2716 3 ปีที่แล้ว +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 TH-cam 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.

  • @DravenFNM
    @DravenFNM 3 ปีที่แล้ว +1

    I thought setting variable "direction" to down was unnecessary at 11:40, but I was completely wrong cause it fired repeated errors.. why is it needed?

    • @RyiSnow
      @RyiSnow  3 ปีที่แล้ว +1

      Without setting that the direction variable is null (not instantiated) so you likely get NullPointer exception error. You need to set a value to avoid that either in Entity class or Player class.

    • @DravenFNM
      @DravenFNM 3 ปีที่แล้ว +1

      @@RyiSnow Oh I see.. thank you 🙏

  • @benn125m
    @benn125m 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

      Thank you for your kind words!

  • @wen2790
    @wen2790 11 หลายเดือนก่อน

    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?

  • @SkullGD.
    @SkullGD. 2 ปีที่แล้ว

    When I run it at 14:50 in the video the screen is white and I get a flood of errors like too many to write, the screen is white, and it says Cannot invoke “Object.getClass()” because “this.gp” is null. Can you help me with this?

    • @SkullGD.
      @SkullGD. 2 ปีที่แล้ว

      It also says The method drawImage(BufferedImage, BufferedImageOp, int int) in the type Graphics 2D is not applicable for the arguments (BufferedImage, int, int, int, int)

    • @SkullGD.
      @SkullGD. 2 ปีที่แล้ว

      Nevermind I figured it out lol

  • @themasterrogerdelgado
    @themasterrogerdelgado ปีที่แล้ว

    Enjoying this series a lot. Thank you.

  • @jimisperks
    @jimisperks ปีที่แล้ว

    I can't get my spirits into the res and player resource I don't know where to find it I don't know how to set my eclipse up where I can have that file selecting bar on the right

    • @jimisperks
      @jimisperks ปีที่แล้ว

      What I'm saying is that I don't have that package explorer option on the left so I can't find my files what did I do

  • @Rockyzach88
    @Rockyzach88 ปีที่แล้ว +1

    Apparently getClass().getRAS() starts at the root folder of the class so to get it to work like that you have to put the resources in the entity folder or package (w/e).

  • @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.

  • @doxuantruong04
    @doxuantruong04 6 หลายเดือนก่อน +1

    i use netbeans IDE and i don't play program, can use help me?

  • @jarrodmcevoy
    @jarrodmcevoy 3 ปีที่แล้ว +2

    Can't wait for the background tiles :)

  • @SouryadiptoSamadritaMazumdar
    @SouryadiptoSamadritaMazumdar หลายเดือนก่อน

    i'm trying but the rect is not moving even though everything's fine

  • @TamLe-yh3eg
    @TamLe-yh3eg ปีที่แล้ว +2

    for anyone want the character to move diagonally, just replace all else if conditions in update method to all if conditions

    • @Madworld130
      @Madworld130 หลายเดือนก่อน

      Thank you for this! Exactly what I needed

  • @Talight-db9hj
    @Talight-db9hj 6 หลายเดือนก่อน

    Бро, ты лучший❤
    Ты единственный на кого я подписался не из вежливости, а из уважения к твоему труду и твоим знаниям!

  • @SlothfulSage285
    @SlothfulSage285 2 ปีที่แล้ว +2

    I was trying to limit myself to one video a night, but I could not help it and decided to do another! :)

  • @lukeeckardt7675
    @lukeeckardt7675 7 วันที่ผ่านมา

    The scwitch(direction) fall dont work: i try this: BufferedImage image = switch (direction) {
    case "up" -> (spriteNum == 1) ? up1 : up2;
    case "down" -> (spriteNum == 1) ? down1 : down2;
    case "right" -> (spriteNum == 1) ? right1 : right2;
    case "left" -> (spriteNum == 1) ? left1 : left2;
    default -> null;
    }; but i can only see 1 Picture at every way..

  • @rokasvidziunas4396
    @rokasvidziunas4396 ปีที่แล้ว

    thank you so much for the amazing help

  • @acidic231
    @acidic231 2 ปีที่แล้ว +1

    how would the folder part at 8:53 be done in BlueJ?

    • @flyingxo
      @flyingxo ปีที่แล้ว

      did u ever figure this out bc im struggling LOL

    • @kira890
      @kira890 ปีที่แล้ว

      @@flyingxo I really wonder if you're still struggling. I don't use BlueJ, just curious. c:

    • @flyingxo
      @flyingxo ปีที่แล้ว

      @@kira890 i think i found a workaround by putting the folder that i used for my sprites into the folder that had something to do with my project.. i forgot LOL but after i did that i could use the name of the file to refer to it
      if you’re interested i can find my code and see what i did exactly :p

  • @PetersonSnippets
    @PetersonSnippets หลายเดือนก่อน

    does anyone get this error Error: Could not find or load main class main.Main
    Caused by: java.lang.ClassNotFoundException: main.Main. when they create the source folder res @8:30

  • @naokhie9373
    @naokhie9373 ปีที่แล้ว +1

    why cant I import player in the game panel class? thanks

  • @LouShunt
    @LouShunt 3 ปีที่แล้ว +5

    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 ปีที่แล้ว +6

      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 ปีที่แล้ว +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.

  • @smartestpotato2127
    @smartestpotato2127 2 ปีที่แล้ว +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

  • @captinspud
    @captinspud 7 หลายเดือนก่อน

    it doesn't run anymore and it is a repaint error. i have no clue what to do

  • @XtoInf
    @XtoInf 3 ปีที่แล้ว +1

    My sprite seems to only change to the right and up sprite’s

  • @chochi7962
    @chochi7962 2 หลายเดือนก่อน

    I have a problem but not with the code. I cant make my image with transparent background. Can someone help me.

  • @jannfrancisjuson6229
    @jannfrancisjuson6229 ปีที่แล้ว

    Why can't my character move down? Even though I followed it correctly

  • @mamapuci
    @mamapuci 2 ปีที่แล้ว +1

    Im here bro. Ehehe. Really, lot learning from you. Thanks again bro

  • @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?

  • @playLaF
    @playLaF 4 หลายเดือนก่อน +1

    Hi @RyiSnow
    I have a question, is it possible for the sprites to move diagonally? Thanks!

  • @anasfaisal4581
    @anasfaisal4581 ปีที่แล้ว

    how do i create a new folder? im working on intelliJ, and i can see you made the res folder indside the src folder.

    • @RyiSnow
      @RyiSnow  ปีที่แล้ว

      I made a video for that!
      th-cam.com/video/Q9jtg0ocGgo/w-d-xo.html

  • @JadiBudiLaksono
    @JadiBudiLaksono ปีที่แล้ว

    I'm stuck in running with new images @sts (spring tool suite application), it's getting me null when read image or maybe my folder res structure.
    Thank you and I'm sorry..

  • @oyorin6076
    @oyorin6076 3 ปีที่แล้ว +2

    I wanted to know if I can put more than 8 sprites, like 16 sprites so that the character has more movement, how to do this?

    • @printlnPrints
      @printlnPrints 3 ปีที่แล้ว

      you can scale up the process to include more image files, essentially what was done in the tutorial but repeated for each file. You'll have to add a line variables in Entity to initialize the extra files, add lines in getPlayerImage() to read the extra files, and modify the if statement with sprite counter to switch spriteNum more than just two times. You can use modulo to make the spriteNum if statement easier too, doing spriteCounter % 10 == 0 in the if statement and removing spriteCounter = 0; at the end will change the sprite every second, rather than hard coding it in for every second. since modulo returns 0 anytime x is divisible by y, thus it will change any time spriteCounter is divisible by 10 (10, 20, 30, etc...), add an else statement at the end to set the spriteNum back to 1 to display the idle sprite while not moving and re add spriteCounter = 0; in there as well.

    • @oyorin6076
      @oyorin6076 2 ปีที่แล้ว

      @@printlnPrints can you send me an example? Please

  • @osekagorska2330
    @osekagorska2330 2 ปีที่แล้ว +1

    I have problem with adding my own model. I made it 16x16 same as you did but It just doesn't want to load.

    • @lvdv4j
      @lvdv4j 2 ปีที่แล้ว

      Are you sure you added the getPlayerImage(); method to the player constructor in the Player class? I had the same problem and this fixed it

  • @cseager3239
    @cseager3239 2 หลายเดือนก่อน

    I was working with vscode and when i downloaded intellij and copy and pasted all my vscode onto there after mimicing RyiSnow's file structure exactly i got it to work. Im gonna try to push it to github so that i can pull it back onto a vscode editor. I guess my compiler is just limited in what it can do.

  • @carrot_15
    @carrot_15 24 วันที่ผ่านมา +1

    the image don't show up 😭

    • @FoxTop-bs5sr
      @FoxTop-bs5sr 11 วันที่ผ่านมา +1

      Try to create Source Folder res, add Package player, and writte path "/player/(sprite_name)"

    • @carrot_15
      @carrot_15 11 วันที่ผ่านมา +1

      @ ok I will try that, thanks 😊

    • @carrot_15
      @carrot_15 11 วันที่ผ่านมา

      @@FoxTop-bs5sr i tried it but it still won't show up. i even watched the intellij video

  • @ErkeTan879
    @ErkeTan879 5 หลายเดือนก่อน

    Help! when pressed, the player does not move at all

  • @lonelylad9818
    @lonelylad9818 8 หลายเดือนก่อน

    How would I make it so that my character can walk diagonally like if I press W and D at the same time?

  • @mehmetdrmz2210
    @mehmetdrmz2210 ปีที่แล้ว

    I keep getting an error even though the file path and name are correct plase help me :)

  • @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.

  • @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?

  • @mikemazanetz4183
    @mikemazanetz4183 10 หลายเดือนก่อน

    thank you very much for these wonderful video tutorials. An excellent resource !

  • @teodorairina0404
    @teodorairina0404 2 ปีที่แล้ว +1

    You are doing a great job ! I will tell my friends about you !!!

  • @stvideoprjcts101
    @stvideoprjcts101 6 หลายเดือนก่อน

    hello, Thank you for this video, it is very helpful. However, I would like to ask why is there no visible image is showing on my window since I added the sprites, I check everything and I am confused. I used eclipse, thank you for your help!

    • @stvideoprjcts101
      @stvideoprjcts101 6 หลายเดือนก่อน

      i fixed it now, thank you still! consider this as appreciation post. hehe

  • @s1moni336
    @s1moni336 ปีที่แล้ว

    isn't it better just to make a walk class? so we can use it for other characters too?

  • @jackzytt
    @jackzytt 6 หลายเดือนก่อน

    I have a question, why is the character way smaller for me?

  • @bluestreak711
    @bluestreak711 2 ปีที่แล้ว +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 ปีที่แล้ว +1

      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.

  • @God5147
    @God5147 ปีที่แล้ว

    Excuse me my machine can't appear left sides "left1" and "left2". IDE is Eclipse and my machine is Mac.

  • @aleksandrajovanovic2631
    @aleksandrajovanovic2631 ปีที่แล้ว

    can someone pls help me my square was moving but now it wont i followed every step