as soon as i try to move my character i get this error messaage: Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null at pptrialgame2/main.CollisionChecker.checkObject(CollisionChecker.java:98) at pptrialgame2/entity.Player.update(Player.java:83) at pptrialgame2/main.GamePanel.update(GamePanel.java:96) at pptrialgame2/main.GamePanel.run(GamePanel.java:83) at java.base/java.lang.Thread.run(Thread.java:833)
The message indicates the program is trying to read an empty object. Did you instantiate objects in the AssetSetter? Also, did you add the condition "if(gp.obj[i] != null)" when you check object collision?
Hi Ryi I really appreciate the time and effort you put into these tutorials as nobody is doing this for tile games in any language let alone java with the level of precision, stylistic elements and conceptual information that you provide. There is only one other programmer a C++ guy called Javid but he is a PhD and has built a game engine etc. Anyways I'm happy to see programmers who are passionate about Java and promoting game development in the language which I think it doesn't get enough credit especially when you consider that some of the greatest games were built in Java. I look forward to playing your game when it is released on Steam. Keep up the good work.
Firstly, absolutely love this tutorial, easily one of the best Java tutorials out there, really appreciate that you take the time to explain many concepts. I'm at the part where we are testing for object collision, and for some reason I'm not getting the console text output over objects like the key, but rather on a blank vertical line of grass near the second door! Any ideas where I'm going wrong?
Glad you liked the video. That sounds like collision is triggered at a wrong spot. Probably something is wrong in checkObject method. Can I see your CollisionChecker class? Then maybe I can find out why. You can paste your code here.
@@RyiSnow I did post the code however I spotted the error (after days of searching for it!), in the checkObject method, I had put entity.solidArea.y = entity.worldX + entity.solidArea.y; , and that reference to worldX should've been worldY. Feels so good when you fix errors like that.
To summarize the data flow for all the new classes, methods, etc, in the last couple videos: -Create a SuperObject class which sets up the default rectangle hitbox the items use and implements the draw() method that determines where the item is in on the world map in relation to the player and whether it's in view of the camera before sending it to be drawn. -Create individual item classes (Key, Door, etc.) which load images and can set collision properties that are unique to each item -Create an AssetSetter class that uses the setObject() method to determine what type of item will appear on the world map and where it will appear. These objects and their position are held in an array of SuperObject classes that is instantiated GamePanel. -Create a setupGame() method in GamePanel class which calls the setObject() method from AssetSetter class. We call the setupGame() method from Main so that it places the items on the world map before calling the startGameThread() method in GamePanel. -GamePanel class calls the draw() method from SuperClass so the items are drawn above the world map and below the player sprite in its paintComponent() method. It calls the draw() method in SuperClass using a for loop so that all the different items in the obj array are drawn on the world map in their unique place. -The checkObject() method in the CollisionChecker class is called from within the update() method in the Player class. It determines whether the player hitbox collides with the hitbox of an item on the map using the intersects() method from the Rectangle class. If returns an index value for the item collided with which is used for further processing. -The pickupObject() method is called later in the update() method of the Player class. It is passed the index value returned from the checkObject() method. In the code in this video, we use the index value to determine when the player picks up keys and if they have a key to open doors.
I have done something different but pretty cool with the doors rather than setting it to null I added in another bool value + an image the boolValue is needs key and the image is open door basically if the needsKey value is set to true when the door is collided with then it goes to see if the player has a key and then if the player does it sets the collision to false and the image to the open door
good! you do not need to add or subtract speed in order to determine the player coordinates in switch, because they are already defined in player class.
I think at 15:18 you can check if the entity is a player without passing the boolean parameter to the 'checkObject' function. You could have simply done: if(entity instanceof Player) { index = i; }
My object collision doesn't print the message “up, down… collision!”. I am trying to get to 14:15 in the video, can you help me? I usually never make comments and I've been able to solve all the problems by myself until now. I have tried so many things like re-watching this video and the previous one, and I re-checked my work around 5-6 different times. Also: Ryu if you are actually reading this, thank you! This series is SO awesome and helpful! I'm sorry I had to bring up my problem, but that aside I will try to keep following along :)
// CHECK OBJECT COLLISION int objIndex = gp.cChecker.checkObject(this, true); pickUpObject(objIndex);
// IF COLLISION IS FALSE, PLAYER CAN MOVE if(collisionOn == false) { switch(direction) { case "up":worldY -= speed;break; case "down":worldY += speed;break; case "left":worldX -= speed;break; case "right":worldX += speed;break; } }
SuperObject: __________________________________________________________________________________________________________________ package object; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.image.BufferedImage; import main.GamePanel; public class SuperObject { public BufferedImage image; public String name; public boolean collision = false; public int worldX, worldY; public Rectangle solidArea = new Rectangle(0, 0, 48, 48); public int solidAreaDefaultX = 0; public int solidAreaDefaultY = 0;
public void draw(Graphics2D g2, GamePanel gp) {
int screenX = worldX - gp.player.worldX + gp.player.screenX; int screenY = worldY - gp.player.worldY + gp.player.screenY;
@@RyiSnow The code wasn't working in the comments for some reason, so here's a link to the google doc with the info (hopefully this works): docs.google.com/document/d/10LaUFJ9s7t6COYnin0psqkPZNIExLjU2jELysEOfAT4/edit?usp=sharing
Hey great video ! I use your video to great my own "game engine" so I can reuse the code to make another game quickly. I will love to see a tutorial on particule and window management (like full screen, resize window, etc).
Cool! Yeah, separating a part of your code and stock them as your own tools/libraries is a great idea (especially when you repeat the same thing in every project) and it often makes your work a lot easier. I found creating tools is as fun as making games (I realized that when I made my 2D tile editor). I hope your engine will turn out good!
@@Gabriel-mh7ek hey! So does this tool allow you to make a map using your png’s (like a normal tile editor), then get it in text format? If so, how did you do it because I’m looking for something like that :D
Hello! I’m Absoutly loving this tutorial series I recently found from your channel. I’m super curious to know if there’s a way to make it so that when you pickup the boots and increase player speed to make it only last for 5-10 seconds. I tried to make a separate integer as a “speed counter” and imitated it in the player class to “speed counter ++” Then an if statement to return the player speed after some time. But I’m having trouble forging this out. If you still monitor these comments I would love some help!
@@RyiSnow hey Ryi, thanks so much for replying. here is the string of code that I'm having issues with case "Speed_Potion": speed += 2; gp.obj[i] = null; //-----------------------------------------10 seconds of speed +2------------------------------ SpeedCounter ++; if(SpeedCounter == 20){ System.out.println("speed counter"+ SpeedCounter); speed = 3; SpeedCounter = 0; } //--------------------------------------------returns base speed------------------------------ break; } } }
Oooh, amazing tutorial, ofc we could implement some OOP patterns like state, builder etc. but i will do it in my own projects. Maybe in future parts you do, but i didnt get there yet. I spend last half year on lerning java and thats amazing how this all puzzles fits together. Do you guys also fight with debugging? In here insted of index = i; in checkObject method i wrote return i; It took me 2h to figureout how to fix it. Whats amazing is that I take 10min break and then I fix it instantly.
Whenever i try to move, i get this error message:Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 68 out of bounds for length 50 at CollisionChecker.CheckTile(CollisionChecker.java:39) at Player.update(Player.java:73) at GamePanel.update(GamePanel.java:117) at GamePanel.run(GamePanel.java:99) at java.base/java.lang.Thread.run(Thread.java:1583)
@RyiSnow I recently have been following the tutorial I think I’m having a logic error because it is not showing the error, but the code runs and when I go to the doors it prints 0 keys but I can’t pick up keys do you know how to fix this or would you want me to send the code
I figured out the error if anyone has the same issue: in the pickUpObject method I wrote my case “key”: with a lower case letter and it should have been with a capital like this case “Key”:
Been a long while, but by any chance did you find a fix for this? Fixed it! The speed modifiers were being called after the if statement in the switch.
Hey i have an issue, when i try to run my code, my door collides if i don't pick up any key, but then even if i pick up a key i still can't pass through the door. And when i pick up all three all my doors disappear before i get to it
Hi Ryi I've followed up to this far however I don't know why my player is unable to interact with keys as well as the doors not performing any collision what do you think I.ve done something wrong or did i forget something or maybe made a mistake in a class the only thing I see that I'm is a scope above of the return command.
@@RyiSnow know i´m making some panel to have inventory in the top of the game, But also to see how many points you got, now you can only see it in the terminal.
hey!! thank you for this tutorial . i am having a problem in the output , my door is not being solid and my character can easily pass through it even when it has no keys
@RyiSnow help i tried to help to find my error but cant fix by my own , so plz help me out . my full is perfectly work but my player could not available to receive key object . can u tell me the error ????
13:47 You obviously know what youre talking about, but you often say "parse this Player class" which is technically wrong. We only parse the pointer of the current instance of the class. Could be misleading for people who dont know the difference.
there's something wrong with my map. when i move my player downward there are only some tiles and then black background starts showing moreover my pickup object method isnt working. i have checked it several times but i still cant figure out
hello, thank you for this tutorial! when you are checking the first result i do not get the message "up/down/etc collision!" when touching key or door, and my door isn't solid either. Do you know what happened? please let me know if i should paste some code here
the only things i changed were what intellij suggested to me ("enhanced switch statements") like this: switch(entity.direction) { case "up": entity.solidArea.y -= entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("up collision!"); } break; case "down": entity.solidArea.y += entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("down collision!"); } break; case "left": entity.solidArea.x -= entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("left collision!"); } break; case "right": entity.solidArea.x += entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("right collision!"); } break; } to switch (entity.direction) { case "up" -> { entity.solidArea.y -= entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("up collision!"); } } case "down" -> { entity.solidArea.y += entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("down collision!"); } } case "left" -> { entity.solidArea.x -= entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("left collision!"); } } case "right" -> { entity.solidArea.x += entity.speed; if (entity.solidArea.intersects(gp.obj[i].solidArea)) { System.out.println("right collision!"); } } } and i didnt think things like that could impact the games functioning (switching it back didnt change anything either, still no "up collision!").
now that i went through the video again, i realized i was missing an int counter2 = 0, maybe that is what went wrong? i went through your last video as well and cannot find where you put the int counter2, could you please tell me what video it is in? sorry for the trouble, thank you so much again for this tutorial, it is very clear and simple to follow through.
Don't worry about the counter2. That is for setting the player character in standstill position after he stopped moving. I doubt it's related with your issue (check part 10+ video if you are curious). Could you paste your CollisionChecker class here? Then I will take a look.
I noticed that too, I think it's written like this because the checkTile method contains direction specific information in the if checks, but checkObject doesn't have that, so it's easier to just check after the switch ends. It's a small difference, but it does add to readability.
bro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxed
Hi. Thanks for the tutoriales, I have a problem, My door isn't Solid, I can walk over it, I don't know why the code is the same, any idea for why it's happening that?
@RyiSnow im sorry for pining you but.. IM COMPLETLY STUCK HERE! whenever i load the game i walk through the doors... i can pick up keys and open doors but when i dont have keys i just walk through and the doors dont disappear Update 2: i restarted my work from scratch and i did it alL by hand without the help of chatgpt and my current problem is that keys are not being currently displayed at first it was working but there was one key only being displayed but now there are none once i tried moving the code somewhere else.. What do you think the problem is? the problem is in player class picKUpObjects and i dont know what to do so i could fix this code
You can make a separate class for this navigate to that class first then navigate to the game (Main class) from the home screen on PLAY button clicked ;). Hope this opens your mind I'm developing this game for Android currently, a little harder but I know sort of what I'm doing :)
If you're interested, this is a fireball demo that I recently posted on my twitter. The fireball was created as Entity. twitter.com/RyiSnow2017/status/1484146787980308480
@@RyiSnow Hi RyiSnow, Can I ask, the way you create bullets is, first of all, create a child class called bullet which extends the entity class ? Then I also need to call the bullet class on the player and gamepanel class ? I just need a direction and some ideas so I can work on it. I know that the bullet class needs X and Y axis, so I will be using worldX and WorldY. for the direction, I will be using the player direction
Hi Ryi, I need help I able to detect and make the object going null , I check using System.out.println(gp.obj[i]); gp.obj[i] = null; System.out.println(gp.obj[i]); but the image keep there not gone, what do you think Should I check?
ahh my bad :D I typo if(player == true) { index = 1; } it should be if(player == true) { index = i; } that's why only object number one that always disappear XD
I'm having a small issue when checking the object collision. After I add the line: { int objIndex = gp.cChecker.checkObject(this, true); } into the update method inside of the Player class, all of my assets (chests, keys, and doors) disappear from the game. Omitting this line makes them reappear, but I don't see that as fixing the problem lol. I am getting a warning about the objIndex stating that its value is never used, only option it gives to remedy is to remove it all together. and again, I don't see that as a solution. I'm going to continue trying to figure this out. If anyone has any input or ideas of where I so obviously made a mistake, that would be much appreciated. 😁 I will update with my solution when it comes to me. So that took 5 minutes. I mistakenly used the = instead of the + when setting the positions of gp.obj[i]. I really need to pay a little more attention to detail!
HEELLLLPPP :) Hey so this is amazing, I even had to make my own Rectangle class for this method to work... Curious though, when my player interacts with the door for example if the player faces up or down when moving the collision is detected and the player is able to walk away perfectly fine. BUTTT when the door is placed on a horizontal plain so x axis the player gets stuck on the door and walks overtop. I'll keep looking at the code but I'm not seeing where or why this occurs.
Hey im having issues with my code not placing more than 1 object, for instance, I want to draw the door and the key but It only draws number 0 in the array (in this case only the door is being drawn)
Try adding this line after g2.drawImage in the SuperObject: System.out.println(name + " is displayed"); And check if "Key is displayed" is printed out on the console when the key is visible on the screen. If the text is printed out, the object is rendered but somehow the image is invisible so maybe something is wrong with your image. If the text is not printed out, the program is not drawing the key object so maybe something is wrong with your object array or the for loop to draw them.
@@RyiSnow I made a bunch of other assets in the array and no matter how much I add, only the number 0 in the array is ever shown no matter the image/object. It says in the terminal that all of them are displaying but its not showing on the screen.
i implement the diagonal movement, but yeah im stuck because when the player was interact with object that have collision value true the player just stuck, i think its because the for loop while getting the object. i hope i found the solution asap
can anyone help? I am at the end, I can get the player to pick up the key, key increases by one, but then I go down to the door and it does nothing it just stays there.. I have all the same code so I don't know why it's doing this...
I just played with this today, you'd want a redraw method after a set time for the object or objects you want to redraw and place them in maybe .random locations on the screen for the player to collect at any time :).. I haven't tested the random placement part yet but you'd want a void to make sure the item doesn't get placed in the boarder for example!!
If u want to use a better way of returning a non collision object u rather use -1 instead of 999. Its just a better practise, even though you prolly wont need it.
Hello and your videos are awesome anyways i have a problem with the two up map keys the game stops and the error says about the key that goes to null because they are already null for some reason (The Down key works correctly)
RyiSnow, I keep getting this error when I try to run my movement, I get a null pointer error Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null at main.CollisionChecker.checkObject(CollisionChecker.java:74) at entity.Player.update(Player.java:88) at main.GamePanel.update(GamePanel.java:98) at main.GamePanel.run(GamePanel.java:82) at java.base/java.lang.Thread.run(Thread.java:833)
entity.solidArea.x = entity.solidAreaDefaultX; entity.solidArea.y = entity.solidAreaDefaultY; gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX; gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY; These need to be inside of if (gp.obj[i] != null) Probably you've placed outside of it. move the 4 lines to fix it.
for some reason, even though I defined the i variable as an int it still tells me I need to make it an integer, and i know this video is years old, but thanks for the videos in general!
Update! in one of the next 1-2 videos, it will deleted^^ :P until now, the GamePanel's properties public final int worldWidth = tileSize * maxWorldCol; and public final int worldHeight = tileSize * maxWorldRow; were not used,.. Is there something I have overseen, or will this come yet ^^' :P ..but I will see anyways :D this is so much fun to learn, and to create individually
Why am I getting this when I tried to move my character? Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 168 out of bounds for length 50 at my2dgame.CollisionChecker.checkTile(CollisionChecker.java:54) at entity.Player.update(Player.java:86) at my2dgame.GamePanel.update(GamePanel.java:101) at my2dgame.GamePanel.run(GamePanel.java:76) at java.base/java.lang.Thread.run(Thread.java:831) I can't seem to fix it. can anybody help?????
Looks like one of the index (col or row) for the mapTileNum is getting a number which is beyond the array's boundary (50). That is not supposed to happen so I think some calculation is not correct.
@@apro8723 Don't know if this is relevant anymore or not, but I think it might be because of the statement: entity.solidArea.x = entity.worldX + entity.solidArea.x inside of the checkObject()-method in CollisionChecker. If you don't reset the value of the rectangles values like he does in 12:20 it will make the rectangle bigger than the map at some point - which would then give you an error since the array's boundary is only as big as the world (thus you get an array out of bounds exception).
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null at Main.CollisionDetection.checkObject(CollisionDetection.java:79) at entity.Player.update(Player.java:89) at Main.GamePanel.update(GamePanel.java:97) at Main.GamePanel.run(GamePanel.java:72) at java.base/java.lang.Thread.run(Thread.java:831)
Did you instantiate the object? Also, did you add the line "if(gp.obj[i] != null)"? NullPointerException error occurs when program tries to refer to an instance but it was empty (null).
Can you please help me, I don't understand why it's not working. I got this message : Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "name" because "this.gp.obj[i]" is null at entity.Player.pickUpObject(Player.java:144) at entity.Player.update(Player.java:106) at main.gamePanel.update(gamePanel.java:108) at main.gamePanel.run(gamePanel.java:89) at java.base/java.lang.Thread.run(Thread.java:1623)
Hey RyiSnow!! I love your videos so much. For some reason im getting an error on " gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX; gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY;" Anything im doing wrong?
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 184 out of bounds for length 50 at main.CollisionChecker.checkTile(CollisionChecker.java:36) at entity.Player.update(Player.java:93) at main.GamePanel.update(GamePanel.java:150) at main.GamePanel.run(GamePanel.java:137) at java.base/java.lang.Thread.run(Thread.java:831) how can I fix this?
-I managed to fix a similar error by removing the lines "entity.solidArea.y -= entity.speed" etc. in the "checkObject" method's switch statement. I'm not sure if that'll cause more problems down the line or not, though.- Update: I believe this actually did cause a problem where I would become unable to move when touching any collidable object (except doors I had the key to). I ended up restarting this program from scratch, and now I'm not getting the error. I'm still not sure what caused the error in the first place; I would recommend just rewriting the method from scratch, and if that doesn't work, rewriting the class from scratch.
Hey, great video. Im having a problem, and I need some help. All my objects are being placed at the top of the map when I add this line "int objindex = gp.cChecker.checkObject(this, true);" in the player class. What could it be ? I could send you my code if needed.
help i'm getting an error and i can't move it after Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "name" because "this.gp.obj[i]" is null at entity.Player.pickUpObject(Player.java:117) at entity.Player.update(Player.java:81) at main.GamePanel.update(GamePanel.java:88) at main.GamePanel.run(GamePanel.java:74) at java.base/java.lang.Thread.run(Thread.java:1583)
Hello, I tried your method the collision of object and the player is perfectly fine and return the right object index however when I set null value in obj[] from pickUpObject method I got this error Exception in thread "Thread-4" java.lang.NullPointerException at main.Collision.checkObject(Collision.java:138) at entity.Player.updatePos(Player.java:163) at entity.Player.update(Player.java:84) at main.GameLoop.update(GameLoop.java:39) at main.GameLoop.run(GameLoop.java:66) at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null at main.CollisionChecker.checkObject(CollisionChecker.java:116) at entity.Player.update(Player.java:107) at main.GamePanel.update(GamePanel.java:155) at main.GamePanel.run(GamePanel.java:137) at java.base/java.lang.Thread.run(Thread.java:833)
entity.solidArea.x = entity.solidAreaDefaultX; entity.solidArea.y = entity.solidAreaDefaultY; gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX; gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY; These need to be inside of if (gp.obj[i] != null) Probably you've placed outside of it. move the 4 lines to fix it.
i need help because i have this error meesage and i am not able to move the charackter Exception in thread "Thread-0" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "entity.solidAreaDefaultY" is null at main.CollisionChecker.checkObjekt(CollisionChecker.java:108) at entity.Player.update(Player.java:100) at main.GamePanel.update(GamePanel.java:150) at main.GamePanel.run(GamePanel.java:137) at java.base/java.lang.Thread.run(Thread.java:831)
I place this highe : entity.solidArea.x = entity.solidAreaDefaultX; entity.solidArea.y = entity.solidAreaDefaultY; gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX; gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY; and optain this error message: Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 184 out of bounds for length 50 at main.CollisionChecker.checkTile(CollisionChecker.java:28) at entity.Player.update(Player.java:93) at main.GamePanel.update(GamePanel.java:150) at main.GamePanel.run(GamePanel.java:137) at java.base/java.lang.Thread.run(Thread.java:831)
Hello, and huge thanks for your amazing tutorials Ryi! I'm encountering an error the moment my character touches the Key Object (I've added what's in each line to the error message below): Exception in thread "Thread-0" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "" is null at entity.Player.pickUpObject(Player.java:123)
@@RyiSnow I did, trust me! It didn't help and I'm getting a different sort of error: "Cannot invoke "String.hashCode()" because "" is null" I've tried moving brackets, I've gone through this video and the previous one to try and see where I go wrong but I simply cannot find what I'm doing wrong D:
For anyone that has this error: Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at main.CollisionChecker.checkObject(CollisionChecker.java:67) at entity.Player.update(Player.java:86) at main.GamePanel.update(GamePanel.java:118) at main.GamePanel.run(GamePanel.java:103) at java.base/java.lang.Thread.run(Thread.java:1570) try checking your i loop in checkObject() and see if you mispelled your "i" as a "1".
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null at Bohol/My2Dgame.CollisionChecker.checkObject(CollisionChecker.java:111) at Bohol/entity.Player.update(Player.java:92) at Bohol/My2Dgame.GamePanel.update(GamePanel.java:130) at Bohol/My2Dgame.GamePanel.run(GamePanel.java:115) at java.base/java.lang.Thread.run(Thread.java:833) can u help me this this why it's not working? these are seem to be the problem
Hmm, when you are setting your objects' image, do you have the correct file path? I think it should be /objects/key.png" for instance. What IDE are you using? In some IDE's like IntelliJ that I'm using, you have to right click the res folder and Mark directory as - Resources Root. As he explained in an earlier video, / at the beginning means it goes from root, meaning the project folder, into res, and then it checks the folders from there.
excuse me i need help i'm getting an error message and im not able to move my character anymore
as soon as i try to move my character i get this error messaage:
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null
at pptrialgame2/main.CollisionChecker.checkObject(CollisionChecker.java:98)
at pptrialgame2/entity.Player.update(Player.java:83)
at pptrialgame2/main.GamePanel.update(GamePanel.java:96)
at pptrialgame2/main.GamePanel.run(GamePanel.java:83)
at java.base/java.lang.Thread.run(Thread.java:833)
The message indicates the program is trying to read an empty object. Did you instantiate objects in the AssetSetter? Also, did you add the condition "if(gp.obj[i] != null)" when you check object collision?
@@RyiSnow i think i did... can i send my code to you through mail? im sorry im new to coding so im making a lot of mistakes TT^TT
Did you check if object is null or not when you draw objects in the paintComponent method?
@@RyiSnow yuppp it is null
Bro’s helping random people he doesn’t know in the comment section, truly a legend
Hi Ryi I really appreciate the time and effort you put into these tutorials as nobody is doing this for tile games in any language let alone java with the level of precision, stylistic elements and conceptual information that you provide. There is only one other programmer a C++ guy called Javid but he is a PhD and has built a game engine etc. Anyways I'm happy to see programmers who are passionate about Java and promoting game development in the language which I think it doesn't get enough credit especially when you consider that some of the greatest games were built in Java. I look forward to playing your game when it is released on Steam. Keep up the good work.
Yeah it's a bit sad that not many people make games from scratch any more. In any case, I'm enjoying this and good to know that you are as well.
I always have to laugh at your reaction when you misspell things! I always see it beforehand and anticipate for you to realize :D
Thank all that is holy that this man decided to use a dark theme... I'd say 95% of tutorials on YT have eyeball-burning light theme as the default.
You are not horrible, you are awesome!
reached episode 8, i am really enjoying . Lets goo
Firstly, absolutely love this tutorial, easily one of the best Java tutorials out there, really appreciate that you take the time to explain many concepts. I'm at the part where we are testing for object collision, and for some reason I'm not getting the console text output over objects like the key, but rather on a blank vertical line of grass near the second door! Any ideas where I'm going wrong?
Glad you liked the video. That sounds like collision is triggered at a wrong spot. Probably something is wrong in checkObject method. Can I see your CollisionChecker class? Then maybe I can find out why. You can paste your code here.
@@RyiSnow I did post the code however I spotted the error (after days of searching for it!), in the checkObject method, I had put entity.solidArea.y = entity.worldX + entity.solidArea.y; , and that reference to worldX should've been worldY. Feels so good when you fix errors like that.
@@WarrenBainesMusic You mean you posted the code but got deleted? It happens from time to time. Anyways, good to know it was solved!
great job again! really appreciate these videos to go back to while i work
To summarize the data flow for all the new classes, methods, etc, in the last couple videos:
-Create a SuperObject class which sets up the default rectangle hitbox the items use and implements the draw() method that determines where the item is in on the world map in relation to the player and whether it's in view of the camera before sending it to be drawn.
-Create individual item classes (Key, Door, etc.) which load images and can set collision properties that are unique to each item
-Create an AssetSetter class that uses the setObject() method to determine what type of item will appear on the world map and where it will appear. These objects and their position are held in an array of SuperObject classes that is instantiated GamePanel.
-Create a setupGame() method in GamePanel class which calls the setObject() method from AssetSetter class. We call the setupGame() method from Main so that it places the items on the world map before calling the startGameThread() method in GamePanel.
-GamePanel class calls the draw() method from SuperClass so the items are drawn above the world map and below the player sprite in its paintComponent() method. It calls the draw() method in SuperClass using a for loop so that all the different items in the obj array are drawn on the world map in their unique place.
-The checkObject() method in the CollisionChecker class is called from within the update() method in the Player class. It determines whether the player hitbox collides with the hitbox of an item on the map using the intersects() method from the Rectangle class. If returns an index value for the item collided with which is used for further processing.
-The pickupObject() method is called later in the update() method of the Player class. It is passed the index value returned from the checkObject() method. In the code in this video, we use the index value to determine when the player picks up keys and if they have a key to open doors.
Great
I hope to see a video of your enemy soon
I have done something different but pretty cool with the doors rather than setting it to null I added in another bool value + an image the boolValue is needs key and the image is open door basically if the needsKey value is set to true when the door is collided with then it goes to see if the player has a key and then if the player does it sets the collision to false and the image to the open door
Could you please show me your code on how you did this. I am trying to implement it myself but I am having trouble.
Hello, i have a question, when my player touches to far to a structure its glitched and gets stuck and cant leave. any ideas let me know.
you're legend in doing tutorials!
This is awesome! I'm can only pickup the bottom key atm but I'll make it work
I'm really enjoying your tutorials!
Thank you as always for these videos. I really appreciate it :)
thanks for all videos, learning is ertertaining and informative :) You are legend.
good! you do not need to add or subtract speed in order to determine the player coordinates in switch, because they are already defined in player class.
I think at 15:18 you can check if the entity is a player without passing the boolean parameter to the 'checkObject' function. You could have simply done:
if(entity instanceof Player)
{
index = i;
}
thanks, I had a bug and I used your code and it worked 🥰
just cmt to beat RUclips's algorithm :))). Keep going!!!
Excuse me, how could I implement the tile collision system on objects?
My object collision doesn't print the message “up, down… collision!”. I am trying to get to 14:15 in the video, can you help me?
I usually never make comments and I've been able to solve all the problems by myself until now. I have tried so many things like re-watching this video and the previous one, and I re-checked my work around 5-6 different times.
Also: Ryu if you are actually reading this, thank you! This series is SO awesome and helpful! I'm sorry I had to bring up my problem, but that aside I will try to keep following along :)
No worries. Can I see your SuperObject, Player and CollisionChecker class? You can paste them here.
Player:
_____________________________________________________________________________________________
package entity;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Color;
import main.GamePanel;
import main.KeyHandler;
public class Player extends Entity{
GamePanel gp;
KeyHandler keyH;
public final int screenX;
public final int screenY;
int hasKey = 0;
public Player(GamePanel gp, KeyHandler keyH) {
this.gp = gp;
this.keyH = keyH;
screenX = gp.screenWidth/2 - (gp.tileSize/2);
screenY = gp.screenHeight/2 - (gp.tileSize/2);
solidArea = new Rectangle();
solidArea.x = 8;
solidArea.y = 16;
solidAreaDefaultX = solidArea.x;
solidAreaDefaultY = solidArea.y;
solidArea.width = 32;
solidArea.height = 30;
setDefaultValues();
getPlayerImage();
}
public void setDefaultValues() {
worldX = gp.tileSize * 23;
worldY = gp.tileSize * 21;
speed = 4;
// speed = gp.worldWidth/600;
direction = "down";
}
public void getPlayerImage() {
try {
up1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_1.png"));
up2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_2.png"));
down1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_1.png"));
down2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_2.png"));
left1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_1.png"));
left2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_2.png"));
right1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right_1.png"));
right2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right_2.png"));
}catch(IOException e) {
e.printStackTrace();
}
}
public void update() {
if(keyH.upPressed == true || keyH.downPressed == true ||
keyH.leftPressed == true || keyH.rightPressed == true) {
if (keyH.upPressed == true) {
direction = "up";
}
else if(keyH.downPressed == true) {
direction = "down";
}
else if (keyH.leftPressed == true) {
direction = "left";
}
else if (keyH.rightPressed == true) {
direction = "right";
}
// CHECK TILE COLLISION
collisionOn = false;
gp.cChecker.checkTile(this);
// CHECK OBJECT COLLISION
int objIndex = gp.cChecker.checkObject(this, true);
pickUpObject(objIndex);
// IF COLLISION IS FALSE, PLAYER CAN MOVE
if(collisionOn == false) {
switch(direction) {
case "up":worldY -= speed;break;
case "down":worldY += speed;break;
case "left":worldX -= speed;break;
case "right":worldX += speed;break;
}
}
spriteCounter++;
if(spriteCounter > 12) {
if(spriteNum == 1) {
spriteNum = 2;
}
else if(spriteNum == 2) {
spriteNum = 1;
}
spriteCounter = 0;
}
}
}
public void pickUpObject(int i) {
if(i != 999) {
String objectName = gp.obj[i].name;
switch(objectName) {
case "Key":
gp.playSE(1);
hasKey++;
gp.obj[i] = null;
System.out.println("Has:" + hasKey);
break;
case "Door":
if(hasKey > 0) {
gp.playSE(3);
gp.obj[i] = null;
hasKey--;
}
System.out.println("Key: " + hasKey);
break;
case "Boots":
gp.playSE(2);
speed += 1;
gp.obj[i] = null;
break;
}
}
}
public void draw(Graphics2D g2) {
// g2.setColor(Color.white);
// g2.fillRect(x, y, gp.tileSize, gp.tileSize);
BufferedImage image = null;
switch(direction) {
case "up":
if (spriteNum == 1) {
image = up1;
}
if (spriteNum == 2) {
image = up2;
}
break;
case "down":
if (spriteNum == 1) {
image = down1;
}
if (spriteNum == 2) {
image = down2;
}
break;
case "left":
if (spriteNum == 1) {
image = left1;
}
if (spriteNum == 2) {
image = left2;
}
break;
case "right":
if (spriteNum == 1) {
image = right1;
}
if (spriteNum == 2) {
image = right2;
}
break;
}
g2.drawImage(image, screenX, screenY, gp.tileSize, gp.tileSize, null);
}
}
_____________________________________________________________________________________________
SuperObject:
__________________________________________________________________________________________________________________
package object;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import main.GamePanel;
public class SuperObject {
public BufferedImage image;
public String name;
public boolean collision = false;
public int worldX, worldY;
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
public int solidAreaDefaultX = 0;
public int solidAreaDefaultY = 0;
public void draw(Graphics2D g2, GamePanel gp) {
int screenX = worldX - gp.player.worldX + gp.player.screenX;
int screenY = worldY - gp.player.worldY + gp.player.screenY;
if (worldX + gp.tileSize > gp.player.worldX - gp.player.screenX &&
worldX - gp.tileSize < gp.player.worldX + gp.player.screenX &&
worldY + gp.tileSize> gp.player.worldY - gp.player.screenY &&
worldY - gp.tileSize< gp.player.worldY + gp.player.screenY) {
g2.drawImage(image, screenX, screenY, gp.tileSize, gp.tileSize, null);
}
}
}
__________________________________________________________________________________________________________________
Can you provide CollisionChecker too?
@@RyiSnow The code wasn't working in the comments for some reason, so here's a link to the google doc with the info (hopefully this works):
docs.google.com/document/d/10LaUFJ9s7t6COYnin0psqkPZNIExLjU2jELysEOfAT4/edit?usp=sharing
Hey great video ! I use your video to great my own "game engine" so I can reuse the code to make another game quickly. I will love to see a tutorial on particule and window management (like full screen, resize window, etc).
Cool! Yeah, separating a part of your code and stock them as your own tools/libraries is a great idea (especially when you repeat the same thing in every project) and it often makes your work a lot easier. I found creating tools is as fun as making games (I realized that when I made my 2D tile editor). I hope your engine will turn out good!
@@Gabriel-mh7ek hey! So does this tool allow you to make a map using your png’s (like a normal tile editor), then get it in text format? If so, how did you do it because I’m looking for something like that :D
Hello! I’m Absoutly loving this tutorial series I recently found from your channel.
I’m super curious to know if there’s a way to make it so that when you pickup the boots and increase player speed to make it only last for 5-10 seconds.
I tried to make a separate integer as a “speed counter” and imitated it in the player class to “speed counter ++”
Then an if statement to return the player speed after some time. But I’m having trouble forging this out. If you still monitor these comments I would love some help!
I think you're on the right track! Make sure to record the original speed so you can restore it when the speed up time is up!
@@RyiSnow hey Ryi, thanks so much for replying. here is the string of code that I'm having issues with
case "Speed_Potion":
speed += 2;
gp.obj[i] = null;
//-----------------------------------------10 seconds of speed +2------------------------------
SpeedCounter ++;
if(SpeedCounter == 20){
System.out.println("speed counter"+ SpeedCounter);
speed = 3;
SpeedCounter = 0;
}
//--------------------------------------------returns base speed------------------------------
break;
}
}
}
Oooh, amazing tutorial, ofc we could implement some OOP patterns like state, builder etc. but i will do it in my own projects. Maybe in future parts you do, but i didnt get there yet.
I spend last half year on lerning java and thats amazing how this all puzzles fits together.
Do you guys also fight with debugging? In here insted of index = i; in checkObject method i wrote return i;
It took me 2h to figureout how to fix it. Whats amazing is that I take 10min break and then I fix it instantly.
Thanks you so much for this video!
Whenever i try to move, i get this error message:Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 68 out of bounds for length 50
at CollisionChecker.CheckTile(CollisionChecker.java:39)
at Player.update(Player.java:73)
at GamePanel.update(GamePanel.java:117)
at GamePanel.run(GamePanel.java:99)
at java.base/java.lang.Thread.run(Thread.java:1583)
for some reason randomly when i collected an object it gave me some out of range error and idk what to do
Hi Ryi, I really enjoy your content, but I need help with this part. My checkObject function doesn't seem to work. Could you please help me?
@RyiSnow I recently have been following the tutorial I think I’m having a logic error because it is not showing the error, but the code runs and when I go to the doors it prints 0 keys but I can’t pick up keys do you know how to fix this or would you want me to send the code
I figured out the error if anyone has the same issue: in the pickUpObject method I wrote my case “key”: with a lower case letter and it should have been with a capital like this case “Key”:
ik I'm late, but my character is getting stuck in the door when you don't have a key. Any help?
Been a long while, but by any chance did you find a fix for this?
Fixed it! The speed modifiers were being called after the if statement in the switch.
Hey i have an issue, when i try to run my code, my door collides if i don't pick up any key, but then even if i pick up a key i still can't pass through the door.
And when i pick up all three all my doors disappear before i get to it
Awesome series!
Hi Ryi I've followed up to this far however I don't know why my player is unable to interact with keys as well as the doors not performing any collision what do you think I.ve done something wrong or did i forget something or maybe made a mistake in a class the only thing I see that I'm is a scope above of the return command.
May I see your CollisionChecker class? You can paste it here.
@@RyiSnow same problem , which code i'll send u
My game player isn't picking the objects at the end of program
I make it work with points to , in my game you can get points from apple and Carrots. thanks for a good video :)
That's so cool! I just love when people use my code and make something their original :D Keep up the good work and thank you for letting me know!
@@RyiSnow know i´m making some panel to have inventory in the top of the game, But also to see how many points you got, now you can only see it in the terminal.
You can draw text on the screen with Graphics2D's drawString method so maybe you want to try that :) On-screen text will be featured in Part 10!
@@RyiSnow im gone test that
thanks
hey!! thank you for this tutorial . i am having a problem in the output , my door is not being solid and my character can easily pass through it even when it has no keys
@RyiSnow help i tried to help to find my error but cant fix by my own , so plz help me out .
my full is perfectly work but my player could not available to receive key object .
can u tell me the error ????
13:47 You obviously know what youre talking about, but you often say "parse this Player class" which is technically wrong. We only parse the pointer of the current instance of the class. Could be misleading for people who dont know the difference.
there's something wrong with my map. when i move my player downward there are only some tiles and then black background starts showing moreover my pickup object method isnt working. i have checked it several times but i still cant figure out
hello, thank you for this tutorial! when you are checking the first result i do not get the message "up/down/etc collision!" when touching key or door, and my door isn't solid either. Do you know what happened? please let me know if i should paste some code here
the only things i changed were what intellij suggested to me ("enhanced switch statements") like this:
switch(entity.direction) {
case "up":
entity.solidArea.y -= entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("up collision!");
}
break;
case "down":
entity.solidArea.y += entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("down collision!");
}
break;
case "left":
entity.solidArea.x -= entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("left collision!");
}
break;
case "right":
entity.solidArea.x += entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("right collision!");
}
break;
}
to
switch (entity.direction) {
case "up" -> {
entity.solidArea.y -= entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("up collision!");
}
}
case "down" -> {
entity.solidArea.y += entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("down collision!");
}
}
case "left" -> {
entity.solidArea.x -= entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("left collision!");
}
}
case "right" -> {
entity.solidArea.x += entity.speed;
if (entity.solidArea.intersects(gp.obj[i].solidArea)) {
System.out.println("right collision!");
}
}
}
and i didnt think things like that could impact the games functioning (switching it back didnt change anything either, still no "up collision!").
now that i went through the video again, i realized i was missing an int counter2 = 0, maybe that is what went wrong? i went through your last video as well and cannot find where you put the int counter2, could you please tell me what video it is in? sorry for the trouble, thank you so much again for this tutorial, it is very clear and simple to follow through.
Don't worry about the counter2. That is for setting the player character in standstill position after he stopped moving. I doubt it's related with your issue (check part 10+ video if you are curious).
Could you paste your CollisionChecker class here? Then I will take a look.
i figured it out; i think i accidentally typed gp.obj[i].solidArea.x twice by accident. thank you for letting me know about the counter2!
Isn't it better to check if entity intersects with object outside of the switch ?
I noticed that too, I think it's written like this because the checkTile method contains direction specific information in the if checks, but checkObject doesn't have that, so it's easier to just check after the switch ends. It's a small difference, but it does add to readability.
bro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxedbro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxed
I think i have a problem, its that when i move all the objecs are teleported to the very top of the screen
Hi! I have a question: do you have a C or C++ background? :)
Lovely course ^_^
Hi. Thanks for the tutoriales, I have a problem, My door isn't Solid, I can walk over it, I don't know why the code is the same, any idea for why it's happening that?
bro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxed
@RyiSnow
im sorry for pining you but..
IM COMPLETLY STUCK HERE!
whenever i load the game i walk through the doors... i can pick up keys and open doors but when i dont have keys i just walk through and the doors dont disappear
Update 2:
i restarted my work from scratch and i did it alL by hand without the help of chatgpt
and my current problem is that keys are not being currently displayed at first it was working but there was one key only being displayed but now there are none once i tried moving the code somewhere else.. What do you think the problem is? the problem is in player class picKUpObjects and i dont know what to do so i could fix this code
What is the difference between entity and object x and y? Isn't it the same thing ?
can you make a home screen for the game with the option to resize the game to different resolutions ??
You can make a separate class for this navigate to that class first then navigate to the game (Main class) from the home screen on PLAY button clicked ;). Hope this opens your mind I'm developing this game for Android currently, a little harder but I know sort of what I'm doing :)
HI Ryi, is it possible to post a tutorial on players shooting projectiles like bullets? I'm kind of lost on how to do it
You can create bullets as Entity. Since I've received a couple of similar requests, I might squeeze a video at some point.
@@RyiSnow Ohh ok, thanks !
If you're interested, this is a fireball demo that I recently posted on my twitter. The fireball was created as Entity.
twitter.com/RyiSnow2017/status/1484146787980308480
@@RyiSnow Wow it looks so cool ! Would love your tutorial on this !
@@RyiSnow Hi RyiSnow, Can I ask, the way you create bullets is, first of all, create a child class called bullet which extends the entity class ? Then I also need to call the bullet class on the player and gamepanel class ?
I just need a direction and some ideas so I can work on it.
I know that the bullet class needs X and Y axis, so I will be using worldX and WorldY.
for the direction, I will be using the player direction
Enjoyed it!
Hi Ryi, I need help
I able to detect and make the object going null , I check using
System.out.println(gp.obj[i]);
gp.obj[i] = null;
System.out.println(gp.obj[i]);
but the image keep there not gone, what do you think Should I check?
after I check at the game actually the key is disappeared, but only one and specific one key
ahh my bad :D
I typo
if(player == true) {
index = 1;
}
it should be
if(player == true) {
index = i;
}
that's why only object number one that always disappear XD
I'm having a small issue when checking the object collision. After I add the line: { int objIndex = gp.cChecker.checkObject(this, true); } into the update method inside of the Player class, all of my assets (chests, keys, and doors) disappear from the game. Omitting this line makes them reappear, but I don't see that as fixing the problem lol. I am getting a warning about the objIndex stating that its value is never used, only option it gives to remedy is to remove it all together. and again, I don't see that as a solution.
I'm going to continue trying to figure this out. If anyone has any input or ideas of where I so obviously made a mistake, that would be much appreciated. 😁 I will update with my solution when it comes to me.
So that took 5 minutes. I mistakenly used the = instead of the + when setting the positions of gp.obj[i]. I really need to pay a little more attention to detail!
Thank you so much!
saved me a ton of debugging
HEELLLLPPP :) Hey so this is amazing, I even had to make my own Rectangle class for this method to work... Curious though, when my player interacts with the door for example if the player faces up or down when moving the collision is detected and the player is able to walk away perfectly fine. BUTTT when the door is placed on a horizontal plain so x axis the player gets stuck on the door and walks overtop. I'll keep looking at the code but I'm not seeing where or why this occurs.
why would you make your own rectangle class?
@@dash8497 I am using Android Studio! the default class is different for AS!
Hi, thanks for the videos, i implemented a Interface that have a method called executeAction() to implement in objects and avoid switch
Hey im having issues with my code not placing more than 1 object, for instance, I want to draw the door and the key but It only draws number 0 in the array (in this case only the door is being drawn)
Did you assign a different index to your key?
@@RyiSnow yes, my door is on [0] of the array, and the key is assigned to [1], but it only displays [0]. Everything else works perfectly though.
Try adding this line after g2.drawImage in the SuperObject: System.out.println(name + " is displayed");
And check if "Key is displayed" is printed out on the console when the key is visible on the screen. If the text is printed out, the object is rendered but somehow the image is invisible so maybe something is wrong with your image. If the text is not printed out, the program is not drawing the key object so maybe something is wrong with your object array or the for loop to draw them.
Yup its says that both are being displayed but only the door is being shown. Will check on it
@@RyiSnow I made a bunch of other assets in the array and no matter how much I add, only the number 0 in the array is ever shown no matter the image/object. It says in the terminal that all of them are displaying but its not showing on the screen.
Hello, i need help. When i touch a object, it doesnt write " up collision"(or "down collision", "right collision" or left "collision").
i'm having the same issue
i implement the diagonal movement, but yeah im stuck because when the player was interact with object that have collision value true the player just stuck, i think its because the for loop while getting the object. i hope i found the solution asap
did u get the solution? im having the same problem
here
can anyone help? I am at the end, I can get the player to pick up the key, key increases by one, but then I go down to the door and it does nothing it just stays there.. I have all the same code so I don't know why it's doing this...
Hi, thank you so much for this tutorial! I would like to know how can I allow more spawning of the objects after all the objects have been collected?
I just played with this today, you'd want a redraw method after a set time for the object or objects you want to redraw and place them in maybe .random locations on the screen for the player to collect at any time :).. I haven't tested the random placement part yet but you'd want a void to make sure the item doesn't get placed in the boarder for example!!
I have a string.hash code(); error because of objectName is null. How do I fix this
Isn't there a better way to do that
I mean the objects are checking intersecting all the time
1:50 it's alright man I think it's funny
but whit zoominout the man move how can i fix it?
If u want to use a better way of returning a non collision object u rather use -1 instead of 999. Its just a better practise, even though you prolly wont need it.
Thank you Ry again so much! When you get a chance I’d like to ask because my character goes to the door and gets stuck!
Amazing video!!!!
You're the best!!
Hello and your videos are awesome anyways i have a problem with the two up map keys the game stops and the error says about the key that goes to null because they are already null for some reason (The Down key works correctly)
bro has w rizz perchance? check the sigma base and see if any gyatts are getting fanum taxed
wow thank you
Hello, i do not getting collision, the keys disappear, but collision no happen
you are getting the collision if the keys are disappearing, because they disappear if your player collides with them
good video
Anyone have a fix for when not having any key's, you can still pass through doors?
nvm lol i got it
@@quentin4516 if you dont mind me asking what did you fix? I am having the same problem
@@sachmandhaliwal4618 I forgot, but I would check your player collision
RyiSnow, I keep getting this error when I try to run my movement, I get a null pointer error
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null
at main.CollisionChecker.checkObject(CollisionChecker.java:74)
at entity.Player.update(Player.java:88)
at main.GamePanel.update(GamePanel.java:98)
at main.GamePanel.run(GamePanel.java:82)
at java.base/java.lang.Thread.run(Thread.java:833)
it's an error at
gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX;
gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY;
i fixed it
@@fonzafeytt6644 how?
entity.solidArea.x = entity.solidAreaDefaultX;
entity.solidArea.y = entity.solidAreaDefaultY;
gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX;
gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY;
These need to be inside of
if (gp.obj[i] != null)
Probably you've placed outside of it.
move the 4 lines to fix it.
@@helioktt You have no idea how much this helps. Thank you thank you!
im here:)
for some reason, even though I defined the i variable as an int it still tells me I need to make it an integer, and i know this video is years old, but thanks for the videos in general!
Are you using all lowercase? like "for(int i = 0....)" ?
help my character can still pass through objects
i am invested.
Update! in one of the next 1-2 videos, it will deleted^^ :P
until now, the GamePanel's properties
public final int worldWidth = tileSize * maxWorldCol; and
public final int worldHeight = tileSize * maxWorldRow; were not used,..
Is there something I have overseen, or will this come yet ^^' :P ..but I will see anyways :D
this is so much fun to learn, and to create individually
Thanks!
Why am I getting this when I tried to move my character?
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 168 out of bounds for length 50
at my2dgame.CollisionChecker.checkTile(CollisionChecker.java:54)
at entity.Player.update(Player.java:86)
at my2dgame.GamePanel.update(GamePanel.java:101)
at my2dgame.GamePanel.run(GamePanel.java:76)
at java.base/java.lang.Thread.run(Thread.java:831)
I can't seem to fix it. can anybody help?????
Looks like one of the index (col or row) for the mapTileNum is getting a number which is beyond the array's boundary (50). That is not supposed to happen so I think some calculation is not correct.
I'm having the same issue. Did you ever figure this out?
having the same issue here. cant seem to fix it at all
HOW TO FIX????@@RyiSnow
@@apro8723 Don't know if this is relevant anymore or not, but I think it might be because of the statement:
entity.solidArea.x = entity.worldX + entity.solidArea.x inside of the checkObject()-method in CollisionChecker.
If you don't reset the value of the rectangles values like he does in 12:20 it will make the rectangle bigger than the map at some point - which would then give you an error since the array's boundary is only as big as the world (thus you get an array out of bounds exception).
intersects don't work!! Help :(
same problem
how did you fix it???
The door doesn't open when I have more than 0 keys
I had a similar problem.
It was because I had written "Door" as "door" in the pickUpObject method
@law3922 You saved me. I was stuck there cause my door would not open. Thank you so much
Hey RyiSnow I fixed the first error. But now whenever i run the game and try to move, I get a null pointer.
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null
at Main.CollisionDetection.checkObject(CollisionDetection.java:79)
at entity.Player.update(Player.java:89)
at Main.GamePanel.update(GamePanel.java:97)
at Main.GamePanel.run(GamePanel.java:72)
at java.base/java.lang.Thread.run(Thread.java:831)
Did you instantiate the object? Also, did you add the line "if(gp.obj[i] != null)"? NullPointerException error occurs when program tries to refer to an instance but it was empty (null).
@@RyiSnow Yes I did add if(gp.obj[i] != null).
@@RyiSnow Still doesn't work
Can you please help me, I don't understand why it's not working. I got this message : Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "name" because "this.gp.obj[i]" is null
at entity.Player.pickUpObject(Player.java:144)
at entity.Player.update(Player.java:106)
at main.gamePanel.update(gamePanel.java:108)
at main.gamePanel.run(gamePanel.java:89)
at java.base/java.lang.Thread.run(Thread.java:1623)
Check the line 144 in the Player class! That's where something went wrong.
It's showing FPS:60
In your game panel class, you have to remove the System.out.println("FPS:" + drawCount);
CODE in Answers
CODE Player:
package entityFolder;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import main.GamePanel;
import main.KeyHandler;
public class Player extends Entity {
GamePanel gp;
KeyHandler keyH;
public final int screenX ;
public final int screenY ;
int hasKey = 0;
public Player(GamePanel gp, KeyHandler keyH) {
this.gp = gp;
this.keyH = keyH;
screenX = gp.screenWidth/2 - (gp.tileSize/2);
screenY= gp.screenHeight/2 - (gp.tileSize/2);
solidArea = new Rectangle();
solidArea.x = 8;
solidArea.y = 16;
solidAreaDefaultX = solidArea.x;
solidAreaDefaultY = solidArea.y;
solidArea.width = 32;
solidArea.height = 32;
setDefaultValues();
getPlayerImage();
}
public void setDefaultValues() {
worldX = gp.tileSize * 23;
worldY = gp.tileSize * 21;
speed = 4;
direction = "down";
}
public void getPlayerImage() {
try {
up1 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_up_1.png")
);
up2 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_up_2.png")
);
down1 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_down_1.png")
);
down2 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_down_2.png")
);
left1 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_left_1.png")
);
left2 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_left_2.png")
);
right1 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_right_1.png")
);
right2 =
ImageIO.read(
getClass().getResourceAsStream("../res/player/boy_right_2.png")
);
} catch (Exception e) {
e.printStackTrace();
}
}
public void update() {
if (
keyH.upPressed == true ||
keyH.downPressed == true ||
keyH.leftPressed == true ||
keyH.rightPressed == true
) {
if (keyH.upPressed == true) {
direction = "up";
} else if (keyH.downPressed == true) {
direction = "down";
} else if (keyH.rightPressed == true) {
direction = "right";
;
} else if (keyH.leftPressed == true) {
direction = "left";
}
// CHECK TILE COLLISION
collisionOn = false;
gp.cChecker.checkTile(this);
// CHECK OBJEKT COLLISION
int objIndex = gp.cChecker.checkObject(this, true);
pickUpObject(objIndex);
// IF COLLISION = FALSE, PLAYER CAN MOVE
if(collisionOn == false){
switch(direction){
case "up" : worldY -= speed; break;
case "down" : worldY += speed; break;
case "right": worldX += speed; break;
case "left" : worldX -= speed; break;
}
}
spriteCounter++;
if (spriteCounter > 10) {
if (spriteNum == 1) {
spriteNum = 2;
} else if (spriteNum == 2) {
spriteNum = 1;
}
spriteCounter = 0;
}
}
}
public void pickUpObject(int i){
if(i != 999){
String objectName = gp.obj[i].name;
switch(objectName){
case "Key" : hasKey++; gp.obj[i] = null; break;
case "Door" : if(hasKey > 0){gp.obj[i] = null;hasKey--;} break;
// case "Boots": speed += 3; gp.obj[i] = null; break;
}
}
}
public void draw(Graphics2D g2) {
// g2.setColor(Color.WHITE);
// g2.fillRect(x, y, gp.tileSize, gp.tileSize);
BufferedImage image = null;
switch (direction) {
case "up":
if (spriteNum == 1) {
image = up1;
}
if (spriteNum == 2) {
image = up2;
}
break;
case "down":
if (spriteNum == 1) {
image = down1;
}
if (spriteNum == 2) {
image = down2;
}
break;
case "right":
if (spriteNum == 1) {
image = right1;
}
if (spriteNum == 2) {
image = right2;
}
break;
case "left":
if (spriteNum == 1) {
image = left1;
}
if (spriteNum == 2) {
image = left2;
}
break;
}
g2.drawImage(image, screenX, screenY, gp.tileSize, gp.tileSize, null);
}
}
CODE Super Obj:
package objects;
import java.awt.image.BufferedImage;
import main.GamePanel;
import java.awt.*;
public class SuperObject {
public BufferedImage image;
public String name;
public boolean collision = false;
public int worldX , worldY;
public Rectangle solidArea = new Rectangle(0,0,48,48);
public int solidAreaDefaultX = 0;
public int solidAreaDefaultY = 0;
public void draw(Graphics2D g2, GamePanel gp){
int screenX = worldX - gp.player.worldX + gp.player.screenX;
int screenY = worldY - gp.player.worldY + gp.player.screenY;
if( worldX + gp.tileSize > gp.player.worldX - gp.player.screenX &&
worldX - gp.tileSize < gp.player.worldX + gp.player.screenX &&
worldY + gp.tileSize > gp.player.worldY - gp.player.screenY &&
worldY - gp.tileSize < gp.player.worldY + gp.player.screenY //
){
g2.drawImage(image, screenX, screenY, gp.tileSize, gp.tileSize, null);
}
}
}
"imagine not putting your own twists to a game a youtuber has a tutorial on" -me
Algorithm comment
Hey RyiSnow!! I love your videos so much. For some reason im getting an error on " gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX;
gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY;" Anything im doing wrong?
It says "solidAreaDefaultX cannot be resolved or is not a field"
May I see your CollisionChecker class?
@@RyiSnow It wont let me send it
Did you create those two variables in SuperObject class?
@@RyiSnow omg i didnt
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 184 out of bounds for length 50
at main.CollisionChecker.checkTile(CollisionChecker.java:36)
at entity.Player.update(Player.java:93)
at main.GamePanel.update(GamePanel.java:150)
at main.GamePanel.run(GamePanel.java:137)
at java.base/java.lang.Thread.run(Thread.java:831)
how can I fix this?
-I managed to fix a similar error by removing the lines "entity.solidArea.y -= entity.speed" etc. in the "checkObject" method's switch statement. I'm not sure if that'll cause more problems down the line or not, though.-
Update: I believe this actually did cause a problem where I would become unable to move when touching any collidable object (except doors I had the key to).
I ended up restarting this program from scratch, and now I'm not getting the error. I'm still not sure what caused the error in the first place; I would recommend just rewriting the method from scratch, and if that doesn't work, rewriting the class from scratch.
@@dgarrard100 thx for your answer
damn im having the exact same problem here and its quite weird because this error happens when I try to move ingame. very strange
Hey, great video. Im having a problem, and I need some help. All my objects are being placed at the top of the map when I add this line "int objindex = gp.cChecker.checkObject(this, true);" in the player class. What could it be ? I could send you my code if needed.
Maybe you haven't set their worldX and worldY?
@@RyiSnow yeah it was that. Thanks.
help i'm getting an error and i can't move it after
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "name" because "this.gp.obj[i]" is null
at entity.Player.pickUpObject(Player.java:117)
at entity.Player.update(Player.java:81)
at main.GamePanel.update(GamePanel.java:88)
at main.GamePanel.run(GamePanel.java:74)
at java.base/java.lang.Thread.run(Thread.java:1583)
Looks like you're trying to read an empty object at line 117. There should be a reason for that so I suggest you look into that.
i looked into it like a bajillion times and just checked again but it doesn't help
nevermind it's fixed thanks for the help anyway
nevermind again its still there
@RyiSnow it's been 14 hours
Hello, I tried your method the collision of object and the player is perfectly fine and return the right object index however when I set null value in obj[] from pickUpObject method
I got this error
Exception in thread "Thread-4" java.lang.NullPointerException
at main.Collision.checkObject(Collision.java:138)
at entity.Player.updatePos(Player.java:163)
at entity.Player.update(Player.java:84)
at main.GameLoop.update(GameLoop.java:39)
at main.GameLoop.run(GameLoop.java:66)
at java.lang.Thread.run(Thread.java:745)
and I want to add animation to object how can I do that?
I fixed the issue but how about animation?
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null
at main.CollisionChecker.checkObject(CollisionChecker.java:116)
at entity.Player.update(Player.java:107)
at main.GamePanel.update(GamePanel.java:155)
at main.GamePanel.run(GamePanel.java:137)
at java.base/java.lang.Thread.run(Thread.java:833)
entity.solidArea.x = entity.solidAreaDefaultX;
entity.solidArea.y = entity.solidAreaDefaultY;
gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX;
gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY;
These need to be inside of
if (gp.obj[i] != null)
Probably you've placed outside of it.
move the 4 lines to fix it.
@@helioktt Broooooo you don't know how many moths i took to figure this issue out!!!!! i thank you very much!!!
i need help because i have this error meesage and i am not able to move the charackter Exception in thread "Thread-0" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "entity.solidAreaDefaultY" is null
at main.CollisionChecker.checkObjekt(CollisionChecker.java:108)
at entity.Player.update(Player.java:100)
at main.GamePanel.update(GamePanel.java:150)
at main.GamePanel.run(GamePanel.java:137)
at java.base/java.lang.Thread.run(Thread.java:831)
I place this highe : entity.solidArea.x = entity.solidAreaDefaultX;
entity.solidArea.y = entity.solidAreaDefaultY;
gp.obj[i].solidArea.x = gp.obj[i].solidAreaDefaultX;
gp.obj[i].solidArea.y = gp.obj[i].solidAreaDefaultY;
and optain this error message:
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 184 out of bounds for length 50
at main.CollisionChecker.checkTile(CollisionChecker.java:28)
at entity.Player.update(Player.java:93)
at main.GamePanel.update(GamePanel.java:150)
at main.GamePanel.run(GamePanel.java:137)
at java.base/java.lang.Thread.run(Thread.java:831)
Did you instantiate the solidArea in Entity?
@@RyiSnow hi thx for your help i fixed the problem (i wrote AreaDefaultX= areay;
@@coolfactor1796 where did you put this?
Hello, and huge thanks for your amazing tutorials Ryi!
I'm encountering an error the moment my character touches the Key Object (I've added what's in each line to the error message below):
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "" is null
at entity.Player.pickUpObject(Player.java:123)
Check the pinned comment!
@@RyiSnow I did, trust me! It didn't help and I'm getting a different sort of error: "Cannot invoke "String.hashCode()" because "" is null"
I've tried moving brackets, I've gone through this video and the previous one to try and see where I go wrong but I simply cannot find what I'm doing wrong D:
Can I see your pickUpObject method?
@@RyiSnow
public void pickUpObject(int i) {
if(i != 999) {
String objectName = gp.obj[i].name;
switch(objectName) {
case "Key":
hasKey++;
gp.obj[i] = null;
System.out.println("Key:"+hasKey);
break;
case "Door":
if(hasKey > 0) {
gp.obj[i] = null;
hasKey--;
}
System.out.println("Key:"+hasKey);
break;
}
}
}
@@RyiSnow
int objIndex = gp.cChecker.checkObject(this, true);
pickUpObject(objIndex);
For anyone that has this error:
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
at main.CollisionChecker.checkObject(CollisionChecker.java:67)
at entity.Player.update(Player.java:86)
at main.GamePanel.update(GamePanel.java:118)
at main.GamePanel.run(GamePanel.java:103)
at java.base/java.lang.Thread.run(Thread.java:1570)
try checking your i loop in checkObject() and see if you mispelled your "i" as a "1".
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot read field "solidArea" because "this.gp.obj[i]" is null
at Bohol/My2Dgame.CollisionChecker.checkObject(CollisionChecker.java:111)
at Bohol/entity.Player.update(Player.java:92)
at Bohol/My2Dgame.GamePanel.update(GamePanel.java:130)
at Bohol/My2Dgame.GamePanel.run(GamePanel.java:115)
at java.base/java.lang.Thread.run(Thread.java:833)
can u help me this this why it's not working? these are seem to be the problem
Hmm, when you are setting your objects' image, do you have the correct file path? I think it should be /objects/key.png" for instance. What IDE are you using? In some IDE's like IntelliJ that I'm using, you have to right click the res folder and Mark directory as - Resources Root. As he explained in an earlier video, / at the beginning means it goes from root, meaning the project folder, into res, and then it checks the folders from there.