C# XNA-相交类-2D
好的,我已经搜索了几个小时没有任何结果,真的希望你能帮助我。我很难让交叉路口正常工作。当我的课程发生冲突时,什么都没有发生,这可能是因为我失明了,但对XNA来说也是个新手。我已经创建了一个射击游戏,我希望游戏在玩家控制的航天飞机与敌人相撞时结束 使用类;Game1.cs、Player.cs和敌军.cs。注意,我只粘贴了我认为解决这个问题所必需的信息 游戏1.csC# XNA-相交类-2D,c#,class,vector,xna,rectangles,C#,Class,Vector,Xna,Rectangles,好的,我已经搜索了几个小时没有任何结果,真的希望你能帮助我。我很难让交叉路口正常工作。当我的课程发生冲突时,什么都没有发生,这可能是因为我失明了,但对XNA来说也是个新手。我已经创建了一个射击游戏,我希望游戏在玩家控制的航天飞机与敌人相撞时结束 使用类;Game1.cs、Player.cs和敌军.cs。注意,我只粘贴了我认为解决这个问题所必需的信息 游戏1.cs protected override void LoadContent() { shuttle = new Player(Co
protected override void LoadContent()
{
shuttle = new Player(Content.Load<Texture2D>(@"spaceship"), new Vector2(300, 400), new Vector2(24, 24), spriteBatch);
}
protected override void Update(GameTime gameTime)
{
if (random.Next(0, 70) < droprate)
{
int posX = random.Next(20, 580);
enemyList.Add(new Enemy(Content.Load<Texture2D>(@"enemy1"), new Vector2(posX, -50), new Vector2(24, 24), spriteBatch));
droprate += 0.10;
}
foreach (Enemy e in enemyList)
{
if (shuttle.rectangle.Intersects(e.rectangle))
Exit();
}
}
敌方
public Rectangle rectangle;
public Enemy(Texture2D texture, Vector2 initialPos, Vector2 origin, SpriteBatch spriteBatch) : base(texture, initialPos, origin, spriteBatch)
{
this.rectangle = new Rectangle(Convert.ToInt32(initialPos.X), Convert.ToInt32(initialPos.Y), 48, 48);
}
public override void Update(GameTime gameTime)
{
initialPos.Y += 3.5f;
if (initialPos.X < 300)
initialPos.X += 0.41f;
if (initialPos.X > 300)
initialPos.X -= 0.41f;
}
公共矩形;
公敌(纹理2D纹理,向量2初始位置,向量2原点,SpriteBatch SpriteBatch):基础(纹理,初始位置,原点,SpriteBatch)
{
this.rectangle=新矩形(Convert.ToInt32(initialPos.X)、Convert.ToInt32(initialPos.Y)、48、48);
}
公共覆盖无效更新(游戏时间游戏时间)
{
初始位置Y+=3.5f;
如果(初始位置X<300)
初始位置X+=0.41f;
如果(初始位置X>300)
初始位置X-=0.41f;
}
如果要使用浮点值,每次都可以将initialPos
中的浮点值转换为矩形。initialPos将保持其准确性,而矩形将只对浮点值进行四舍五入。或者,您可以创建自己的RectangleF
结构,用于XNA的所有其他对象(因为我借鉴了@TheC4Fox关于浮点矩形的想法,所以对@TheC4Fox给予了一些赞扬……只是为了使矩形F能够轻松地与XNA的其他功能兼容。+1表示您的答案,兄弟,因为它回答了OP的原始Q)。例如:
public struct RectangleF
{
float w = 0;
float h = 0;
float x = 0;
float y = 0;
public float Height
{
get { return h; }
set { h = value; }
}
//put Width, X, and Y properties here
public RectangleF(float X, float Y, float width, float height)
{
w = width;
h = height;
x = X;
y = Y;
}
public bool Intersects(Rectangle refRectangle)
{
Rectangle rec = new Rectangle((int)x, (int)y, (int)w, (int)h);
if (rec.Intersects(refRectangle)) return true;
else return false;
}
}
然后你会在你的玩家和敌人职业中声明:
public RectangleF rectangle;
public Enemy(Texture2D texture, Vector2 initialPos, Vector2 origin, SpriteBatch spriteBatch) : base(texture, initialPos, origin, spriteBatch)
{
this.rectangle = new RectangleF(initialPos.X, initialPos.Y, 48, 48);
}
public override void Update(GameTime gameTime)
{
rectangle.Y += 3.5f;
if (rectangle.X < 300)
rectangle.X += 0.41f;
if (rectangle.X > 300)
rectangle.X -= 0.41f;
}
公共矩形f矩形;
公敌(纹理2D纹理,向量2初始位置,向量2原点,SpriteBatch SpriteBatch):基础(纹理,初始位置,原点,SpriteBatch)
{
this.rectangle=新矩形F(初始位置X,初始位置Y,48,48);
}
公共覆盖无效更新(游戏时间游戏时间)
{
矩形Y+=3.5f;
if(矩形X<300)
矩形X+=0.41f;
如果(矩形X>300)
矩形X-=0.41f;
}
HTH玩家或敌人移动了吗?在最初创建它们之后,你似乎从来没有更新过它们的碰撞边界。玩家/航天飞机由我控制,而敌人的宇宙飞船一直朝屏幕底部移动。你是否为你的
敌人
和更新过矩形
的值>Player
对象?您正在使用相交()
方法正确,这意味着您提供的数据是错误的。我不确定这一点。我可以发布我的更新函数,也许这样您会更好地理解。您是否使用了调试器?可能有助于查看您是否正在使用您认为正确的数据是的,我理解您的意思。但我无法理解cordi矩形的nate不能用float表示。它必须是int,我如何绕过它?使用rectangleF:@TheC4Fox,XNA没有rectangleF。你可以做的,@ArvinAshrafi,只是像你现在做的那样将位置存储为float,并且在每次更新调用结束时,将float值转换成一个矩形,就像你在初始化时所做的那样调整玩家/敌人:this.rectangle=新矩形(Convert.ToInt32(initialPos.X)、Convert.ToInt32(initialPos.Y)、48、48);
@davidsbro使用XNA框架意味着他不能使用绘图库?@c4fox没有,但我认为将浮点值转换成XNA矩形更容易,因为如果他想在spritebatch.Draw()中使用矩形,就不必将矩形f转换成矩形,和其他XNA函数,人类无法检测碰撞矩形是否偏离0.9f。我只是将其抛出,因为这两种方式都可以实现
public override void Update(GameTime gameTime)
{
initialPos.Y += 3.5f;
rectangle.Y += 3.5f;
if (initialPos.X < 300){
initialPos.X += 0.41f;
rectangle.X += 0.41f;
}
if (initialPos.X > 300)
initialPos.X -= 0.41f;
rectangle.X -= 0.41f;
}
public override void Update(GameTime gameTime)
{
initialPos.Y += 3.5f;
if (initialPos.X < 300) initialPos.X += 0.41f;
if (initialPos.X > 300) initialPos.X -= 0.41f;
rectangle.X -= Convert.ToInt32(initialPos.X);
rectangle.Y += Convert.ToInt32(initialPos.Y);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace TestBench{
class Program{
static void Main(string[] args){
float f = 0.0F;
int Y = 0;
for (int i = 0; i < 10; i++) {
f += 0.41F;
Y = Convert.ToInt32(f);
Debug.WriteLine("Float: " + f + " Int32 " + Y);
}
}
}
}
Float: 0.41 Int32 0
Float: 0.82 Int32 1
Float: 1.23 Int32 1
Float: 1.64 Int32 2
Float: 2.05 Int32 2
Float: 2.46 Int32 2
Float: 2.87 Int32 3
Float: 3.28 Int32 3
Float: 3.69 Int32 4
Float: 4.1 Int32 4
public struct RectangleF
{
float w = 0;
float h = 0;
float x = 0;
float y = 0;
public float Height
{
get { return h; }
set { h = value; }
}
//put Width, X, and Y properties here
public RectangleF(float X, float Y, float width, float height)
{
w = width;
h = height;
x = X;
y = Y;
}
public bool Intersects(Rectangle refRectangle)
{
Rectangle rec = new Rectangle((int)x, (int)y, (int)w, (int)h);
if (rec.Intersects(refRectangle)) return true;
else return false;
}
}
public RectangleF rectangle;
public Enemy(Texture2D texture, Vector2 initialPos, Vector2 origin, SpriteBatch spriteBatch) : base(texture, initialPos, origin, spriteBatch)
{
this.rectangle = new RectangleF(initialPos.X, initialPos.Y, 48, 48);
}
public override void Update(GameTime gameTime)
{
rectangle.Y += 3.5f;
if (rectangle.X < 300)
rectangle.X += 0.41f;
if (rectangle.X > 300)
rectangle.X -= 0.41f;
}