C# XNA-水平移动动画矩形(无用户输入,但定时器)
在这里,我想移动这个动画矩形水平没有用户输入,使用定时器,也许?动画矩形应左右移动(从起点到终点,整个路径=315像素)。我指的是点1(从左边开始)和点2(从右边结束)之间的315像素。 这是我的密码: 变量:C# XNA-水平移动动画矩形(无用户输入,但定时器),c#,timer,xna,rectangles,C#,Timer,Xna,Rectangles,在这里,我想移动这个动画矩形水平没有用户输入,使用定时器,也许?动画矩形应左右移动(从起点到终点,整个路径=315像素)。我指的是点1(从左边开始)和点2(从右边结束)之间的315像素。 这是我的密码: 变量: //Sprite Texture Texture2D texture; //A Timer variable float timer = 0f; //The interval (300 millisecond
//Sprite Texture
Texture2D texture;
//A Timer variable
float timer = 0f;
//The interval (300 milliseconds)
float interval = 300f;
//Current frame holder (start at 1)
int currentFrame = 1;
//Width of a single sprite image, not the whole Sprite Sheet
int spriteWidth = 32;
//Height of a single sprite image, not the whole Sprite Sheet
int spriteHeight = 32;
//A rectangle to store which 'frame' is currently being shown
Rectangle sourceRect;
//The centre of the current 'frame'
Vector2 origin;
接下来,在LoadContent()
方法中:
//Texture load
texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");
//Increase the timer by the number of milliseconds since update was last called
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
//Check the timer is more than the chosen interval
if (timer > interval)
{
//Show the next frame
currentFrame++;
//Reset the timer
timer = 0f;
}
//If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
currentFrame = currentFrame % 2;
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
//Texture Draw
spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
最后是Draw()
方法:
//Texture load
texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");
//Increase the timer by the number of milliseconds since update was last called
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
//Check the timer is more than the chosen interval
if (timer > interval)
{
//Show the next frame
currentFrame++;
//Reset the timer
timer = 0f;
}
//If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
currentFrame = currentFrame % 2;
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
//Texture Draw
spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
所以我想移动
sourceRect
。Ir必须左循环和右循环。您的spriteBatch.Draw()
调用将继续对目标矩形使用相同的值。你想让它怎么移动?例如,每帧右侧5个像素?只需将您的destinationRectangle
(第二个参数为spriteBatch.Draw()
)更改为:
编辑:从注释中请求的示例
class Foo
{
int x;
int maxX = 400;
void Update(GameTime gameTime)
{
if (x++ >= maxX)
x = 263;
}
void Draw()
{
spriteBatch.Draw(texture, new Vector2(263 + x, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
}
}
当然,除了spriteBatch.Draw()调用之外,您将保留所有当前代码;您只需添加x
部分。不,动画没问题,但我需要矩形移动,而不是在一个位置上进行动画。什么是“new Vector(263554)”参数?它显示矩形的位置。我相信,这是您应该更新的。不过,我已经有一段时间没有使用XNA了。它只是在所需的点之间闪烁,我想看看它在设置动画时如何从点1移动到点2(不闪烁)。然而,这是有用的。请回复。您可以创建一个名为x
的字段,在Update()
函数中递增该字段。然后你可以用它来偏移你的目标矩形
:新向量2(263+x554)代码>我是新来的,所以我可以举个例子吗?好的,谢谢,但我需要它循环,当它到达第2点时,它应该返回到第1点并重复。请回答。我已经按照要求再次编辑了我的答案。基本上,我引入了maxX
字段。