不确定如何修复此StackOverflow异常是未处理的错误
本文关键字:未处理 错误 异常 StackOverflow 何修复 不确定 | 更新日期: 2023-09-27 17:52:46
第一次在这里发布,我知道我可能会因为我的编码而受到批评,但我是一名12年级的学生,为了我的软件专业自学这门语言,我今天才开始理解课程,所以我试着带着这个想法重新做我的游戏,游戏远未完成。对不起,长帖子的代码,但我真的不能找出什么错了,在game1.cs只是player. loadcontent(内容),player. update (gameTime)和player. draw (spriteBatch),玩家是Player_Main类的对象。
namespace HonourInBlood
{
public class Player_Main //This class deals with updating all other classes
{
public Player_Sprite psprite = new Player_Sprite();
public Player_Movement pmove = new Player_Movement();
public Player_Stats pstats = new Player_Stats();
public Player_Abilities pabilities = new Player_Abilities();
public Player_Controls pcontrols = new Player_Controls();
public Player_Main()
{
}
public void Update(GameTime gameTime)
{
psprite.Update(gameTime);
pmove.Update(gameTime);
pstats.Update(gameTime);
pabilities.Update(gameTime);
}
public void LoadContent(ContentManager Content)
{
psprite.LoadContent(Content);
}
public void Draw(SpriteBatch spriteBatch)
{
psprite.Draw(spriteBatch);
}
}
public class Player_Sprite //This class deals with drawing the players sprite and animations
{
public Rectangle player;
public Texture2D playerAttack, playerWalk; //PLAYER TEXTURES
public Player_Main pmain = new Player_Main();
public Player_Sprite()
{
}
public void Update(GameTime gameTime)
{
}
public void LoadContent(ContentManager Content)
{
player = new Rectangle(0, 0, 64, 64);
playerAttack = Content.Load<Texture2D>("player_attack_large");
playerWalk = Content.Load<Texture2D>("player_move_large");
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(playerWalk, pmain.pmove.position, new Rectangle(0, 0, 64, 64), Color.White);
}
}
public class Player_Movement //This class deals with all movement relating to the player ie sprinting
{
public Vector2 position, velocity, velocity_base, sprint;
public Player_Main pmain = new Player_Main();
Game1 gamecore = new Game1();
public Player_Movement()
{
}
public void Update(GameTime gameTime)
{
KeyboardState KS = Keyboard.GetState();
//Player movement using arrow keys
if (KS.IsKeyDown(Keys.Up))
position.Y -= velocity.Y;
if (KS.IsKeyDown(Keys.Down))
position.Y += velocity.Y;
if (KS.IsKeyDown(Keys.Right))
position.X += velocity.X;
if (KS.IsKeyDown(Keys.Left))
position.X -= velocity.X;
//Making edge of screen stop player movement to prevent going off-screen
if (position.X < 0)
position.X = 0;
if (position.Y < 0)
position.Y = 0;
if (position.X + pmain.psprite.player.Width > gamecore.GraphicsDevice.Viewport.Width)
position.X = gamecore.GraphicsDevice.Viewport.Width - pmain.psprite.player.Width;
if (position.Y + pmain.psprite.player.Height > gamecore.GraphicsDevice.Viewport.Height)
position.Y = gamecore.GraphicsDevice.Viewport.Height - pmain.psprite.player.Height;
//
//SPRINTING
if (KS.IsKeyDown(pmain.pcontrols.sprintKey))
{
if (KS.IsKeyDown(Keys.Up) || KS.IsKeyDown(Keys.Down) || KS.IsKeyDown(Keys.Right) || KS.IsKeyDown(Keys.Left)) //You're only sprinting when you're moving, right?
{
if (pmain.pstats.currentStamina > 0) //Only when you have stamina can you run
{
velocity.X += sprint.X;
velocity.Y += sprint.X;
pmain.pstats.currentStamina -= pmain.pstats.stamina; //Decreases stamina
}
}
}
//
//
//SPRINTING
if (pmain.pstats.currentStamina > 0)
{
if (velocity.X > velocity_base.X) //When current velocity becomes greater than base velocity check whether space bar is released
{
if (KS.IsKeyUp(pmain.pcontrols.sprintKey))
velocity.X = velocity_base.X; //If it is, set current velocity back to base
}
if (velocity.Y > velocity_base.Y)
{
if (KS.IsKeyUp(pmain.pcontrols.sprintKey))
velocity.Y = velocity_base.Y;
}
if (velocity.X > velocity_base.X + sprint.X)
velocity.X = velocity_base.X + sprint.X;
if (velocity.Y > velocity_base.Y + sprint.Y)
velocity.Y = velocity_base.Y + sprint.Y;
//
}
if (pmain.pstats.currentStamina <= 0) //When you run out of stamina, set speed to normal
{
velocity.X = velocity_base.X;
velocity.Y = velocity_base.Y;
pmain.pstats.currentStamina = 0;
}
}
public void LoadContent(ContentManager Content)
{
position = new Vector2(400, 300);
velocity = new Vector2(4, 4); //Current velocity
velocity_base = new Vector2(4, 4); //Base velocity
sprint = new Vector2(6, 6); //Added velocity when sprinting
}
public void Draw(SpriteBatch spriteBatch)
{
}
}
public class Player_Stats //This class deals with all stats of the player
{
public double stamina, maxStamina, currentStamina, staminaRate, staminaDisplay;
public double mana, maxMana, currentMana, manaRate, manaDisplay;
public Player_Stats()
{
//STAMINA
maxStamina = 100f;
stamina = 1f;
currentStamina = maxStamina;
staminaRate = 0.1f;
staminaDisplay = maxStamina;
//
//MANA
maxMana = 200f;
mana = 0.5f;
currentMana = maxMana;
manaRate = 0.5f;
manaDisplay = maxMana;
//
}
public void Update(GameTime gameTime)
{
currentStamina += staminaRate; //Stamina regen
currentMana += manaRate; //TEST STATEMENT
if (currentStamina > maxStamina) //Cant have more stamina than the max now, can we?
currentStamina = maxStamina;
if (currentMana > maxMana)
currentMana = maxMana;
}
public void LoadContent(ContentManager Content)
{
}
public void Draw(SpriteBatch spriteBatch)
{
}
}
public class Player_Abilities //This class deals with all abilities the player has
{
public double spellCD1, spell1Cost, spellDisplay1;
Player_Controls pcontrols = new Player_Controls();
Player_Stats pstats = new Player_Stats();
Player_Movement pmove = new Player_Movement();
HUD HUDmain = new HUD();
public Player_Abilities()
{
spellCD1 = 150;
spell1Cost = 120;
}
public void Update(GameTime gameTime)
{
KeyboardState KS = Keyboard.GetState();
//MANA AND SPELL STUFF///////////////////////////////
if (KS.IsKeyDown(pcontrols.spellKey1))
{
if (pstats.currentMana >= spell1Cost)
{
if (spellCD1 <= 0)
{
if (KS.IsKeyDown(Keys.Up))
pmove.position.Y -= 300;
if (KS.IsKeyDown(Keys.Down))
pmove.position.Y += 300;
if (KS.IsKeyDown(Keys.Right))
pmove.position.X += 300;
if (KS.IsKeyDown(Keys.Left))
pmove.position.X -= 300;
pstats.mana = spell1Cost;
pstats.currentMana -= pstats.mana;
spellCD1 = 150;
}
}
}
spellCD1--;
spellDisplay1 = spellCD1 / 60;
spellDisplay1 = Math.Round(spellDisplay1, 1);
if (spellCD1 <= 0)
spellCD1 = 0;
}
public void LoadContent(ContentManager Content)
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(HUDmain.HUDFont, "Ability: Cooldown", new Vector2(20, 40), Color.White);
spriteBatch.DrawString(HUDmain.HUDFont, "Teleport: " + spellDisplay1 + "s", new Vector2(20, 60), Color.White);
}
}
public class Player_Controls //This class deals with the controls
{
public Keys sprintKey, spellKey1;
public Player_Controls()
{
sprintKey = Keys.LeftShift;
spellKey1 = Keys.D1;
}
public void Update(GameTime gameTime)
{
}
public void LoadContent(ContentManager Content)
{
}
}
}
下面是调用栈:
. player_sprite . player_sprite()第45行+ 0xffffffe6字节c#honorinblood . player_main . player_main()第14行+ 0x15字节c#player_sprite . player_sprite()行45 + 0x15字节c#honorinblood . player_main . player_main()第14行+ 0x15字节c#player_sprite . player_sprite()行45 + 0x15字节c#honorinblood . player_main . player_main()第14行+ 0x15字节c#荣誉血液。player_sprite . player_sprite()行45 + 0x15字节c#
以这种方式重复,直到:
The maximum number of stack frames supported by Visual Studio has been exceeded.
这是一个新问题(给克里斯·巴拉德):
改变代码在所有的地方我也有,我只是比以前更困惑,我有一个HUD.cs文件,引用Player_Stats类在Player.cs文件的变量,但我不能,因为:
'HonourInBlood.Player_Stats' does not contain a constructor that takes 0 arguments
如果你浏览一下代码,看看每个类的开头,你会发现有一个引用网络,现在导致问题,因为我把它改成了你给我看的方式
好的,我知道问题了。
每次你创建一个Player_Main
-它有一个字段,创建一个新的Player_Sprite
Player_Sprite
本身有一个字段创建另一个新的Player_Main
这反过来又创建一个新的Player_Sprite
等,永远(或至少直到你的堆栈溢出)
编辑
我猜你想要一个Player_Main
,它保留了对Player_Sprite
的引用,而Player_Sprite
需要记住创建它的Player_Main
(而不是创建一个新的)。比如:
public class Player_Main
{
public Player_Sprite psprite;
public Player_Main()
{
psprite = new Player_Sprite(this);
}
}
public class Player_Sprite
{
public Player_Main _pmain;
public Player_Sprite(Player_Main pmain)
{
_pmain = pmain;
}
}
关于第二个问题。我怀疑您已经更新了Player_Stats
类,以便在构造函数中使用Player_Main
。
对于Player_Stats
,这是不必要的。这是因为Player_Stats
中的代码都不需要来访问Player_Main
的方法或属性。Player_Controls
、Player_Abilities
和Player_Movements
仅在Player_Sprite
中需要,因为Draw
方法访问pmain.pmove.position
Draw
方法,所以:
public class Player_Sprite //This class deals with drawing the players sprite and animations
{
public Rectangle player;
public Texture2D playerAttack, playerWalk; //PLAYER TEXTURES
public Player_Sprite()
{
}
// .. methods removed for brevity
public void Draw(SpriteBatch spriteBatch, Player_Movement pmove)
{
spriteBatch.Draw(playerWalk, pmove.position, new Rectangle(0, 0, 64, 64), Color.White);
}
}
因此Player_Main.Draw
变成:
public void Draw(SpriteBatch spriteBatch)
{
psprite.Draw(spriteBatch, pmove);
}
并且您不再需要将this
传递给Player_Sprite
构造函数。
EDIT修改后的Player_Movement
public class Player_Movement //This class deals with all movement relating to the player ie sprinting
{
public Vector2 position, velocity, velocity_base, sprint;
public Player_Main pmain = null;
Game1 gamecore = new Game1();
public Player_Movement(Player_Main pmain)
{
this.pmain = pmain;
}
public void Update(GameTime gameTime)
{
KeyboardState KS = Keyboard.GetState();
//Player movement using arrow keys
if (KS.IsKeyDown(Keys.Up))
position.Y -= velocity.Y;
if (KS.IsKeyDown(Keys.Down))
position.Y += velocity.Y;
if (KS.IsKeyDown(Keys.Right))
position.X += velocity.X;
if (KS.IsKeyDown(Keys.Left))
position.X -= velocity.X;
//Making edge of screen stop player movement to prevent going off-screen
if (position.X < 0)
position.X = 0;
if (position.Y < 0)
position.Y = 0;
if (position.X + pmain.psprite.player.Width > gamecore.GraphicsDevice.Viewport.Width)
position.X = gamecore.GraphicsDevice.Viewport.Width - pmain.psprite.player.Width;
if (position.Y + pmain.psprite.player.Height > gamecore.GraphicsDevice.Viewport.Height)
position.Y = gamecore.GraphicsDevice.Viewport.Height - pmain.psprite.player.Height;
//
//SPRINTING
if (KS.IsKeyDown(pmain.pcontrols.sprintKey))
{
if (KS.IsKeyDown(Keys.Up) || KS.IsKeyDown(Keys.Down) || KS.IsKeyDown(Keys.Right) || KS.IsKeyDown(Keys.Left)) //You're only sprinting when you're moving, right?
{
if (pmain.pstats.currentStamina > 0) //Only when you have stamina can you run
{
velocity.X += sprint.X;
velocity.Y += sprint.X;
pmain.pstats.currentStamina -= pmain.pstats.stamina; //Decreases stamina
}
}
}
//
//
//SPRINTING
if (pmain.pstats.currentStamina > 0)
{
if (velocity.X > velocity_base.X) //When current velocity becomes greater than base velocity check whether space bar is released
{
if (KS.IsKeyUp(pmain.pcontrols.sprintKey))
velocity.X = velocity_base.X; //If it is, set current velocity back to base
}
if (velocity.Y > velocity_base.Y)
{
if (KS.IsKeyUp(pmain.pcontrols.sprintKey))
velocity.Y = velocity_base.Y;
}
if (velocity.X > velocity_base.X + sprint.X)
velocity.X = velocity_base.X + sprint.X;
if (velocity.Y > velocity_base.Y + sprint.Y)
velocity.Y = velocity_base.Y + sprint.Y;
//
}
if (pmain.pstats.currentStamina <= 0) //When you run out of stamina, set speed to normal
{
velocity.X = velocity_base.X;
velocity.Y = velocity_base.Y;
pmain.pstats.currentStamina = 0;
}
}
public void LoadContent(ContentManager Content)
{
position = new Vector2(400, 300);
velocity = new Vector2(4, 4); //Current velocity
velocity_base = new Vector2(4, 4); //Base velocity
sprint = new Vector2(6, 6); //Added velocity when sprinting
}
public void Draw(SpriteBatch spriteBatch)
{
}
}
在Player_Main
构造函数中,这样做:
public class Player_Main //This class deals with updating all other classes
{
public Player_Sprite psprite = new Player_Sprite();
public Player_Movement pmove = null;
public Player_Stats pstats = new Player_Stats();
public Player_Abilities pabilities = new Player_Abilities();
public Player_Controls pcontrols = new Player_Controls();
public Player_Main()
{
pmove = new Player_Movement(this);
}
// ... rest removed for brevity
}