Sprite Movement

After I moves my Sprite from one place to another,
the original Sprite did not deleted and exists in the Canvas.
So, my question is Should I need to apply LayerManger.remove(Sprite) method to delete the original Sprite everytime?
If I apply paint’s drawImage, I only can apply repaint() to eliminate the orginal picture.
Can I apply repaint() instead of LayerManger.remove(Sprite) ?
Thank you~

Hi

Your sprite will be an instance of the Sprite object that you have created. Using LayerManager.remove will only remove the reference to the sprite object you have created, from the LayerManager. To actually free the sprite you will have to set it to null.
e.g.
Sprite mySprite = new Sprite(parameters…);
do some code…
remove it from layer manager…
mySprite = null;

This will enable the garbage collector to free up the memory used by the sprite. Or you can do a manual system.gc(), which wont necessarily guarantee a garbage collect… but thats beside the point.

You will have to repaint the screen every time a change is made in order to make that change visible to the user…

What I mean is…

Of course you may render your screen every few milliseconds or game ticks. This really is dependant on your code. But what I mean by the above statement is you need to make your code display those changes on the screen. In this case the change is removing the sprite. Using layermanager.remove will remove the sprite from the layer manager. But you will either have to force the layer manager to repaint or have your rendering system do it for you.

Hope this helps.

Ameano

I see, thanks a lot. :slight_smile:
Another question I want to ask is what is the difference between repaint() and flushGraphics() ?
Thanks

Not quite sure… but I believe flushGraphics is used when you want to flush the current graphics buffer to the device (screen) and repaint is used when you want to force the graphics context to paint everything again to the current graphics buffer (in double buffered systems), but may not necessarily paint to the screen. Sounds about right.

Amin

I see, thanks a lot :slight_smile:

Actually, the diferences between repaint() and flushGraphics() are that repaint informs the application that the Canvas needs to be repainted. The painting operation happens using the Canvas paint() method. Since it is a request, you have no idea when the application will answer your request. But it shall do so sooner or later.

flushGraphics can only be used with gameCanvas(), and its responsibility is to flush the offscreen buffer into the device display. So, the preferred aproach for using GameCanvas is not to override the paint method and ask for a repaint(), but to get the graphics object from the GameCanvas (by calling getGraphics()) drawing stuff with it and then flush the graphics.

The painting operations used with the graphics object returned by the getGraphics() method of GameCanvas will draw on an offscreen buffer, and flushGraphics() will flush the buffer content into the display.

Diferently from the asynchronous behaviour of paint()/repaint(), flushGraphics will only return when the buffer content is fully drawn into the display.

The API docs have some more information about that.

Thanks for danielmfreitas !~~ :smiley: