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

时间:2010-07-16 19:11:52

标签: c# parameters reference xna

我遇到了XNA中的精灵动画问题,这似乎是由作为参考值传递的结构引起的。但我没有在任何地方使用 ref 关键字。不可否认,我是C#noob,因此这里可能会出现一些浅的骨头错误,但我看不到它。

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

我所看到的是每个蚂蚁或蜜蜂使用相同的动画结构,我认为我传入的那个并按值复制。所以在Update()期间,当我为每个蚂蚁/蜜蜂推进动画计时器时,下一个蚂蚁/蜜蜂的动画计时器会提前少量。如果屏幕上有1个蚂蚁,它会正常动画。 2只蚂蚁,运行速度快两倍,依此类推。显然,不是我想要的。

这是代码的删节版本。 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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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