Anyway, I'm working on making a custom framework for a 2D tile-based tycoon game I'm working on. The current concept is simple; draw up a 2D map, have customers randomly pop up and walk to the stand, then perform all the stuff that costs money and what-not.

I have a working tile map system... however, I added a character sprite to my game and it flashes. Like, blinking flashes. I'm double buffering (at least, I believe I am), and I've checked out a couple different things, but I'm not sure what the flashing is.

I'm using a 6x4 sprite sheet, with each sprite being 32x32 pixels. I'm only ordering the game to use the first four rows.

I'm not sure about how I'm rendering it, so here's the main GamePanel class. The GameStateManager it referenced doesn't do much more than provide a wrapper for each individual game state. Everything has it's own sketch/render function, basically.

Sorry, but I can't find the bit of code that relates to drawing your buffered image from the animation class.

That would be in the Customer superclass. In the Customer's update() function, I update the automaton instance of the Animation class, which by all rights should move to the next frame. Then, in the Customer's sketch() function (I use the word sketch instead of draw) I get the Animation's current image and draw it.... theoretically.

Are you using Java2D? Also is the entire screen flashing? Because in that case your going to need to make a buffer. Just google Java BufferStrategy and stuff around that and you should find some nice tutorials!

Are you using Java2D? Also is the entire screen flashing? Because in that case your going to need to make a buffer. Just google Java BufferStrategy and stuff around that and you should find some nice tutorials!

I'm using Graphics2D, if that's a part of Java2D... also, no. Just the sprite itself. The rest of the game renders perfectly.

Well, I thought about using a library, but decided if I learn at this level, I'll be better off later, hahah.

And in any case, if I use BufferStrategy, I would instantiate an instance of it in my GamePanel (JPanel) and... do you draw to the Strategy directly, or what? Strategy is a little... ambiguous to me at the moment.

It is a very efficient way to render. And no you would still draw to your Graphics2D object but after your call strategy.draw(); or something along those lines you call g.dispose(); to clear the previous graphics. I'm sure there is an in-depth tutorial somewhere on them somewhere. I haven't used them in a while since I'm hooked up with slick. (It doesn't make you a bad programmer to use a library by the way. Many beginners think that!)

Well, I thought about using a library, but decided if I learn at this level, I'll be better off later, hahah.

And in any case, if I use BufferStrategy, I would instantiate an instance of it in my GamePanel (JPanel) and... do you draw to the Strategy directly, or what? Strategy is a little... ambiguous to me at the moment.

Yes, you draw to the strategy, then "flip" it to show what you've just drawn.

It would appear that whenever I try calling this.createBufferStrategy(3) in the constructor of my GamePanel, Eclipse gives me the error "The method createBufferStrategy(int) from the type Component is not visible". Am I missing something about it?

It would appear that whenever I try calling this.createBufferStrategy(3) in the constructor of my GamePanel, Eclipse gives me the error "The method createBufferStrategy(int) from the type Component is not visible". Am I missing something about it?

A BufferStrategy can only be created when the canvas or frame that it will display to is visible on screen. This is one of the main drawbacks of BS (heh) in my opinion.It is easy to work around however: just call setVisible(true) on the JFrame or whatever prior to createBufferStrategy().

Ahhh, you don't have a class extend bufferStrategy. You make an instance of it!

He is making an instance of it.

Another alternative is to just use a regular BufferedImage for double buffering instead of a BufferStrategy, at least while learning the ropes.Doing so has some advantages:

You do the double buffering manually, so you get to see (and control) what exactly is going on.

You don't have to deal with BufferStrategy's volatility (it can be "corrupted" (simplification), which you then have to deal with)

It is slightly slower in my experience, but if you're hitting performance problems just for buffering, then you have other problems.If you would like to learn more, simply google "java double buffering" or some such. There will be many, many tutorials.

A BufferStrategy can only be created when the canvas or frame that it will display to is visible on screen. This is one of the main drawbacks of BS (heh) in my opinion.It is easy to work around however: just call setVisible(true) on the JFrame or whatever prior to createBufferStrategy().

Well, that's just odd, considering I do set the Frame itself to visible prior to messing with the GamePanel. Do I create the Strategy in the Game class (JFrame) or the GamePanel (JPanel)? Because either way, I use setVisible(true) on both, and I get the same annoying error.

But is the GamePanel added to the frame (and visible) before you attempt to create the buffers?Also, no need to call setVisible() on a panel if it's in a frame. Calling setVisible() on the frame will suffice.

EDIT2: Also note that the JPanel should be (or contain) a Canvas, and that Canvas is what you should create the bufferstrategy with. Otherwise use window.createBufferStrategy() and forget the whole panel business.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org