我是XNA游戏开发的新手,我目前正在练习制作游戏。
我开始创建一个简单的平台游戏,并慢慢建立游戏和游戏功能的逻辑。我最近的补充似乎打破了这个项目。它在添加健康栏功能后开始,我添加11个纹理(健康栏空,健康栏1/10,健康栏2/10等...)。
当玩家遇到诸如游戏中的尖峰等有害物体时,为健康栏显示的纹理会发生变化(因此,如果健康栏位于9,纹理将更改为healthBar8)。
该问题始于一个实际错误,阻止了项目的构建,这是因为在两个不同的Visual Studio窗口中打开了两个相同的项目。 (这对我来说太愚蠢了,我在搜索到我打开了两个窗口之前用Google搜索了如何修复给定的错误)。建议的修复是在预构建命令行框中输入这两行:
如果存在“$(TargetPath).locked”del“$(TargetPath).locked” 如果存在“$(TargetPath)”如果不存在“$(TargetPath).locked”move“$(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循环导致这个'build but not launch'错误。我尝试了几次评论循环,然后再将它重新放回桌面上,每次我评论它都成功启动了游戏。我正在通电话,所以我道歉没有引用确切的循环,但它是一个while循环,类似于: int j:0; while(j&lt; staticEnemyList.Count) { if(player.enclosedRectangle.Intersects(staticEnemyList [i] .GetRectangle)) { playerHealth-- } 其他 { J ++ } }
我认为它与将敌人列表索引设置为'i'而不是j有关,但这并没有返回错误,因为我在此基础上有一个循环,称为'i'。仍然不知道为什么这个while循环打破了视觉工作室,任何建议都会很酷?再次,我很抱歉我的可怕格式等,一旦我上电脑,我将使用实际的while循环代码进行更新。谢谢所有评论的人,很棒的建议 - 再说一遍......对不起 - 真的很难在电话上发表评论而且我无法在8小时内回答,这只是暂时的:)
答案 0 :(得分:0)
项目已经建成,但由于我错误地创建了一个愚蠢无限的循环而没有出现。这是通过创建几个不同的整数而不是使用正确的整数及其相应的while循环来实现的。一个简单的问题很容易解决,但对于那些不熟悉C#的人来说,这看起来真的太吓人了。