Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为WP7在C#XNA中的列表中创建对象冲突_C#_Windows Phone 7_Xna - Fatal编程技术网

为WP7在C#XNA中的列表中创建对象冲突

为WP7在C#XNA中的列表中创建对象冲突,c#,windows-phone-7,xna,C#,Windows Phone 7,Xna,我试图使一个冲突矩形与一个列表一起工作 下面是代码: List<Planet> planets; Planet planet; Texture2D levelSelectTexture; Texture2D star; Vector2 position; Rectangle planetRectangle; public PlanetSelect(EventHandler screenEvent, GraphicsDevice graphics, ContentManager

我试图使一个冲突矩形与一个列表一起工作

下面是代码:

  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);
}
列出行星;
行星;
纹理2d水平选择纹理;
纹理2d星;
矢量2位置;
矩形平面三角形;
公共平面选择(EventHandler screenEvent、GraphicsDevice graphics、ContentManager内容、SpriteBatch批处理)
:基本(屏幕事件、图形、内容、批次)
{
星号=content.Load(“熊星”);
位置=新矢量2((Graphics.Viewport.Width-star.Width)/2,(Graphics.Viewport.Height-star.Height)/2);
行星=新列表();
行星=新行星(内容,批次);
行星载荷(“planet0”);
行星速度=0.04f;
行星半径=80;
行星角度=90度;
行星。添加(行星);
行星=新行星(内容、批次);
行星载荷(“planet2”);
行星速度=0.02f;
行星半径=135;
行星角度=120;
行星。添加(行星);
行星=新行星(内容、批次);
行星载荷(“planet3”);
行星速度=0.009f;
行星半径=180;
行星角度=MathHelper.ToRadians(160);
行星。添加(行星);
**planetRectangle=新矩形(planet.position.X,planet.position.Y,planet.image.Width,planet.image.Height)**
levelSelectTexture=content.Load(“levelSelectMenu”);
}
公共覆盖无效更新(游戏时间游戏时间)
{
TouchCollection TouchCollection=TouchPanel.GetState();
foreach(touchCollection中的TouchLocation tl)
{
if(tl.State==TouchLocationState.Moved)
{
如果(planetRectangle.包含((int)tl.Position.X,(int)tl.Position.Y))
{
调用(这个,新的EventArgs());
}
}
foreach(行星中的行星)
{
行星角度+=行星速度;
浮动轨道x=(浮动)(数学Cos(行星角度)*行星半径);
浮动轨道=(浮动)(数学Sin(行星角度)*行星半径);
float x=(Graphics.Viewport.Width-planet.image.Width)/2+orbitx;
浮动y=(Graphics.Viewport.Height-planet.image.Height)/2+轨道;
行星位置=新矢量2(x,y);
}
更新(游戏时间);
}
公共覆盖无效绘图(SpriteBatch SpriteBatch)
{
spriteBatch.Draw(levelSelectTexture,Vector2.Zero,Color.White);
foreach(行星中的行星)
{
planet.Draw();
}
绘制(星形、位置、颜色、白色);
基础绘制(spriteBatch);
}
每次我尝试创建一个碰撞矩形并传入planet.position值以及图像宽度和高度时,它都会抛出一个错误,不允许我这样做

所以我宣布:长方形,行星,三角形

然后当我初始化时,我做planetRectangle=新的矩形(planet.position.X,planet.position.Y,planet.image.Width,planet.image.Height)

它只是不想将值传递到矩形上,如何与对象列表发生冲突?我应该在基类中还是在sprite类中创建它

我得到的错误是:

无法将float转换为int(关于planet.position.X&Y),并且最佳重载匹配有争议。我突出显示了它有问题的行


通常,这些行星对象以同心圆的形式移动,我希望能够通过触摸来选择它们,但要做到这一点,我需要先创建一个矩形。

您需要将行星值强制转换为
int
以将其传递到
矩形中:

planetRectangle = new Rectangle((int)planet.position.X, (int)planet.position.Y, (int)planet.image.Width, (int)planet.image.Height);  

您需要将
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...
            }
        }
    }
}

这似乎解决了语法错误,但我的触摸不起作用,它位于updateThank中谢谢你的帮助!现在就试试看!效果很好,非常感谢,如果我创建一个名称相同的列表,我如何指定哪个行星是哪个行星?
if(planet.Load(“planet0”)){//或者:if(GetPlanetRectangle(planet.Contains(…))if(planet.Bounds.Contains((int)tl.Position.X,(int)tl.Position.Y)){changeScreenDelegate(ScreenState.Menu);}
尝试此方法无效,它允许触摸所有纹理,当我返回时,所有纹理都已更改为特定纹理。听起来你做错了什么。建议你问一个新问题。