0

我试图使碰撞矩形与列表一起使用。

所以这里是代码:

  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 )并且最佳重载匹配有争论。我已经突出显示了它有问题的行。

通常这些行星对象在同心圆中移动,我希望能够通过触摸选择它们,但即使这样做我也需要先创建一个矩形。

4

2 回答 2

1

您需要转换planetRectangle为方法。您已经编写了一条语句,该语句采用您创建的第三个“行星”对象并创建了一个矩形,该矩形存储在变量中一次planetRectangle并且永远不会更改

尝试删除您突出显示的那一行,并将其放入如下方法中:

static Rectangle GetPlanetRectangle(Planet planet)
{
    planetRectangle = new Rectangle(planet.position.X,
            planet.position.Y, planet.image.Width, planet.image.Height);
}

或者更好的是,如果Planet是您自己的类,请将其添加为如下属性Planet

public Rectangle Bounds
{
    get
    {
        return new Rectangle(this.position.X,
                this.position.Y, this.image.Width, this.image.Height);
    }
}

然后您需要为每个行星调用此方法或访问此属性,如下所示:

foreach(TouchLocation tl in touchCollection)
{
    if(tl.State == TouchLocationState.Moved) // Moved? Is that right?
    {
        foreach(Planet planet in planets)
        {
            // Alternately: if(GetPlanetRectangle(planet).Contains(...))
            if(planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y))
            {
                // Do stuff...
            }
        }
    }
}
于 2012-07-23T12:01:12.023 回答
0

您需要将行星值转换int为将它们传递到Rectangle

planetRectangle = new Rectangle((int)planet.position.X, (int)planet.position.Y, (int)planet.image.Width, (int)planet.image.Height);  
于 2012-07-23T04:17:11.007 回答