以Monogame

时间:2017-12-03 13:41:07

标签: c# monogame

我正在尝试使用不同的颜色为我的游戏绘制一个Rectangle对象,因此看起来它已经打开了。

基本上,当我运行程序时,它应该将门呈现为灰色矩形,然后在10秒后切换颜色。然后他们应该'关闭',在打开3秒后恢复原来的颜色。但是,它们从错误的颜色开始,然后根本不改变。

我研究了使用状态机确定门对象是打开还是关闭,然后调用Draw()中的方法将其绘制为蓝色矩形或灰色矩形。

我对_doorStates的枚举:

enum doorState
    {
        Open,
        Closed,
    }

doorState[] _doorStates = new doorState[50];

这是我在Update()中的代码,用于确定是否需要打开门。我有一个门对象数组和一个_doorStates数组。我知道这不是最有效的方法,但改变它有点晚了!

for (int i = 0; i < doorNumber; i++)
        {
            if (doors[i].doorState == false)//if the object's bool value says it is closed
            {
                if (timeElapsed % doors[i].doorOpenRate == 0 && timeElapsed >= doors[i].doorOpenRate)//is it time for the door to open?
                {
                    doors[i].doorState = true;

                    _doorStates[i] = doorState.Open;

                    timeOpened = timeElapsed;
                }
            }

            else
            {
                if (timeElapsed - timeOpened == 3)//has it been open for 3 seconds?
                {
                    doors[i].doorState = false;

                    _doorStates[i] = doorState.Closed;
                }
            }
        }

这是Draw()方法中的代码:

for (int i = 0; i < doorNumber; i++)
        {
            switch(_doorStates[i])
            {
                case doorState.Open:
                    drawOpenDoor(doors[i], gameTime);
                    break;
                case doorState.Closed:
                    drawClosedDoor(doors[i], gameTime);
                    break;
            }
        }

这些是我单独的绘制方法:

    private void drawOpenDoor(door door, GameTime deltaTime)
    {
        spriteBatch.Draw(rect, door.doorObject, Color.CornflowerBlue);
    }

    private void drawClosedDoor(door door, GameTime deltatime)
    {
        spriteBatch.Draw(rect, door.doorObject, Color.DarkGray);
    }

这是门对象的构造函数:

public door (int xPos, int yPos, int width, int height, int orientation, int openRate, int openDuration)
    {
        if (orientation == 0)
        {
            doorObject = new Rectangle(xPos, yPos, width, height);
        }

        else
        {
            doorObject = new Rectangle(xPos + 15, yPos, height, width);
        }

        doorOrientation = orientation;//horizontal = 0, vertical = 1
        doorOpenRate = openRate;
        doorOpenDuration = openDuration;
        doorState = false;
    }

这是我的第一篇文章,如果您需要更多信息,请告知我们这些课程的工作方式。我使用this post来创建状态机。

0 个答案:

没有答案