简体   繁体   中英

XNA spriteBatch.Draw - flickering sprites

I'm writing sprite animation in XNA. I use spriteBatch.Draw method to do this:

spriteBatch.Draw(waterTexture, waterPosition, rectWater, Color.White * 1.0f, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
// 'rectWater' is rect used to cut sprite image from sprite texture

The problem is that it almowst works, but near end of animation it blinks once and continue animation. What could it be?

Ok, I figured out what was wrong.

The problem was, that I had wrong alghoritm to change frames, and it was sometimes skipping to blank frame. Look at the code:

Rectangle rectWater = new Rectangle(((currentFrame % numerOfWaterSpriteFrames) % horizontalNumerOfWaterSpriteFrames) * (int)sizeOfWaterFrame.X, ((aktualnaFramka % numerOfWaterSpriteFrames) / horizontalNumerOfWaterSpriteFrames) * (int)sizeOfWaterFrame.Y, (int)sizeOfWaterFrame.X, (int)sizeOfWaterFrame.Y);
//The correct code

I wasn't moduling the currentFrame by numerOfWaterSpriteFrames in first and second Rectangle constructor parameter. Just like this:

Rectangle rectWater = new Rectangle((currentFrame % horizontalNumerOfWaterSpriteFrames) * (int)sizeOfWaterFrame.X, (aktualnaFramka / horizontalNumerOfWaterSpriteFrames) * (int)sizeOfWaterFrame.Y, (int)sizeOfWaterFrame.X, (int)sizeOfWaterFrame.Y);
//The wrong code

It's ok now.

I can't really help much without seeing the code that's animating the sprite, but a blink at the same point in the animation every time suggests a dud frame to me. What's probably happening is you're drawing a blank frame at the end. A likely cause of this is a straightforward off-by-one error; if you're using a loop to iterate over the frames of your animation, then your index might be out by one (using >= instead of > , for example) which means you're drawing an extra frame that shouldn't be in there. The fact that you're getting a blank frame instead of a crash suggests that the extra frame falls within the spritesheet, either by the structure of your animation algorithm or by luck.

I'd need to see the animation code to be sure what's happening, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM