Java - Slick - Deleting Images - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Tools & Technology (/forum-10.html)
+--- Thread: Java - Slick - Deleting Images (/thread-9319.html)
Java - Slick - Deleting Images - various_names__ - Sep 3, 2011 01:37 AM
Hey there, good people of iDevGames.
I'm various_names__ and I've been reading the forums for a while, and I couldn't find one thing;
I wanted to know how (or if) you could remove (read: delete, get totally rid of) and image in slick, after a certain event happens.
Like in this pseudo code:
I have a Graphics component (or whatever its called), which I use for drawing the image, and i use an Image data type from the
I wanted to know this because I'm a bit of a newbie to Slick/Java, and so I'm learning by making a simple space shooter game, and I would like to keep it in top quality (framerate-wise ), and I'm worried that if I start loading too many bullets the game might lag.
Oh, btw, one last thing; what is the optimal way of loading the bullets? Should I load them individually, or off a list, or what? Any help is appreciated.
RE: Java - Slick - Deleting Images - AndyKorth - Sep 3, 2011 12:19 PM
I'm not quite sure what you mean when you say you want to remove an image.
Probably you just want to stop drawing the image on the screen, so don't call nyancat.draw() in your render loop (or graphics.draw(nyancat); )
I expect slick to handle any number of bullets you can manage. I render several hundred objects with slick each frame and it's no problem. Just try it. If you do run into performance problems, come back and we can discuss more solutions.
If you actually do need to destroy images, the Image class does have a destroy method- but you almost certainly should not be calling destroy unless you are dynamically creating images in your game using render textures or something like that.
You should be doing something like:
Also a side note, I'm the only one here who ever uses Slick.. you'll find a more active slick community here:
RE: Java - Slick - Deleting Images - various_names__ - Sep 4, 2011 12:54 AM
Hey Andy, thanks alot for the very useful (and polite! ) reply.
Your post answered all my questions, except for one;
How can I draw several images of the same type?
For example, let's pretend I have this code (Using nyancat as an example again ):
Then I would run drawNyan in my update loop. However, that would only draw one nyancat to the screen. How could I draw several nyans, without going too overboard with the code? Is there some way to run the drawNyan function several times and make it draw different nyans to the screen (while using the same "Image nyancat" data)? Because from my knowledge, for as long as I run the drawNyan code is for how long the nyancat will stay displayed, when the drawNyan function stops being used the nyancat goes invisible, while I need it to generate a new nyancat.
Thanks for all your help,
RE: Java - Slick - Deleting Images - AndyKorth - Sep 4, 2011 09:25 PM
As you probably suspected, you can just:
etc. But you probably will want to do something like:
So you'll need to make a sprite class (or call it entity, or whatever). The plus here is that each cat can have it's own x and y position. It can even be used for different cats- just give one cat the Nyan cat image, and another cat the NEDM cat image, etc. Then by changing the position of the sprites, it will draw at the new position.
You can then add or remove sprites to this list at will- add new cats, remove old cats, etc.
RE: Java - Slick - Deleting Images - various_names__ - Sep 5, 2011 12:25 AM
*nod* Yeah, that works just fine, and fixed my problem for allocation of the (nyan)cats. But now I have another problem: How do I create nyancats dynamically during run-time?
For example, let's pretend my game is running, and when the player presses the spacebar, I want to grab a cat from that list and render it to the screen. Yeah, that works just great, but how do I populate the LinkedList with cats for the game to use to start with?
I mean, surely I could do this:
With nyanEntity having a constructor of (location of cat image, x position, y position), but that's generating the nyans manually. How could I make it so that a function of some sort creates nyans, names them individually (e.g. nyan001, nyan002, nyan003) and inserts them into the LinkedList? I mean, I'm not sure of any way of naming variables dynamically, is there a way?
So I need to:
The only thing I really need help with is the first thing (naming them dynamically). But anyway, thanks alot for your help Andy, I really appreciate it
RE: Java - Slick - Deleting Images - AndyKorth - Sep 5, 2011 10:19 AM
Well the point of having a list is that you don't have a separate variable for everything you add- obviously that won't really work when you have a game with 500 cats, 2000 bullets, etc.
You can just add the cats within a loop also. You should just be creating them as local variables, it sounds like you might want to brush up on how variables and stuff works. You don't even need to assign them to variables if you are immediately putting them in a list.
That will add 10 nyans, they don't need to be individually named. Accessing that list of nyans will probably be done by a loop for most circumstances.
RE: Java - Slick - Deleting Images - various_names__ - Sep 6, 2011 01:08 AM
(Sep 5, 2011 10:19 AM)AndyKorth Wrote: Well the point of having a list is that you don't have a separate variable for everything you add- obviously that won't really work when you have a game with 500 cats, 2000 bullets, etc.
(Sep 5, 2011 10:19 AM)AndyKorth Wrote: it sounds like you might want to brush up on how variables and stuff works.
Yeah, my bad, it was late at night when I posted, and my head was all over the place; I had an idea of assigning each nyan in the list with an individual ID, so that I could reuse them, and for debugging. But a while later of reading your post (and re-reading mine to make sure I hadn't ingested any illegal substances that had acted as retardants/made me trip on ballz at the time of posting), I realized my ignorance at the time.
(Sep 5, 2011 10:19 AM)AndyKorth Wrote: Accessing that list of nyans will probably be done by a loop for most circumstances.
*nod* With a (enhanced) for loop, I could probably do something like run through the list, and check each nyan's position to see if he is outside of the screen, and if so to reuse him to save space, etc. (us C++ programmers like doing that (Saving space that is, not reusing cats ))
Well, I think that's most of my problems are fixed (until I get to implementing the code ), thank you SO much for all your help Andy; most people would have told me to "f*cking Google it!" or to "Use the search bar again, please, we don't want trolls messing up the forum *hurr durr hurr*" So, you know, thanks .
Hehe, I'm back.
One last question, not nyanList related. I'm loading a large png file for my game's background, but I keep getting the error:
"class org.newdawn.slick.opengl.PNGImageData failed to read the data
java.io.IOException: Not a valid PNG file"
followed by numerous references to where this error occurs. I've checked my code and I'm sure I'm loading the image correctly. Is there some kind of maximum size for an image in slick, or is this related to my file's encoding or something?
Heh, thanks again Andy!
Sorry, didn't want to triple post, but...
Reclaimed looks amazing! Wow! It looks so good!
The server isn't still up, is it? I'd have loved to have played it...
But, yeah, good job!
RE: Java - Slick - Deleting Images - AndyKorth - Sep 6, 2011 05:10 AM
How large is large? if it's over 2500 pixels on a side, then it's probably waaaaay too large anyway, and you'd need to think about what you're doing with it and consider some other technique.
I guess you can check the encoding, but if Preview opens the png, it should probably be opened by slick.
Glad you liked how Reclaimed looks- nah the server usually isn't up but I can turn it on.
RE: Java - Slick - Deleting Images - various_names__ - Sep 9, 2011 01:56 AM
It's about 900x480; the size of my game screen (weird dimensions, i know) and it opens properly in any paint/picture program; I'll have to check the encoding.
Another question; what is the most supported script that Slick/Java supports? I need one that can access Java objects, create variables, change variables in a Java object, etc. All the good stuff.
I found Rhino, but Google didn't wanna give me any tutorials except for some (really) outdated ones.
RE: Java - Slick - Deleting Images - AndyKorth - Sep 9, 2011 07:37 AM
A scripting language to include? Unless you know of a language you like better than Java, I would recommend using Java for the whole project. A lot of people will integrate something like Lua into their game, because they like Lua a lot more than the C/C++ code the rest of their engine is written in. But y ou don't have to deal with the problems of C/C++, and you don't seem to have a favorite scripting language already learned and picked out.
Perhaps others here will have different opinions though.
RE: Java - Slick - Deleting Images - OneSadCookie - Sep 9, 2011 08:40 AM
I see no particular benefit to a scripting language over Java itself unless there is some feature (perhaps keybind macros?) that's more easily implemented in the scripting language.
Java's fast, has a good GC, etc. and it's not particularly hard to write...
RE: Java - Slick - Deleting Images - various_names__ - Sep 10, 2011 12:07 AM
It's not that I'm stuck or having problem with something, it's just that I wanted to have enemy AI/weapon AI in scripts; to manage them better, etc.
That way, I could have the AI in a script, and then in my enemy class, instead of extending it for each new enemy type/etc., I could just load a different script.
But, whatever, if you guys think that's a bad idea then I'll just write it all in Java.