Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on a tile based game in Java. I have a 2D array of tiles (JComponents) in a GridLayout, this is fine for creating the world (I think), but I am stumped at how to move the sprite smoothly between tiles. Currently, each JComponent tile draws the world and the sprite in accordance with booleans representing state (i.e. to show the sprite or not to show the sprite), however, this is not smooth. The sprite simply disappears from one tile and appears in another.

Any ideas on how I could go about moving the sprite in a smooth manner?

I am new to the whole game dev thing, so please be patient.

Regards, Jack Hunt

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If you wish to stick to the JComponents in a GridLayout approach (suggestions of the top of my head):

Make the sprites into components of their own.

Make your own container and override the layout functionality. Make the tilemap-container and any sprites children of this container.

Override doLayout to make the tilemap-container fill the available space (or something else, up to you), then position the sprites as wanted (or not, your choice, they might already be placed correctly by some other function). Make sure the tilemap-container is at the bottom (using setComponentZOrder).

The sprites now have position independent of the map and are drawn on top of it.

Edit: Alternatively you can create a layout manager that does this (i.e. instead of overriding doLayout), and use that. And now that I think about it that may be the more "correct" way of doing it. I have an idea for how to do this nice and easy but it's 3 in the morning and the margin's too small.

share|improve this answer
    
Thanks for the response, I think I know what you mean. I shall have a go at this tomorrow, definitely, thank you. It is very late, my eyes are drooping. –  VisionIncision Feb 16 '12 at 2:14
    
I would advice against using JComponents for this. If you really want to, go for it, but it's going to be an enormous amount of work since Swing components are not designed for that after all. If you really, really want to stick with Swing, I'd use JPanel or your own JComponent and do the drawing myself, discarding the GridLayout approach. –  kaoD Feb 16 '12 at 13:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.