我正在尝试制作一个简单的平台游戏。玩家是一个正方形,所有的平台都是正方形。我是 XNA 的初学者,但我有适度的 C# 4.0 经验。
我的代码的问题是,当我的角色穿过平台的底部时,即使我将其设置为他不应该穿过,他也会继续穿过。
提前我想问一下是否有人可以帮助我,因为我无法解决这个问题。
以下是不同代码段如何协同工作:
- 在平台结构中加载图像并存储一个枚举,它可以保存三个值之一:可通过(可以通过)、不可通过(完全实心)和平台(可以通过底部并停在顶部)
- 需要修复的碰撞检测方法(它检查交叉点,检查该特定平台的枚举,然后确定它是否可以通过并返回一个数组,其中包含:{isabove, isbelow, isleft, isright}该平台)
- 如果玩家在平台顶部,它确实返回 true,但当他们到达底部时没有任何反应
- 我的球员的垂直运动处理重力和垂直运动,这可能是罪魁祸首,但我不能说。碰撞检查在开始时在更新循环中运行,然后在之后运行垂直运动方法
这是碰撞的代码:
public bool[] environmentCollisionDetect(Charachter thing, ScreenObjects sreenobjectlist, bool[] platformstatus)
{
//Defaults the collision to false
platformstatus[0] = false; //Above platform
platformstatus[1] = false; //Below Platform
platformstatus[2] = false; //Left
platformstatus[3] = false; //Right
//Store the player in boxA
boxA.Height = thing.image.Height;
boxA.Width = thing.image.Width;
boxA.X = (int)thing.position.X;
boxA.Y = (int)thing.position.Y;
//Find a collision
foreach (Platform platform in screenobjectlist)
{
//Makes sure the platform cannot be passed through
if (platform.platformCollisionProperty != platformCollision.passable)
{
//Store the platform's rectangle
boxB = platform.destinationrectangle;
boxB.Height = platform.image.Height;
boxB.Width = platform.image.Width;
boxB.X = (int)platform.destinationrectangle.X;
boxB.Y = (int)platform.destinationrectangle.Y;
//Collision Check
if (boxA.Intersects(boxB))
{
switch (platform.platformCollisionProperty)
{
//Platform is Solid
case platformCollision.impassible:
//Player is below it
if (boxA.Top > boxB.Bottom)
{
platformstatus[1] = true;
}
//Player is above it
if (boxA.Bottom > boxB.Top)
{
platformstatus[0] = true;
}
break;
//Platform can be passed through bottom
case platformCollision.platform:
if (boxA.Bottom > boxB.Top)
{
platformstatus[0] = true;
}
break;
}
break;
}
}
}
return platformstatus;
}
这是垂直运动代码:
- 当跳转按钮被激活时,isJumping bool 设置为 true
- isinair bool 仅在此循环中用于确定玩家是否仍在空中滑行
最后我被用作计数器,因为我无法让计时器工作
public void verticalMotionI(GameTime gametime) { //Resets the bools if not standing on platform if ((this.isJumping == true) && (this.platformstatus[0] == false)) { this.i = 0; this.isJumping = false; this.isinair = false; } //Jumps if A button is activated and standing on a platform else if ((this.isJumping == true) && (this.platformstatus[0] == true)) { //Set up bools to jump this.isJumping = false; this.isinair = true; //Overwrites the start time this.starttime = currentime; //Move up this.position.Y -= this.playermovementspeed * 1.5f; //Increase counter this.i++; } //Pulls stops moving player if jumptimer had elapsed else if ((this.isinair == true) && (i == this.jumptime)) { this.isinair = false; //Apply gravity Gravity.forceofGravity(this); } //Stops the jump if bottom of a platform is hit else if ((this.isinair == true) && (this.platformstatus[1] == true)) { //Reset bools this.isinair = false; //reset I this.i = 0; //Apply gravity Gravity.forceofGravity(this); } //Continues the jump if in air else if ((this.isinair == true) && (this.platformstatus[1] == false)) { //Move up this.position.Y -= this.playermovementspeed * 1.5f; //Increase counter this.i++; } //Apply Gravity else if ((this.isinair == false) && (this.platformstatus[0] == false)) { //reset I this.i = 0; Gravity.forceofGravity(this); } }