14 Replies Latest reply: Jan 22, 2010 10:32 AM by 843853 RSS

    BufferStrategy problem

    843853
      Hello

      I am having problems with getting bufferstrategy to work properly. I have read a couple of guides and articles and what i have pretty much fits their description but can't get it to work properly.
      The problem is that when my character moves the whole screen flcikers and the background alternates between black and white. Also some artifacts appear and disappear with each movement. here is the portion of the code that deals with buffer strategy:
      class MainLoop extends TimerTask{
      
      
                @Override
                public void run() {
                     // TODO Auto-generated method stub
                     if(running){
                          mainPane.gameUpdate();
                          mainPane.reDraw();
                     }
                
                }
      
           }
      
      private void gameUpdate(){
                     xV = 0;
                     if (isJumping){
                          yV = JUMP_SPEED;     
                          if(moveLeft) xV = -X_SPEED;//-SPEED + 4;
                          if(moveRight) xV = X_SPEED;//SPEED - 4;
                     } else {
                          if(moveLeft) xV = -SPEED;
                          if(moveRight) xV = SPEED;
                     }
                }
      
                private void reDraw(){
      
                     final float POSITION_X = mainPlayer.getX();
                     final float POSITION_Y = mainPlayer.getY();
                     final int IMAGE_W = mainPlayer.getWidth();
                     final int IMAGE_H = mainPlayer.getHeight();
                     didPaint = false;
      
                     if (POSITION_X >= 1792){
      
                          mainPlayer.setX(1791);          
                     } else if (POSITION_X <= 0){
      
                          mainPlayer.setX(1);
                     } else {
      
                          Graphics2D gScr;
                          try {
                               gScr = (Graphics2D) bufferStrategy.getDrawGraphics();
                               
                               if(paintOnce < 1){
                                mainPlayer.paintImage(gScr);
                                didPaint = true;
                               }
                               paintOnce = 1;
                               
                               if(xV != 0){
                                    gScr.clearRect((int)POSITION_X, (int)POSITION_Y, IMAGE_W, IMAGE_H);
                                    //gScr.fillRect((int)POSITION_X, (int)POSITION_Y, IMAGE_W, IMAGE_H);
                                    mainPlayer.setX(POSITION_X + xV);     
                                    mainPlayer.paintImage(gScr);
                                    didPaint = true;
                               }
      
      
                               if(jumpDown && jumpDownCount < 15){
                                    didPaint = true;
                                    gScr.clearRect((int)POSITION_X, (int)POSITION_Y - 7, IMAGE_W, IMAGE_H);
                                    //gScr.fillRect((int)POSITION_X, (int)POSITION_Y - 7, IMAGE_W , IMAGE_H );
                                    mainPlayer.setY(POSITION_Y + yV);
                                    mainPlayer.paintImage(gScr);
                                    if (jumpDownCount == 14){
                                         jumpDown = false;
                                         isJumping = false;
                                         actionJumpKey.setEnabled(true);
                                    }
                                    jumpDownCount++;
                               }
      
                               if(isJumping && jumpUpCount < 15){
                                    didPaint = true;
                                    gScr.fillRect((int)POSITION_X, (int)POSITION_Y + 6, IMAGE_W, IMAGE_H);
                                    mainPlayer.setY(POSITION_Y - yV);
                                    mainPlayer.paintImage(gScr);
                                    if (jumpUpCount == 14){
                                         jumpDown = true;
                                    }
                                    jumpUpCount++;
                               }
                               
                               gScr.dispose();
      
                               if (!bufferStrategy.contentsLost() && didPaint)
                                    bufferStrategy.show( );
                               else
                                    System.out.println("Contents Lost");
      
                               Toolkit.getDefaultToolkit( ).sync( );
                               
                          }catch (Exception e){
                               //System.out.println("Graphics context error: " + e);
                               e.printStackTrace();
                               running = false;
                          }
      
                          
                     }
      
                }
      2 properties:
      frame.getRootPane().setDoubleBuffered(false);
                setFocusTraversalKeysEnabled(false);
      The average fps of the game is pretty much close 60 fps. I am extending canvas and getting the bufferstrategy from it. I would post the whole code but it is rather long, so I will post it if it would help.
      What am I doing wrong : (
      Thank you.