This site requires JavaScript, please enable it in your browser!
Greenfoot back
webmessia
webmessia wrote ...

2011/4/15

Sun Beams

webmessia webmessia

2011/4/15

#
In my scenario BleetFleet I have some sun beams in the background that rotate around the sun. I've so far used two methods to achieve this. Method 1) Paint the sky at construction. Then when running rotate large sun beam image and paint onto the sky, then paint the grass on top. Pros: Less memory used Cons: Slow, from 33fps to 12fps Method 2) Create an array of GreenfootImages for frames(15 at the moment). Then draw the sky on each one, aswell as the sun beams rotated slightly each time. Then draw the grass on each one. This is done once at construction, then the images are looped through when running. Pros: Fast. 30fps+ easily Cons: Lots of memory needed. over 100MB I think. I kept on getting Java Heap space errors after a few compiles. Seems to have less memory problems exported. Is there anyway to reduce the ammount of memory the images take up? Other than reducing the frame count. Or if anyone knows of a more efficient way of drawing the beams I would like to know.
PiRocks PiRocks

2011/4/16

#
Looking at your scenario, it seams like you can create 4 or 5 images that rotate it a little bit (so that you have the starting image again), that way you can loop fast without using much memory
Builderboy2005 Builderboy2005

2011/4/16

#
How about creating a single sun actor with the beams included in the image, and simply rotating the actor?
JL235 JL235

2011/4/16

#
webmessia wrote...
Method 2) Create an array of GreenfootImages for frames(15 at the moment). Then draw the sky on each one, aswell as the sun beams rotated slightly each time. Then draw the grass on each one. This is done once at construction, then the images are looped through when running. Pros: Fast. 30fps+ easily Cons: Lots of memory needed. over 100MB I think. I kept on getting Java Heap space errors after a few compiles. Seems to have less memory problems exported. Is there anyway to reduce the ammount of memory the images take up?
This is quite an advanced part of Java to use, but if your feeling adventurous then one trick you can use is to hold your images using Weak References. Weak References allow you to reference an object that can be reclaimed by Java if it runs out of memory; but before it will give you any out of memory errors. The Weak Reference wraps the object, and replaces the value it's holding with null if the object gets reclaimed. This means you can have lots of images stored without having to worry as much about memory. When some of the images do get reclaimed then you will have to generate a new image to replace it. I've used this technique in the past in Greenfoot for holding large numbers of rotated images, and it did help dramatically. I usually use a map to hold all of this; mapping the angle of the images rotation to the image itself.
You need to login to post a reply.