在单人游戏中绘制动画时遇到问题
本文关键字:遇到 问题 动画 绘制 单人 游戏 | 更新日期: 2023-09-27 17:52:12
我正在尝试创建一个具有多个动画(空闲,攻击,损坏等)的太空船但是当我试图改变动画时,我看到当前动画的第一帧在"旧"位置(动画最后一次被改变的地方)的瞬间
图片说明:https://i.stack.imgur.com/1BgNK.png
红色矩形应该是生命条
运行动画的类:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Test
{
class Animation
{
Texture2D spriteStrip;
float scale;
int elapsedTime;
int frameTime;
int frameCount;
int currentFrame;
Color color;
// The area of the image strip we want to display
Rectangle sourceRect = new Rectangle();
// The area where we want to display the image strip in the game
Rectangle destinationRect = new Rectangle();
public int FrameWidth;
public int FrameHeight;
public bool Active;
public bool Looping;
public Vector2 Position;
public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight, int frameCount, int frametime, Color color, float scale, bool looping)
{
this.color = color;
this.FrameWidth = frameWidth;
this.FrameHeight = frameHeight;
this.frameCount = frameCount;
this.frameTime = frametime;
this.scale = scale;
Looping = looping;
Position = position;
spriteStrip = texture;
elapsedTime = 0;
currentFrame = 0;
Active = true;
}
public void Update(GameTime gameTime)
{
if (Active == false) return;
elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsedTime > frameTime)
{
currentFrame++;
if (currentFrame == frameCount)
{
currentFrame = 0;
if (Looping == false)
Active = false;
}
elapsedTime = 0;
}
sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2, (int)Position.Y - (int)(FrameHeight * scale) / 2, (int)(FrameWidth * scale), (int)(FrameHeight * scale));
}
public void Draw(SpriteBatch spriteBatch)
{
if (Active)
{
spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
}
}
}
}
我写整个代码的原因是因为我不知道问题在哪里。在我的Game1.cs类我有:
全局变量:
Texture2D playerTexture;
Texture2D playerShootingTexure;
Animation playerAnimation = new Animation();
Animation ShootingAnimation = new Animation();
…
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
playerTexture = Content.Load<Texture2D>("Graphics/Player/PlayerShip");
playerShootingTexure = Content.Load<Texture2D>("Graphics/Player/Player_Shooting");
playerAnimation.Initialize(playerTexture, Vector2.Zero, playerTexture.Width / 5, playerTexture.Height, 5, 50, Color.Wheat, 1f, true);
ShootingAnimation.Initialize(playerShootingTexure, Vector2.Zero, playerShootingTexure.Width / 5, playerShootingTexure.Height, 5, 50, Color.WhiteSmoke, 0.75f, true);
}
我改变动画的地方:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Start drawing
spriteBatch.Begin();
if (Player_Attacking == false)
{
player.PlayerAnimation = playerAnimation;
}
else
{
player.PlayerAnimation = ShootingAnimation;
}
player.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
注意:player_attacks是一个布尔变量。当按下'Space'时,变量的值变为TRUE,当释放'Space'时,值变为FALSE
请帮忙!提前感谢!很抱歉有一大段文字
我对确切的问题有点不清楚,所以如果我不理解,也许你可以帮助澄清。
但是根据你的图片,似乎当你按空格键射击时,玩家动画应该改变为用蓝色火焰照亮他的枪(或者它是:)),看起来它正在这样做,但它在错误的位置(在生命条上方-我看到它正在射击的激光在正确的位置:))
但一旦拍摄完成,它就会自我纠正,如第二张照片所示(不再有蓝色火焰)。
所以这意味着玩家射击的动画使用了错误的位置。
你:- 在玩家更新中更新玩家射击动画的位置?
- 调用你的球员射击动画的
Update
在你的球员更新?
你应该在你的播放器Update
PlayerAnimation.Position = Position;
PlayerAnimation.Update(gameTime);
同时,尝试在Game1.cs的Update
中移动检查玩家动画:
protected override void Update(GameTime gameTime)
{
if (Player_Attacking == false)
{
player.PlayerAnimation = playerAnimation;
}
else
{
player.PlayerAnimation = ShootingAnimation;
}
player.Update(gameTime);
}