gpt4 book ai didi

c# - XNA:未调用基本 DrawableGameComponent 覆盖函数(更新、绘制等)

转载 作者:太空狗 更新时间:2023-10-30 00:06:25 25 4
gpt4 key购买 nike

我试图通过让它们扩展 Mircosoft.Xna.Framework.GameCompenent 来封装我的游戏对象,然后仅在 Game1 的 Update() 方法中构建和管理它们。我有我的 Game1 类、一个玩家类和一个动画类。动画应该管理一个对象的 Texture2D 变化,在这个例子中是 Player。

我的问题是,即使我已经成功地扩展了所有内容,没有语法错误,没有抛出异常,并且检查并重新检查了我编写的少量代码,但没有调用覆盖函数,我最终得到了一个黑屏。

Game1.cs:(请注意,仅有两行更改用于玩家声明)

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Player player;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
player = new Player(this);

base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

protected override void UnloadContent()
{

}

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
base.Draw(gameTime);
}
}

播放器.cs:

class Player : Microsoft.Xna.Framework.DrawableGameComponent
{
Rectangle bounds;
Texture2D t;
Animation[] animations = new Animation[4];
String path = @"..\..\..\Content\player.png";

#region Animation Constants
private const int WALK_RIGHT = 0;
#endregion

SpriteBatch spriteBatch;

public Player(Game game) : base(game)
{
//should only ever be one player, all value defaults set in Initialize()
}

public Texture2D T
{
get { return t; }
}

public Rectangle Bounds
{
get { return bounds; }
}

public override void Initialize()
{
base.Initialize();

bounds = new Rectangle(0, 0,
System.Drawing.Image.FromFile(path).Width,
System.Drawing.Image.FromFile(path).Height
);

t = Game.Content.Load<Texture2D>("player");
animations[0] = new Animation(this.Game, "player", "walking", 3);
}

protected override void LoadContent()
{
base.LoadContent();

spriteBatch = new SpriteBatch(this.Game.GraphicsDevice);
}

public override void Update(GameTime gameTime)
{
base.Update(gameTime);

KeyboardState k = Keyboard.GetState();

if (k.IsKeyDown(Keys.Right)) //walk right
{
bounds.X += 3;
if (animations[WALK_RIGHT].Playing)
{
t = animations[WALK_RIGHT].getTexture();
}
else
{
animations[WALK_RIGHT].Play();
}
}
else if (animations[WALK_RIGHT].Playing)
animations[WALK_RIGHT].Stop();

}

public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);

spriteBatch.Begin();
spriteBatch.Draw(t, bounds, Color.White);
spriteBatch.End();
}
}

动画.cs:

class Animation : Microsoft.Xna.Framework.GameComponent
{
Game game;
String name; //name of default sprite; standing, unmoving, neutral, etc. The rest of the animation sprite names should derive from this
String keyword;
int frameCount;
int delay; //frames between texture change


String[] paths; //texture pathnames generated by the MakePaths() function
int currentFrame = 0;
int delayCount = 0;
bool playing = false;

public Animation(Game associatedGame, String nameVal, String keywordVal, int frameCountVal)
: base(associatedGame)
{
name = nameVal;
keyword = keywordVal;
frameCount = frameCountVal;
paths = MakePaths();
delay = 10;
}

public Animation(Game associatedGame, String nameVal, String keywordVal, int frameCountVal, int delayVal)
: base(associatedGame)
{
name = nameVal;
keyword = keywordVal;
frameCount = frameCountVal;
paths = MakePaths();
delay = delayVal;
}

private String[] MakePaths()
{
//format: name_keyword_anim[i]
//format example: player_walking_anim1

String[] temp = new String[frameCount];
for (int i = 0; i < frameCount; i++)
{
temp[i] = name + "_" + keyword + "_" + "anim" + i.ToString();
}

return temp;
}

public Texture2D getTexture()
{
return Game.Content.Load<Texture2D>(paths[currentFrame]);
}

public void Play()
{
playing = true;
}

public void Stop()
{
currentFrame = 0;
delayCount = 0;
playing = false;
}

public bool Playing
{
get { return playing; }
}

public override void Update(GameTime gameTime)
{
if (playing)
{
if (delayCount == delay)
{
delayCount = 0;

if ((currentFrame + 1) == frameCount) currentFrame = 0;
else currentFrame++;
}
else delayCount++;
}
base.Update(gameTime);
}

public override string ToString()
{
return "params: " + name + "," + keyword + "," + frameCount.ToString() + "\nUsing paths: " + paths;
}
}

唯一调用的 LoadContent、Initialize、Update 和 Draw 方法是 Game1 中的方法。真正让我感到困惑的是,我之前能够毫无问题地使用这种技术。 Xna 更新过程自然会调用这些函数。

那么...这是为什么?

最佳答案

您需要将游戏组件添加到组件集合中才能自动调用它们

protected override void Initialize()
{
player = new Player(this);
Components.Add(player);

base.Initialize();
}

参见 http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.components.aspx

关于c# - XNA:未调用基本 DrawableGameComponent 覆盖函数(更新、绘制等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3539897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com