我使用 Xna 和 C#。
我试图让我的列表中的特定对象做不同的事情。
我现在写出来的方式是它选择了我列表中的所有纹理,并且它们都做同样的事情,如果我尝试选择某个纹理,它们仍然做同样的事情:
if (planet.Load("planet0")) { if (planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y)) { changeScreenDelegate(ScreenState.Menu); } }
我基本上试图让每个选择进入不同的屏幕。
这是我班的其余部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace PlanetDrill2
{
class StarSystemUrsa : Screen
{
Texture2D starSystemTexture;
List<Planet> planets;
Texture2D star;
Vector2 position;
Texture2D previousButton;
public StarSystemUrsa(Game game, SpriteBatch spriteBatch, ChangeScreen changeScreen)
: base(game, batch, changeScreen)
{
position = new Vector2((480 - 295) / 2, (800 - 295) / 2);
planets = new List<Planet>();
Planet planet = new Planet(content, spriteBatch);
planet.Load("planet0");
planet.velocity = 0.04f;
planet.radius = 80;
planet.angle = MathHelper.ToRadians(90);
planets.Add(planet);
planet = new Planet(content, spriteBatch);
planet.Load("planet4");
planet.velocity = 0.02f;
planet.radius = 135;
planet.angle = MathHelper.ToRadians(120);
planets.Add(planet);
planet = new Planet(content, spriteBatch);
planet.Load("planet2");
planet.velocity = 0.009f;
planet.radius = 180;
planet.angle = MathHelper.ToRadians(160);
planets.Add(planet);
}
protected override void SetupInputs()
{
base.SetupInputs();
}
public override void Activate()
{
base.Activate();
}
protected override void LoadScreenContent(ContentManager content)
{
starSystemTexture = content.Load<Texture2D>("levelSelectMenu");
star = content.Load<Texture2D>("ursaeMajorisStar");
previousButton = content.Load<Texture2D>("previousButton2");
base.LoadScreenContent(content);
}
protected override void UpdateScreen(GameTime gameTime, DisplayOrientation screenOrientation)
{
TouchCollection touchCollection = TouchPanel.GetState();
foreach(TouchLocation tl in touchCollection)
{
if(tl.State == TouchLocationState.Pressed) // 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))
{
changeScreenDelegate(ScreenState.Menu);
}
}
}
TouchPanel.EnabledGestures = GestureType.DoubleTap | GestureType.VerticalDrag;
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
switch (gesture.GestureType)
{
case GestureType.DoubleTap:
changeScreenDelegate(ScreenState.launchScreen);
break;
case GestureType.VerticalDrag:
break;
default:
break;
}
}
}
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 = (game.GraphicsDevice.Viewport.Width - planet.image.Width) / 2 + orbitx;
float y = (game.GraphicsDevice.Viewport.Height - planet.image.Height) / 2 + orbity;
planet.position = new Vector2(x, y);
}
base.UpdateScreen(gameTime, screenOrientation);
}
protected override void DrawScreen(SpriteBatch batch, DisplayOrientation screenOrientation)
{
batch.Draw(starSystemTexture, Vector2.Zero, Color.White);
foreach (Planet planet in planets)
{
planet.Draw();
}
batch.Draw(star,position, Color.White);
batch.Draw(previousButton, new Vector2(240, 750), Color.White);
base.DrawScreen(batch, screenOrientation);
}
protected override void SaveScreenState()
{
base.SaveScreenState();
}
}
}