I'm brand new to XNA games development and am currently practising to make games.
我是XNA游戏开发的新手,我目前正在练习制作游戏。
I started by creating a simple platformer, and slowly built up the logic of the game and game features. My most recent addition seems to have broken the project. It started after adding a health bar function, where I add 11 textures (health bar empty, health bar 1/10, health bar 2/10 etc...).
我开始创建一个简单的平台游戏,并慢慢建立游戏和游戏功能的逻辑。我最近的补充似乎打破了这个项目。它在添加健康栏功能后开始,我添加了11个纹理(健康栏空,健康栏1/10,健康栏2/10等...)。
When the player encounters a harmful object such as a spike in the game, the texture displayed for the health bar change (so if health bar was on 9, the texture would change to healthBar8).
当玩家遇到诸如游戏中的尖刺等有害物体时,为健康栏显示的纹理会发生变化(因此,如果健康栏位于9,则纹理将更改为healthBar8)。
The issue started off as an actual error preventing the project to build which was caused by having two of the same project open in two different Visual Studio windows. (This was so stupid of me and I googled how to fix the given error before finding out that I had two windows open). The suggested fix was to enter these two lines in the pre-build command line box:
该问题始于一个实际错误,阻止了项目的构建,这是因为在两个不同的Visual Studio窗口中打开了两个相同的项目。 (这对我来说太愚蠢了,我在搜索到我打开两个窗口之前用谷歌搜索了如何修复给定的错误)。建议的修复方法是在预构建命令行框中输入以下两行:
if exist "$(TargetPath).locked" del "$(TargetPath).locked" if exist "$(TargetPath)" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
如果存在“$(TargetPath).locked”del“$(TargetPath).locked”如果存在“$(TargetPath)”如果不存在“$(TargetPath).locked”move“$(TargetPath)”“$(TargetPath) .locked”
This seemed to have fixed the error but now my project only builds and doesn't launch, so I closed the extra window I discovered I had open and removed the two lines from the pre-build command line box, but now the project just builds and doesn't launch.
这似乎修复了错误但现在我的项目只构建并且没有启动,所以我关闭了额外的窗口,我发现我打开并从预构建命令行框中删除了两行,但现在项目刚刚构建而且不会发布。
I tried creating a new project, and checked to see if it built (it did, so Visual Studio itself is not the problem) then copied and pasted the broken project's code into this new project, which still didn't launch.
我尝试创建一个新项目,并检查它是否构建(确实如此,因此Visual Studio本身不是问题)然后将已损坏项目的代码复制并粘贴到这个仍未启动的新项目中。
I have no idea what is causing this and it's extremely annoying. I'm really sorry if my question is stated poorly as this is my first Stack overflow question (because I have never felt the need to actually post question on the internet), but I can't find a solution after extensive googling.
我不知道造成这种情况的原因是什么,而且非常烦人。我真的很抱歉,如果我的问题陈述不好,因为这是我的第一个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);
}
}
As you can see I've commented out all of the health bar code, I hope this adds detail to my question but really sorry if this is a terrible question I just have no clue what to do or where to start to fix it.
正如你所看到的,我已经注释掉了所有的健康条形码,我希望这能为我的问题增加细节,但是如果这是一个可怕的问题我真的很抱歉我不知道该做什么或从哪里开始修复它。
TEMPORARY UPDATE - FIXED IT!! :D
临时更新 - 固定它! :d
I narrowed it down to one single while loop that caused this 'build but not launch' error. I tried a few times commenting the loop out and then bringing it back on the table again and every time I commented it out it launched the game successfully. I'm on my phone right know so I apologise for not quoting the exact loop yet but it was a while loop, something like: int j :0; while (j < staticEnemyList.Count) { if(player.enclosedRectangle.Intersects (staticEnemyList[i].GetRectangle)) { playerHealth-- } else { j++ } }
我把它缩小到一个while循环导致这个'build but not launch'错误。我尝试了几次评论循环,然后再次将它重新放回桌面上,每次我评论它都成功启动了游戏。我正在打电话知道所以我道歉没有引用确切的循环但它是一个while循环,类似于:int j:0; while(j
I think it has something to do with setting the enemy list index as 'i' instead of j but this didn't return an error as I had a loop before this base on an integer called 'i'. Still have no clue as to why this while loop broke visual studio, any suggestions would be cool? Again I am so sorry for my terrible format etc as soon as I get on my computer I will update with the actual while loop code. Thanks everyone who commented, great advice - AGAIN... sorry - really difficult to comment on phone and I can't answer within 8 hours, this is only temporary :)
我认为这与将敌人列表索引设置为'i'而不是j有关,但这并没有返回错误,因为我在此基础之前有一个循环,称为'i'。仍然不知道为什么这个while循环打破了视觉工作室,任何建议都会很酷?再次,我很抱歉我的可怕格式等,一旦我上电脑,我将使用实际的while循环代码进行更新。谢谢所有评论的人,很棒的建议 - 再说一遍......对不起 - 真的很难在电话上发表评论而我在8小时内无法回答,这只是暂时的:)
1 个解决方案
#1
0
The project built but didn't appear because of a silly infinite while loop I created by mistake. This was made by creating a couple of different integers and not using the correct integer with its corresponding while loop. A simple problem really easy to fix but for someone who is new to C# this can seem really intimidating.
该项目已经建成,但由于我错误地创建了一个愚蠢无限的循环而没有出现。这是通过创建几个不同的整数而不是使用正确的整数及其相应的while循环来实现的。一个简单的问题很容易解决,但对于那些不熟悉C#的人来说,这看起来真的很吓人。
#1
0
The project built but didn't appear because of a silly infinite while loop I created by mistake. This was made by creating a couple of different integers and not using the correct integer with its corresponding while loop. A simple problem really easy to fix but for someone who is new to C# this can seem really intimidating.
该项目已经建成,但由于我错误地创建了一个愚蠢无限的循环而没有出现。这是通过创建几个不同的整数而不是使用正确的整数及其相应的while循环来实现的。一个简单的问题很容易解决,但对于那些不熟悉C#的人来说,这看起来真的很吓人。