Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Game Sprite Flashing/Not Animated  (Read 629 times)
0 Members and 1 Guest are viewing this topic.
Offline Splashsky

Senior Newbie


Exp: 1-3 months


Blonde, not dumb.


« Posted 2014-05-09 17:34:02 »

Hey everybody! My first post on the JGO forums.

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.

This is the Animation class that's supposed to manage running through the frames, while this is the class of the character itself.

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.
Offline Bearded Cow
« Reply #1 - Posted 2014-05-09 19:35:56 »

Where is the bit of code that says, "animation... draw". I can't find it, sorry if being blind.
Offline Splashsky

Senior Newbie


Exp: 1-3 months


Blonde, not dumb.


« Reply #2 - Posted 2014-05-09 20:37:22 »

Where is the bit of code that says, "animation... draw". I can't find it, sorry if being blind.

Whoops, sorry! If I understand what you're asking for, you'll want the specific GameState that I'm drawing into.

Though, the Customer superclass does have actual drawing of each frame of the animation.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bearded Cow
« Reply #3 - Posted 2014-05-09 20:48:28 »

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

You have a variable 'frames' which is a BufferedImage array, and I can see the update method but not the draw method.

But what I can tell from this, is that you don't seem to be doublebuffering.
Offline Splashsky

Senior Newbie


Exp: 1-3 months


Blonde, not dumb.


« Reply #4 - Posted 2014-05-09 21:24:15 »

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.

You have a variable 'frames' which is a BufferedImage array, and I can see the update method but not the draw method.
See above. Tongue

But what I can tell from this, is that you don't seem to be doublebuffering.
Sigh... I figured as much. I'm somewhat of a newbie to this stuff. How would I implement double buffering into it as it is?
Offline Jacob Pickens
« Reply #5 - Posted 2014-05-09 21:38:04 »

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!
Offline Splashsky

Senior Newbie


Exp: 1-3 months


Blonde, not dumb.


« Reply #6 - Posted 2014-05-09 21:40:41 »

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.
Offline Jacob Pickens
« Reply #7 - Posted 2014-05-09 21:51:47 »

Yes, Graphics2D is Java2D. (And honestly, If your a true beginner, I'd start with Slick2D. Its a really amazing and easy library. Also, slick is really good on animations from sprite sheets  Wink )

Slick Tutorial:
https://www.youtube.com/watch?v=AXNDBQfCd08
Offline Splashsky

Senior Newbie


Exp: 1-3 months


Blonde, not dumb.


« Reply #8 - Posted 2014-05-09 21:56:25 »

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.
Offline Jacob Pickens
« Reply #9 - Posted 2014-05-09 22:01:54 »

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!)

EDIT: Or apparently you do draw to the strategy whoops.  Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #10 - Posted 2014-05-09 22:03:07 »

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.

http://docs.oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html

^ Read that, also read the previous pages to understand what the BufferStrategy is doing for you.
Offline Splashsky

Senior Newbie


Exp: 1-3 months


Blonde, not dumb.


« Reply #11 - Posted 2014-05-09 22:13:29 »

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?
Offline Jacob Pickens
« Reply #12 - Posted 2014-05-09 22:14:30 »

Ahhh, you don't have a class extend bufferStrategy. You make an instance of it!  Pointing  Grin
Offline BurntPizza
« Reply #13 - Posted 2014-05-09 22:16:43 »

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().

Also, use createBufferStrategy(2), triple buffering is not necessary.


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

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.
Offline Splashsky

Senior Newbie


Exp: 1-3 months


Blonde, not dumb.


« Reply #14 - Posted 2014-05-09 22:26:17 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class Game {
   
   public static void main(String[] args) {
     
      // For the main function, we'll initialize the window.
      JFrame window = new JFrame("DEV - Lemonade Tycoon");
      window.setContentPane(new GamePanel());
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setResizable(false);
      window.pack();
      window.setVisible(true);
     
   }
   
}
Offline BurntPizza
« Reply #15 - Posted 2014-05-09 22:32:16 »

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.

Example class using BufferStrategy on SO

Read the comments for potential gotchas.

EDIT: I would use window.add(panel) instead of setContentPane(panel).

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.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!