小编Chr*_*ris的帖子

如何在C#游戏中控制加速和减速

我正在建立一个驾驶游戏.视图是一个透视图,玩家的视角来自向前行驶的汽车后方.当汽车向前行驶时,它周围的所有环境都会向下移动并缩放(现在看起来相当不错),这给人的印象是汽车向前移动.现在,我希望有一些逼真的驾驶控制,这样汽车可以提高速度,然后在向上箭头释放时逐渐减速.目前,当按下向上箭头时,我会调用几个精灵的所有移动功能.我正在寻找一种方法来控制它,以便在汽车缓慢等时不会经常调用这些函数.到目前为止我的代码是:

   protected void Drive()
    {
        KeyboardState keyState = Keyboard.GetState();

        if (keyState.IsKeyDown(Keys.Up))
        {
            MathHelper.Clamp(++TruckSpeed, 0, 100);
        }
        else
        {
            MathHelper.Clamp(--TruckSpeed, 0, 100);
        }

        // Instead of using the condition below, I want to use the TruckSpeed
        // variable some way to control the rate at which these are called 
        // so I can give the impression of acceleration and de-acceleration.

        if (keyState.IsKeyDown(Keys.Up))
        {
            // Lots of update calls in here
        }
   }
Run Code Online (Sandbox Code Playgroud)

我认为这应该很容易,但出于某种原因,我无法理解它.非常感谢这里的一些帮助!谢谢

c# xna

6
推荐指数
1
解决办法
4434
查看次数

如何使用三元运算符增加值

我希望有人可以指出我哪里出错了?我没有太多使用三元运算符的经验,但觉得有充分的理由在这里使用它,因为它减少了代码长度.

但是,我收到了上述错误.任何人都可以看到我错在哪里?

Colour.R <= 255 ? Colour.R+=10 : Colour.R+=11;
Run Code Online (Sandbox Code Playgroud)

编译器返回

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

其中颜色代表颜色矢量.

c# ternary-operator

0
推荐指数
1
解决办法
910
查看次数

XNA经过的时间没有更新

我想在我的XNA游戏中实现一个简单的计数器.认为这很简单.我有以下代码:

    elapsed = gameTime.ElapsedGameTime.TotalMilliseconds;
        timer -= (int)elapsed;

        if (timer <= 0)
        {
            timer = 10;   //Reset Timer
        }
Run Code Online (Sandbox Code Playgroud)

但过去从未改变过0.0.我错过了一些明显的东西吗?我怀疑我.我将gameTime声明在顶部并像往常一样初始化.

如上所述,这里有更多代码:

公共类Game1:Microsoft.Xna.Framework.Game {

private GameTime zombieTime; 



    public Game1()
    {
        zombieTime = new GameTime();
        // Other (unrelated) stuff here
    }



    protected void AddZombie()
    {
        elapsed = zombieTime.ElapsedGameTime.TotalMilliseconds;
        timer -= (int)elapsed;

        if (timer <= 0)
        {
            timer = 10;   //Reset Timer
            Zombie zombie = new Zombie(ScreenWidth, ScreenHeight, random);
            zombie.LoadContent(this.Content, "ZombieSprites/ZombieLeft1");
            zombies.Insert(0, zombie);
        }
     }

    protected void Update()
    {
        AddZombie();
        // Other game update …
Run Code Online (Sandbox Code Playgroud)

c# xna

0
推荐指数
1
解决办法
1634
查看次数

标签 统计

c# ×3

xna ×2

ternary-operator ×1