【问题标题】:Game project not launching after building in XNA在 XNA 中构建后游戏项目未启动
【发布时间】:2014-02-10 20:14:16
【问题描述】:

我是 XNA 游戏开发的新手,目前正在练习制作游戏。

我从创建一个简单的平台游戏开始,然后慢慢构建游戏的逻辑和游戏功能。我最近的添加似乎破坏了这个项目。它是在添加健康条功能后开始的,我在其中添加了 11 个纹理(健康条为空、健康条 1/10、健康条 2/10 等...)。

当玩家在游戏中遇到诸如尖刺之类的有害物体时,生命条显示的纹理会发生变化(因此如果生命条为 9,则纹理将变为 healthBar8)。

该问题最初是由于在两个不同的 Visual Studio 窗口中打开了两个相同的项目而导致无法构建项目的实际错误。 (这对我来说太愚蠢了,在发现我打开了两个窗口之前,我搜索了如何修复给定的错误)。建议的修复是在预构建命令行框中输入这两行:

如果存在 "$(TargetPath).locked" del "$(TargetPath).locked" 如果存在 "$(TargetPath)" 如果不存在 "$(TargetPath).locked" 移动 "$(TargetPath)" "$(TargetPath).locked"

这似乎已经修复了错误,但现在我的项目只构建并且没有启动,所以我关闭了我发现我已经打开的额外窗口并从预构建命令行框中删除了这两行,但现在项目只是构建而不启动。

我尝试创建一个新项目,并检查它是否构建(它确实构建了,所以 Visual Studio 本身不是问题)然后将损坏的项目的代码复制并粘贴到这个新项目中,但仍然没有启动。

我不知道是什么原因造成的,这非常烦人。如果我的问题表述不佳,我真的很抱歉,因为这是我的第一个 Stack 溢出问题(因为我从来没有觉得有必要在互联网上实际发布问题),但经过大量谷歌搜索后我找不到解决方案。

