" Rem ad Triarios redisse"西庇阿非洲人 我正在编写一个平台游戏并且我正在处理碰撞,但当它碰到一个堆栈溢出错误时;我无法自己解决这个问题,如果有人知道怎么做,我会感激不尽修理它。 班级代码(主要关注点):
public class Colliding
{
Rectangle testCollide;
Vector2 playerposition, objectPosition;
Texture2D objectPic;
bool jump;
public Rectangle TestCollide
{
get { return TestCollide; }
set { testCollide = value; }
}
public Vector2 ObjectPosition
{
get { return objectPosition; }
set { objectPosition = value; }
}
public Vector2 Return
{
get { return objectPosition; }
}
public Texture2D ObjectPic
{
set { objectPic = value; }
}
public bool Bool
{
get { return jump; }
}
public void Update()
{
/*if (testCollide.Contains(new Point((int)objectPosition.X, (int)objectPosition.Y + (int)objectPic.Height)) && !
testCollide.Contains(new Point((int)objectPosition.X, (int)objectPosition.Y + (int)objectPic.Height)))
{
objectPosition = new Vector2(objectPosition.X, testCollide.Top - objectPic.Height);
jump = false;
}*/
if (testCollide.Contains(new Point((int)objectPosition.X + (int)objectPic.Width, (int)objectPosition.Y))&&
testCollide.Contains(new Point((int)objectPosition.X + (int)objectPic.Width, (int)objectPosition.Y)))
{
objectPosition = new Vector2(TestCollide.Right - objectPic.Width, ObjectPosition.Y);
jump = false;
}
if (testCollide.Contains(new Point((int)objectPosition.X , (int)objectPosition.Y)) &&
testCollide.Contains(new Point((int)objectPosition.X , (int)objectPosition.Y)))
{
objectPosition = new Vector2(TestCollide.Left +1 , ObjectPosition.Y);
jump = false;
}
我的主要代码(目前有点乱)
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState keyState;
KeyboardState prevKeyState;
Texture2D flour,nonAnimPlayer;
Rectangle ground1,ground2,playerRect;
Vector2 playerPosition;
float jumpspeed;
int screenX = 800;
int screenY=480;
float standardMoveSpeed;
bool jumping;
Colliding colGround1=new Colliding ();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.graphics.PreferredBackBufferWidth = screenX;
this.graphics.PreferredBackBufferHeight = screenY;
}
protected override void Initialize()
{
keyState = Keyboard.GetState();
ground1 = new Rectangle(-100, (screenY / 5) * 4, 300,screenY);
ground2 = new Rectangle(400, (screenY / 5) * 4, 800, screenY);
playerRect = new Rectangle(0, 0, 10, 10);
standardMoveSpeed = 300;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
flour = Content.Load<Texture2D>("background");
nonAnimPlayer = Content.Load<Texture2D>("blok");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
prevKeyState = keyState;
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.D))
playerPosition.X += standardMoveSpeed *(float) gameTime.ElapsedGameTime.TotalSeconds;
if (keyState.IsKeyDown(Keys.A))
playerPosition.X -= standardMoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (keyState.IsKeyDown(Keys.Space)&&prevKeyState.IsKeyUp(Keys.Space ))
{
if (jumpspeed == 0)
jumpspeed = 300;
else
jumpspeed += 35;
jumping = true;
}
playerPosition.Y += standardMoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if(jumping)
{
playerPosition.Y -= standardMoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds+jumpspeed*(float)gameTime.ElapsedGameTime.TotalSeconds ;
jumpspeed-=9;
if (jumpspeed <= 0)
jumping = false;
}
if (ground2.Contains(new Point((int)playerPosition.X, (int)playerPosition.Y + (int)nonAnimPlayer.Height)))
{
playerPosition.Y = ground2.Top - nonAnimPlayer.Height;
jumpspeed = 0;
}
colGround1.ObjectPosition = playerPosition;
colGround1.TestCollide = ground1;
colGround1.ObjectPic = nonAnimPlayer;
colGround1.Update();
playerPosition = colGround1.Return;
if (colGround1.Bool)
jumpspeed = 0;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(flour, ground1, Color.White);
spriteBatch.Draw(flour, ground2, Color.White);
spriteBatch.Draw(nonAnimPlayer, playerPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);