Results 1 to 6 of 6
Thread: Flickering on a 2D Sprite
- 05-06-2012, 07:35 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 20
- Rep Power
- 0
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:
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: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; } }
This was when i spamclicked left and right arrow key.Java Code:Width: -1 Height: -1 Width: 28 Height: 46 Width: -1 Height: -1 Width: -1 Height: -1 Width: 28 Height: 46
The method for "finding the right sprite" is the following
And yeh, ive been trying diffrent things in several hours, but nothing seem to work.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; }
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.
- 05-06-2012, 01:04 PM #2
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.
- 05-06-2012, 04:19 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 20
- Rep Power
- 0
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
- 05-06-2012, 04:28 PM #4
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.
- 05-06-2012, 05:28 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 20
- Rep Power
- 0
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
- 05-06-2012, 05:30 PM #6
Similar Threads
-
Flickering, need help finding a way to get it out
By Atynine in forum Java 2DReplies: 3Last Post: 02-29-2012, 09:38 PM -
Item Flickering
By vincy in forum CLDC and MIDPReplies: 0Last Post: 10-25-2011, 07:48 AM -
import sprite
By Bimz in forum New To JavaReplies: 0Last Post: 08-28-2011, 12:15 PM -
Confisuion about sprite.getX() and sprite.getY()
By Basit781 in forum Java GamingReplies: 0Last Post: 01-10-2011, 06:16 AM -
Applet flickering
By samson in forum Java 2DReplies: 3Last Post: 09-21-2007, 10:51 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks