好吧所以我正在制作一个2D游戏我也是新的复制/粘贴我的整个Game.cs脚本除了Program.cs之外我的项目中没有其他类别
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
bool hasJumped = true;
Vector2 velocity;
Texture2D player;
Texture2D ground1;
Vector2 playerPosition = new Vector2(30, 30);
Vector2 ground1p1 = new Vector2(0,430);
Vector2 ground1p2 = new Vector2(200,430);
Vector2 ground1p3 = new Vector2(0, 310);
Vector2 ground1p4 = new Vector2(200, 310);
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = Content.Load<Texture2D>("Player");
ground1 = Content.Load<Texture2D>("Ground1");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();
if ((playerPosition.Y > 0) && (playerPosition.Y < graphics.GraphicsDevice.Viewport.Height-player.Height))
{
playerPosition += velocity;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right)&&playerPosition.X <=(graphics.GraphicsDevice.Viewport.Width-player.Width))
{
velocity.X = 3f;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left) && (playerPosition.X >= 0))
{
velocity.X = -3f;
}
else velocity.X = 0f;
if (Keyboard.GetState().IsKeyDown(Keys.Up)&& hasJumped==false)
{
playerPosition.Y -= 10f;
velocity.Y = -4f;
hasJumped = true;
}
if (hasJumped==true)
{
velocity.Y += 0.10f;
}
if (DetectPlayerAndGround1Collision2(playerPosition,ground1p1,player,ground1) == true)
{
hasJumped = false;
velocity.Y = 0f;
}
if (DetectPlayerAndGround1Collision2(playerPosition, ground1p2, player, ground1) == true)
{
hasJumped = false;
velocity.Y = 0f;
}
if (hasJumped == false)
{
velocity.Y = 0f;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ground1,ground1p1 , Color.White);
spriteBatch.Draw(ground1, ground1p2, Color.White);
spriteBatch.Draw(ground1, ground1p4, Color.White);
spriteBatch.Draw(player, playerPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
public Boolean DetectPlayerAndGround1Collision2(Vector2 playerPositionM,Vector2 groundPositionM,Texture2D playerM,Texture2D groundM)
{
if (playerPositionM.Y <= groundPositionM.Y + groundM.Height && playerPositionM.Y > groundPositionM.Y)
{
if (playerPositionM.X < groundPositionM.X && (playerPositionM.X + playerM.Width < groundPositionM.X + groundM.Width && playerPositionM.X + playerM.Width > groundPositionM.X)) { return true; }
else if (playerPositionM.X + playerM.Width > groundPositionM.X + groundM.Width && (playerPositionM.X < groundPositionM.X + groundM.Width && playerPositionM.X > groundPositionM.X)) { return true; }
else if ((playerPositionM.X > groundPositionM.X && playerPositionM.X < groundPositionM.X + groundM.Width) && (playerPositionM.X + playerM.Width < groundPositionM.X + groundM.Width && playerPositionM.X + playerM.Width > groundPositionM.X)) { return true; }
}
}
}
有人可以告诉我为什么即时通讯在我的“地球1”精灵中摔倒了吗?我对我的剧本中的错误一无所知我最后的希望是问一个比我更熟练的人,或者我是盲目的我不能看到一些愚蠢的谢谢
答案 0 :(得分:1)
当您尝试检测方形A与方形B的碰撞时,您需要注意很多要点。
采用物理系统的简单例子,其中方框A可以上升并且必须下降,并且我们试图检查方框A的底部与方框B的碰撞,那么你需要检查A的两个点在确定碰撞之前,你可以使用B.和
我将两个点定义为A.O(原点左下角)和A.E(右下角延伸)。同样存在“地面”两点。
在运动中,除非方框A移动得非常缓慢,否则当你跌倒时,你可能会在方框B的边界内垂直移动。首先,您需要查看方框A的底部是否接触或通过方框B的顶部:
A.O.Y <= B.E.Y && A.O.Y > B.O.Y
现在如果那回来是假的那么我们仍在下降,如果它回来了,我们要么在B的顶部或内部。
现在您知道自己处于正确的高度或平台内,您可以开始检查A是否超出B的顶部:
悬挂在左边缘,或中间某处,但不在右边:
A.O.X < B.O.X && (A.E.X < B.E.X && A.E.X > B.O.X)
悬挂在右边缘或中间某处,但不在左侧:
A.E.X > B.E.X && (A.O.X < B.E.X && A.O.X > B.O.X)
中间的某个地方:
(A.O.X > B.O.X && A.O.X < B.E.X) && (A.E.X < B.E.X && A.E.X > B.O.X)
如果其中一个条件为真,那么另外两个是假的,所以要把它们全部放在一起检查这三个条件:
EDIT:
flag = false
If A.O.Y <= B.E.Y && A.O.Y > B.O.Y THEN
If A.O.X < B.O.X && (A.E.X < B.E.X && A.E.X > B.O.X) THEN
set flag to true
ELSE IF A.E.X > B.E.X && (A.O.X < B.E.X && A.O.X > B.O.X) THEN
set flag to true
ELSE IF (A.O.X > B.O.X && A.O.X < B.E.X) && (A.E.X < B.E.X && A.E.X > B.O.X) THEN
set flag to true
return flag
因此,阅读上述内容,我们首先检查您是否在垂直平台上。然后我们按顺序检查,悬挂在左边缘?挂在右边缘?在界限?如果这三个中的任何一个都是真的我们发回去,否则我们返回假。
我希望这很有帮助。