我试图使碰撞矩形与列表一起使用。
所以这里是代码:
List<Planet> planets;
Planet planet;
Texture2D levelSelectTexture;
Texture2D star;
Vector2 position;
Rectangle planetRectangle;
public PlanetSelect(EventHandler screenEvent, GraphicsDevice graphics, ContentManager content, SpriteBatch batch)
: base(screenEvent, graphics, content, batch)
{
star = content.Load<Texture2D>("ursaeMajorisStar");
position = new Vector2((Graphics.Viewport.Width - star.Width) / 2, (Graphics.Viewport.Height - star.Height) / 2);
planets = new List<Planet>();
Planet planet = new Planet(Content, Batch);
planet.Load("planet0");
planet.velocity = 0.04f;
planet.radius = 80;
planet.angle = MathHelper.ToRadians(90);
planets.Add(planet);
planet = new Planet(Content, Batch);
planet.Load("planet2");
planet.velocity = 0.02f;
planet.radius = 135;
planet.angle = MathHelper.ToRadians(120);
planets.Add(planet);
planet = new Planet(Content, Batch);
planet.Load("planet3");
planet.velocity = 0.009f;
planet.radius = 180;
planet.angle = MathHelper.ToRadians(160);
planets.Add(planet);
**planetRectangle = new Rectangle(planet.position.X, planet.position.Y, planet.image.Width, planet.image.Height);**
levelSelectTexture = content.Load<Texture2D>("levelSelectMenu");
}
public override void Update(GameTime gameTime)
{
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
if (tl.State==TouchLocationState.Moved)
{
if (planetRectangle.Contains((int)tl.Position.X,(int)tl.Position.Y))
{
screenEvent.Invoke(this, new EventArgs());
}
}
foreach (Planet planet in planets)
{
planet.angle += planet.velocity;
float orbitx = (float)(Math.Cos(planet.angle) * planet.radius);
float orbity = (float)(Math.Sin(planet.angle) * planet.radius);
float x = (Graphics.Viewport.Width - planet.image.Width) / 2 + orbitx;
float y = (Graphics.Viewport.Height - planet.image.Height) / 2 + orbity;
planet.position = new Vector2(x, y);
}
base.Update(gameTime);
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(levelSelectTexture, Vector2.Zero, Color.White);
foreach (Planet planet in planets)
{
planet.Draw();
}
spriteBatch.Draw(star, position, Color.White);
base.Draw(spriteBatch);
}
每次我尝试创建一个碰撞矩形并传入planet.position值以及图像宽度和高度时,它都会抛出一个错误并且不会让我
所以我声明:矩形planetRectangle;
然后当我初始化时,我执行planetRectangle = new Rectangle (planet.position.X,planet.Position.Y,planet.image.Width,planet.image.Height);
它只是不想将值传递到矩形上,我如何与 objectS 列表发生冲突?我应该以某种方式在基类或精灵类中创建它吗?
我得到的错误是:
无法将 float 转换为 int (关于 planet.position.X&Y )并且最佳重载匹配有争论。我已经突出显示了它有问题的行。
通常这些行星对象在同心圆中移动,我希望能够通过触摸选择它们,但即使这样做我也需要先创建一个矩形。