Im using Xna and C#.
Im trying to have specific objects in my list doing different things.
The way ive written it out at the moment it selects all the textures in my list and all of them do the same thing, if i try and select a certain texture all of them still do the same thing:
if (planet.Load("planet0")) { if (planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y)) { changeScreenDelegate(ScreenState.Menu); } }
Im basically trying to make each selection go to a different screen.
Heres the rest of my class:
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();
}
}
}
Give your planet class another property called ID, then give each planet a different ID when initialising them. Then in your foreach planet in planets loop, you can say
if(planet.ID == 0){ if (planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y)) { changeScreenDelegate(ScreenState.Menu); } }
Do this for each possible ID number, and change the screenstate to whatever screen state you want it to switch to.
Plus you could enumerate the ID numbers to make it clear as to which planet is being referenced.