简体   繁体   English

XNA spriteBatch.Draw-闪烁的精灵

[英]XNA spriteBatch.Draw - flickering sprites

I'm writing sprite animation in XNA. 我正在XNA中编写精灵动画。 I use spriteBatch.Draw method to do this: 我使用spriteBatch.Draw方法来做到这一点:

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. 我没有在第一个和第二个Rectangle构造函数参数中通过numerOfWaterSpriteFrames调制 currentFrame 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. 您得到的是空白帧而不是崩溃的事实表明,无论是通过动画算法的结构还是通过运气,多余的帧都在Spritesheet中。

I'd need to see the animation code to be sure what's happening, though. 不过,我需要查看动画代码以确保发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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