Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: Flickering on a 2D Sprite

  1. #1
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Flickering on a 2D Sprite

    Hello!

    I'm currently working on a simple game in the 2D library in java, but i have stumbled over a flickering bug which i'm pretty annoyed over.

    So i have a player class, in the constructor it loads a PNG sprite sheet.

    And when the player presses one of the arrow keys, it changes the sprite after which array the player pressed:
    Java Code:
    public void changePlayerImage(int dir){ //Dir 0 = Image UP etc
    		switch(dir){
    			default:
    				currentImage = grabSprite(3,2,28,46,pSprites);
    				CURRENT_IMAGE_SHOWN = 1;
    				break;
    			case IMAGE_UP:
    				if(CURRENT_IMAGE_SHOWN != IMAGE_UP)
    					currentImage = grabSprite(3,145,28,46,pSprites);
    					CURRENT_IMAGE_SHOWN = 0;
    				break;
    			case IMAGE_DOWN:
    				if(CURRENT_IMAGE_SHOWN != IMAGE_DOWN)
    					currentImage = grabSprite(3,2,28,46,pSprites);
    					CURRENT_IMAGE_SHOWN = 1;
    				break;
    			case IMAGE_LEFT:
    				if(CURRENT_IMAGE_SHOWN != IMAGE_LEFT)
    					currentImage = grabSprite(3,52,28,46,pSprites);
    					CURRENT_IMAGE_SHOWN = 2;
    				break;
    			case IMAGE_RIGHT:
    				if(CURRENT_IMAGE_SHOWN != IMAGE_RIGHT)
    					currentImage = grabSprite(3,100,28,46,pSprites);
    					CURRENT_IMAGE_SHOWN = 3;
    				break;
    		}
    	}
    So, the problems seems to be that it having problem creating the first picture after the players presses the key, after adding some debugging prints i got these results from outputting the currentImage width and height:

    Java Code:
    Width: -1
    Height: -1
    
    Width: 28
    Height: 46
    
    Width: -1
    Height: -1
    
    Width: -1
    Height: -1
    
    Width: 28
    Height: 46
    This was when i spamclicked left and right arrow key.

    The method for "finding the right sprite" is the following

    Java Code:
    	public Image grabSprite(int x, int y, int width, int height, Image Sprite){
    		Image sprite;
    		ImageFilter filt=new CropImageFilter(x,y,width,height);
    		FilteredImageSource pro=new FilteredImageSource(pSprites.getSource(),filt);
    		sprite=Toolkit.getDefaultToolkit().createImage(pro);
    		return sprite;
    	}
    And yeh, ive been trying diffrent things in several hours, but nothing seem to work.
    My draw method in the player class is fairly simple aswell, maybe that may be the problem:

    Java Code:
    public void draw(Graphics g){
    		g.drawImage(pSprites, pPosX, pPosY, null);
    		System.out.println("Width: " +currentImage.getWidth(null) + "\nHeight: "+ currentImage.getHeight(null)+ "\n");
    		g.dispose();
    	}

    But yeh, i'm completly clueless.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Flickering on a 2D Sprite

    Can you make a small program that compiles, executes and shows the problem for testing?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Flickering on a 2D Sprite

    After yet few hours i still cant find the bug which causes this, so ill upload a jar file with the game here.
    http://driiper.com/~per/Game-Learning3.jar

    Thanks!

    Driiper

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Flickering on a 2D Sprite

    Can you make a small program that compiles, executes and shows the problem for testing?
    I'm not interested in going to a third site for the whole program.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Flickering on a 2D Sprite

    Seems like a friend of mine found the problem, it was the Sprite image which wasnt DoubleBuffered, and the caused the flickering when changing. But thanks anyways!


    Driiper

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Flickering on a 2D Sprite

    If you use Swing components they use double buffering.
    DarrylBurke likes this.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 02-29-2012, 09:38 PM
  2. Item Flickering
    By vincy in forum CLDC and MIDP
    Replies: 0
    Last Post: 10-25-2011, 07:48 AM
  3. import sprite
    By Bimz in forum New To Java
    Replies: 0
    Last Post: 08-28-2011, 12:15 PM
  4. Confisuion about sprite.getX() and sprite.getY()
    By Basit781 in forum Java Gaming
    Replies: 0
    Last Post: 01-10-2011, 06:16 AM
  5. Applet flickering
    By samson in forum Java 2D
    Replies: 3
    Last Post: 09-21-2007, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •