衝突長方形をリストで機能させようとしています。
だからここにコードがあります:
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 の値と画像の幅と高さを渡そうとするたびに、エラーが返されて許可されません
だから私は宣言します: Rectangle planetRectangle;
そして、初期化するときに、planetRectangle = new Rectangle (planet.position.X、planet.Position.Y、planet.image.Width、planet.image.Height); を実行します。
値を四角形に渡したくないだけです.objectSのリストと衝突するにはどうすればよいですか? どうにかして基本クラスまたはスプライトクラスで作成する必要がありますか?
私が得るエラーは次のとおりです。
float を int に変換できず ( Planet.position.X&Y に関して)、最適なオーバーロード マッチに引数があります。問題のある行を強調表示しました。
通常、これらの惑星オブジェクトは同心円状に移動します。タッチで選択できるようにしたいのですが、そのためには最初に長方形を作成する必要があります。