This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I'm making my second game, this time a side-scroller so I can have more expandable playing areas. I have all the moving and KeyListening worked out, but I'm not sure how to attack the detection of the character hitting the ground/landscape. I understand I could use pixel color detection or something similar, but that seems a bit complex for something so simple.
What's the usual technique for doing something like this? I want it to be flexible too, so that it would work regardless of what the terrain looked like.
If you create the landscape image within your program then just record the height(s) of the ground for each step along the x axis. Then check your ships height against this landscape height each time the ship moves. If you're using an image created in another program then I guess the simplest method is to just examine the image and manually store the height at each stage - this will be boring to do and might take a while but at least flat sections will be easy. Writing a program to do a one time analysis of a single image isn't going to save you any time and will probably introduce a lot of bugs. If it was me I'd just create the image within the program.
I see what you're saying about doing a manual rundown of the landscape image and going from there, however I was hoping for something a bit more versatile.
In particular, I have in mind the game Terraria. The maps are randomly generated and this is still achieved. I know Terraria was done in the XNA game studio in C++, so maybe it's not as easily achievable in Java?
My bet is a test for transparency under the centered entity. Too bad that involves some more knowledge on the color class and its methods. More reading for me! haha
Joined: Jun 28, 2009
Could you not use the same map files that this game uses and take the height values from the original map? I did a quick google and found various map viewers and editors for this game so it must be possible to work out the heights from them? The advantage of doing it this way is you can run the maps directly and the game would be closer to the original. You could also use the user built maps as well. There may be copyright issues of course (not sure).
I haven't ever played the game or examined these map files so I might be talking ****. I'm pretty sure that working on the images directly is not the best way to go. If it was me then I think I'd just stump up the £6 and play the game using their software
PS They seem to have a Terraria database/file format that you could read and process.
Well I'm not trying to recreate the game. It's just one source of my inspiration. I think they did a great job on the mapping, and that's why I was looking into it.
I did some pondering and think I figured it out though. Terraria creates randomly generated maps out of varying types of blocks (like Minecraft). Thus, the blocks must be entities and have their own drawing instance data. These probably include shapes which could then utilize the intersects() method to do what I'm trying to do.
This sounds a lot more efficient than transparency as well.
And I do actually own Terraria
Great game; you should look into it!
Not sure if this is the best way to go about it but the Rectangle class has an intersects method. If your landscape and your player extend Rectangle you can just use an if statement to see if they intersect.