【发布时间】:2023-03-03 00:23:01
【问题描述】:
我的碰撞检测不起作用。我有一个弹丸清单和另一个方块清单。我尝试使用循环来检测碰撞检测,如我的代码所示,但它不起作用。我的碰撞检测代码是:
public void Collision_Detection()
{
for (int p = 0; p < projectile.Count; p++)
{
for (int i = 0; i < level_1.Count; i++)
{
if (projectile_obj[p].logRect.Intersects(level_1_blocks[i].rectangle))
{
//Subprogram here
}
}
}
}
我的射弹类是:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DestroytheFort
{
class Projectile
{
public static Texture2D log;
public static Vector2 logPos;
public Rectangle logRect;
public Projectile(Texture2D newLog, Vector2 newLogPos, Rectangle newLogRect)
{
log = newLog;
logPos = newLogPos;
logRect = newLogRect;
}
public void Initialize()
{
}
static double g = 520;
public static int keyState = 0;
static double v, vx, vy, alpha, t2 = 0;
public static void Update(GameTime gameTime)
{
// TODO: Add your update code here
if ((ISU.mouse.LeftButton == ButtonState.Pressed) && ISU.isInLevel == true)
{
keyState = 1;
v = -820;
alpha = MathHelper.ToRadians(33f);
vx = v * Math.Cos(alpha);
vy = v * Math.Sin(alpha);
}
if (keyState == 1)
{
logPos.Y = (float)(vy * t2 + g * t2 * t2 / 2) + 540 - log.Height;
logPos.X = (float)((vx * -1) * t2) + 60;
t2 = t2 + gameTime.ElapsedGameTime.TotalSeconds;
}
if (logPos.Y > ISU.graphics.GraphicsDevice.Viewport.Height - log.Height)
{
logPos.Y = ISU.graphics.GraphicsDevice.Viewport.Height - log.Height;
keyState = 0;
t2 = 0;
}
if (logPos.X > ISU.graphics.GraphicsDevice.Viewport.Width - log.Width)
{
logPos.X = ISU.graphics.GraphicsDevice.Viewport.Width - log.Width;
keyState = 0;
t2 = 0;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(log, logPos, logRect, Color.White);
}
}
还有我的 level_1 课程:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DestroytheFort
{
class Wood_Level_1
{
public Texture2D texture;
public Rectangle rectangle;
public Wood_Level_1(Texture2D newTexture, Rectangle newRect)
{
texture = newTexture;
rectangle = newRect;
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public void Initialize()
{
// TODO: Add your initialization code here
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public void Update(GameTime gameTime)
{
// TODO: Add your update code here
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, Color.White);
}
}
}
level_1 是我的 1 级课程的列表名称。我在主类的 Update() 中调用碰撞检测方法。任何帮助将不胜感激。
【问题讨论】:
-
您的问题格式错误。缺少几个尾随花括号。请更新它。解决您的问题,我很乐意为您解答。
-
谢谢!
标签: c# xna collision-detection