gpt4 book ai didi

c# - 由 C# 参数通过引用而不是值传递引起的动画问题,但是在哪里?

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:01 25 4
gpt4 key购买 nike

我在 XNA 中遇到 Sprite 动画问题,这似乎是由作为引用值传递的结构引起的。但我没有在任何地方使用 ref 关键字。诚然,我是一个 C# 菜鸟,所以这里可能有一些浅薄的愚蠢错误,但我看不到。

我正在创建 10 只 Ant 或蜜蜂,并在它们在屏幕上移动时制作动画。我有一个动画结构数组,每次我创建一只 Ant 或蜜蜂时,我都会向它发送它需要的动画数组值(此时只是 [0] 或 [1])。动画结构的深处是一个用于更改帧的计时器。 ant/bee 类将动画结构存储为私有(private)变量。

我看到的是每只 Ant 或蜜蜂都使用相同的动画结构,我认为我正在传入并按值复制的那个。因此,在 Update() 期间,当我为每只 Ant /蜜蜂提前动画计时器时,下一只 Ant /蜜蜂的动画计时器会提前一小段时间。如果屏幕上有 1 只 Ant ,它会正确地设置动画。 2只 Ant ,它跑得快一倍,以此类推。显然,这不是我想要的。

这是代码的简化版本。 BerryPicking 的 ActorAnimationGroupData[] 如何在 BerryCreatures 之间共享?

class BerryPicking
{
private ActorAnimationGroupData[] animations;
private BerryCreature[] creatures;
private Dictionary<string, Texture2D> creatureTextures;
private const int maxCreatures = 5;

public BerryPickingExample()
{
this.creatures = new BerryCreature[maxCreatures];
this.creatureTextures = new Dictionary<string, Texture2D>();
}

public void LoadContent()
{
// Returns data from an XML file
Reader reader = new Reader();
animations = reader.LoadAnimations();

CreateCreatures();
}

// This is called from another function I'm not including because it's not relevant to the problem.
// In it, I remove any creature that passes outside the viewport by setting its creatures[] spot to null.
// Hence the if(creatures[i] == null) test is used to recreate "dead" creatures. Inelegant, I know.
private void CreateCreatures()
{
for (int i = 0; i < creatures.Length; i++)
{
if (creatures[i] == null)
{
// In reality, the name selection is randomized
creatures[i] = new BerryCreature("ant");

// Load content and texture (which I create elsewhere)
creatures[i].LoadContent(
FindAnimation(creatures[i].Name),
creatureTextures[creatures[i].Name]);
}
}
}

private ActorAnimationGroupData FindAnimation(string animationName)
{
int yourAnimation = -1;

for (int i = 0; i < animations.Length; i++)
{
if (animations[i].name == animationName)
{
yourAnimation = i;
break;
}
}

return animations[yourAnimation];
}

public void Update(GameTime gameTime)
{
for (int i = 0; i < creatures.Length; i++)
{
creatures[i].Update(gameTime);
}
}
}

class Reader
{
public ActorAnimationGroupData[] LoadAnimations()
{
ActorAnimationGroupData[] animationGroup;
XmlReader file = new XmlTextReader(filename);

// Do loading...

// Then later
file.Close();
return animationGroup;
}
}

class BerryCreature
{
private ActorAnimation animation;
private string name;

public BerryCreature(string name)
{
this.name = name;
}

public void LoadContent(ActorAnimationGroupData animationData, Texture2D sprite)
{
animation = new ActorAnimation(animationData);
animation.LoadContent(sprite);
}

public void Update(GameTime gameTime)
{
animation.Update(gameTime);
}
}

class ActorAnimation
{
private ActorAnimationGroupData animation;

public ActorAnimation(ActorAnimationGroupData animation)
{
this.animation = animation;
}

public void LoadContent(Texture2D sprite)
{
this.sprite = sprite;
}

public void Update(GameTime gameTime)
{
animation.Update(gameTime);
}
}

struct ActorAnimationGroupData
{
// There are lots of other members of this struct, but the timer is the only one I'm worried about.
// TimerData is another struct
private TimerData timer;

public ActorAnimationGroupData()
{
timer = new TimerData(2);
}

public void Update(GameTime gameTime)
{
timer.Update(gameTime);
}
}

struct TimerData
{
public float currentTime;
public float maxTime;

public TimerData(float maxTime)
{
this.currentTime = 0;
this.maxTime = maxTime;
}

public void Update(GameTime gameTime)
{
currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (currentTime >= maxTime)
{
currentTime = maxTime;
}
}
}

最佳答案

您确定每个生物都使用了新的 ActorAnimation 吗?我将创建一个包含两个生物的测试,并为每个生物的 animation.animation 值设置监视,并逐步执行更新以查看它们何时发生变化。您的 Updates() 是否可能被多次调用?

关于c# - 由 C# 参数通过引用而不是值传递引起的动画问题,但是在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3268094/

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