public class MainGame : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteFont coinCount;

    List<PlatformSprite> platformsList;
    List<Consumable> pickUpList;
    List<StaticEnemy> staticEnemyList;

    //public Texture2D currentHealthTexture;
    //Texture2D healthEmpty;
    //Texture2D health1;
    //Texture2D health2;
    //Texture2D health3;
    //Texture2D health4;
    //Texture2D health5;
    //Texture2D health6;
    //Texture2D health7;
    //Texture2D health8;
    //Texture2D health9;
    //Texture2D healthFull;

    //List<Texture2D> healthBarsList;

    Texture2D coinAnim;
    Texture2D coinAnimBig;
    Texture2D floatingPlatform;
    Texture2D groundPlatform;
    Texture2D redPlayer;
    Texture2D spikeEnemy;

    Rectangle destRect;
    Rectangle sourceRect;
    Rectangle destRectSmall;
    Rectangle sourceRectSmall;

    PlayerSprite player;
    KeyboardState keyboardState;

    float elapsed;
    float delay = 90f;
    int frameCount = 0;

    public const int screenHeight = 550;
    public const int screenWidth = 800;

    public MainGame()
    {
        graphics = new GraphicsDeviceManager(this);

        graphics.PreferredBackBufferHeight = screenHeight;
        graphics.PreferredBackBufferWidth = screenWidth;

        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        destRect = new Rectangle(100, 100, 60, 60);
        destRectSmall = new Rectangle(100, 300, 20, 20);
        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        coinCount = Content.Load<SpriteFont>("CoinCount");

        platformsList = new List<PlatformSprite>();
        pickUpList = new List<Consumable>();
        //healthBarsList = new List<Texture2D>();
        staticEnemyList = new List<StaticEnemy>();

        coinAnim = Content.Load<Texture2D>("CoinSpinAnim");
        coinAnimBig = Content.Load<Texture2D>("CoinAnim");
        floatingPlatform = Content.Load<Texture2D>("Platform1");
        groundPlatform = Content.Load<Texture2D>("GroundPlatform1");
        redPlayer = Content.Load<Texture2D>("TestSprite");
        spikeEnemy = Content.Load<Texture2D>("Spike");

        //healthEmpty = Content.Load<Texture2D>("HealthBarEmpty");
        //health1 = Content.Load<Texture2D>("HealthBar1");
        //health2 = Content.Load<Texture2D>("HealthBar2");
        //health3 = Content.Load<Texture2D>("HealthBar3");
        //health4 = Content.Load<Texture2D>("HealthBar4");
        //health5 = Content.Load<Texture2D>("HealthBar5");
        //health6 = Content.Load<Texture2D>("HealthBar6");
        //health7 = Content.Load<Texture2D>("HealthBar7");
        //health8 = Content.Load<Texture2D>("HealthBar8");
        //health9 = Content.Load<Texture2D>("HealthBar9");
        //healthFull = Content.Load<Texture2D>("HealthBarFull");
        //healthBarsList.Add(healthEmpty);
        //healthBarsList.Add(health1);
        //healthBarsList.Add(health2);
        //healthBarsList.Add(health3);
        //healthBarsList.Add(health4);
        //healthBarsList.Add(health5);
        //healthBarsList.Add(health6);
        //healthBarsList.Add(health7);
        //healthBarsList.Add(health8);
        //healthBarsList.Add(health9);
        //healthBarsList.Add(healthFull);

        platformsList.Add(new PlatformSprite(groundPlatform, new Vector2(0, 454)));
        platformsList.Add(new PlatformSprite(floatingPlatform, new Vector2(500, 330)));
        platformsList.Add(new PlatformSprite(floatingPlatform, new Vector2(100, 290)));
        platformsList.Add(new PlatformSprite(floatingPlatform, new Vector2(300, 250)));
        platformsList.Add(new PlatformSprite(floatingPlatform, new Vector2(170, 250)));
        platformsList.Add(new PlatformSprite(floatingPlatform, new Vector2(350, 360)));

        staticEnemyList.Add(new StaticEnemy(spikeEnemy, platformsList[0], 300));

        pickUpList.Add(new Consumable(coinAnim, platformsList[1], 30, sourceRectSmall));
        pickUpList.Add(new Consumable(coinAnim, platformsList[3], 45, sourceRectSmall));
        pickUpList.Add(new Consumable(coinAnim, platformsList[4], 60, sourceRectSmall));

        player = new PlayerSprite(redPlayer, platformsList, pickUpList, staticEnemyList, 0);
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        keyboardState = Keyboard.GetState();

        //currentHealthTexture = healthBarsList[player.playerHealth];

        player.Update(keyboardState);

        elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

        if (elapsed >= delay)
        {
            if (frameCount >= 5)
            {
                frameCount = 0;
            }
            else
            {
                frameCount++;
            }

            elapsed = 0;
        }

        sourceRect = new Rectangle(60 * frameCount, 0, 60, 60);
        sourceRectSmall = new Rectangle(20 * frameCount, 0, 20, 20);

        foreach (Consumable c in pickUpList)
        {
            c.Update();
        }

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        foreach (PlatformSprite p in platformsList)
        {
            p.Draw(spriteBatch);
        }

        foreach (Consumable c in pickUpList)
        {
            c.Draw(spriteBatch, sourceRectSmall);
        }

        foreach (StaticEnemy se in staticEnemyList)
        {
            se.Draw(spriteBatch);
        }

        //spriteBatch.Draw(currentHealthTexture, new Vector2(680, 10), Color.White);

        spriteBatch.DrawString(coinCount, ":  " + player.playerCoinCount, new Vector2(30, 10), Color.White);
        spriteBatch.Draw(coinAnim, new Rectangle(10, 10, 20, 20), new Rectangle(0, 0, 20, 20), Color.White);


        player.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

如您所见,我已经注释掉了所有的健康条形码,我希望这会为我的问题增加细节,但如果这是一个糟糕的问题,我真的很抱歉,我只是不知道该做什么或从哪里开始修复它。

临时更新 - 已修复!! :D

我将其缩小到一个导致此“构建但未启动”错误的 while 循环。我尝试了几次将循环注释掉,然后再次将它放回桌面,每次我注释掉它时,它都会成功启动游戏。我在我的手机上知道所以我为没有引用确切的循环而道歉,但它是一个while循环,比如: 整数 j:0; 而 (j

我认为这与将敌人列表索引设置为“i”而不是 j 相关,但这并没有返回错误,因为在此之前我有一个基于名为“i”的整数的循环。仍然不知道为什么这个while循环会破坏视觉工作室,有什么建议会很酷吗?再次为我糟糕的格式等感到抱歉,一旦我上电脑,我将使用实际的 while 循环代码进行更新。感谢所有评论的人,很好的建议 - 再次......对不起 - 真的很难在电话上发表评论,我无法在 8 小时内回复,这只是暂时的 :)

【问题讨论】:

  • 呃哦 yoy 格式如何
  • 您是否尝试将其关闭再打开?
  • 不妨试试,其他都没用。
  • 主游戏循环中发生的事情太多了。另外,我会研究 MonoGame,它仍然受支持 - 与 XNA 不同。
  • 顺便说一句,由于您仍在学习,我是否可以建议将所有健康条图像放在一个纹理文件中,然后使用不同的源矩形绘制正确的图像?

标签: c# visual-studio-2010 xna


【解决方案1】:

由于我错误地创建了一个愚蠢的无限 while 循环,项目已构建但没有出现。这是通过创建几个不同的整数而不使用正确的整数及其相应的 while 循环来实现的。一个很容易解决的简单问题,但对于刚接触 C# 的人来说,这似乎真的很吓人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多