Flickering problem in sprite animation
in
Programming Questions
•
9 months ago
I'm just drawing a simple sprite on the screen. It seems to be flickering on and off. Not sure what to do about it... I've tried double buffering and not, it happens on both. Even though I've heard double buffering is already provided. I'm also getting a decent framerate... Well here's my code if anyone has any ideas.
- Sprite box;
- PGraphics screen;
- void initScreen(){
- size(800, 450);
- frameRate(30);
- rectMode(CENTER);
- screen = createGraphics(800, 450);
- smooth();
- noStroke();
- fill(255);
- }
- void setup() {
- initScreen();
- box = new Sprite("spritesheet.png", 300/5, 360/6);
- };
- void draw() {
- screen.beginDraw();
- screen.background(#999999);
- screen.text(frameRate, width-50, 15);
- box._draw();
- screen.endDraw();
- image(screen, 0, 0);
- }
- class Sprite {
- private PImage img;
- private int x = 0;
- private int y = 0;
- private int w;//sprite width
- private int h;//sprite height
- Sprite(String img_path, int wc, int hc){
- img = loadImage(img_path);
- w = wc;
- h = hc;
- }
- void _draw(){
- screen.image(img.get(x*w, y*h, w, h), 100, 100);
- //move ahead a frame
- x += 1;
- //see if we should move down a row
- if(x*w > img.width){
- x = 0;
- //see if we should go back to the beginning
- if(y*h >= img.height){
- y = 0;
- } else {
- y += 1;
- }
- }
- }
- }
1