I have a problem with mouse clicks,This is how my code looks like,My problem is that when i click on something it should goes to the next page and draw couple of other things for example when I click on a System icon it should goes to the System picture screen and draw its planets and again when i click on a planet it should goes on the planet surface but unfortunately if a planet coordinates is the same as a System,when i click on the system it goes straight to the planet surface.I have used many methods for example two mousestates at the begin and the end of Update but none of them worked.Can some one help me?
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
namespace SpaceCraft
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D Button, BG, BG2, title, cursor, SolarBG, PlanetBG;
Vector2 cursorpos;
Rectangle titlepos, BGpos;
SpriteFont Font;
SoundEffect MenuSound;
SoundEffectInstance MenuSoundInstance;
bool MenuIsTrue = true;
bool NewGame = false;
Random rand = new Random();
MouseState mouse1, mouse2, mouse3, mouse4, mouse5;
KeyboardState CurrentKeyboard, LastKeyboard;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 800;
Content.RootDirectory = "Content";
}
TextButton[] menuButtons;
Galaxy galaxy;
protected override void Initialize()
{
int systemnumber = rand.Next(2, 8);
int planetnumber = rand.Next(3, 10);
Player player1 = new Player();
Player player2 = new Player();
int PicNumber = rand.Next(1, 10);
galaxy = new Galaxy(systemnumber);
SpaceShip[] kships = new SpaceShip[12];
SpaceShip[] rships = new SpaceShip[12];
for (int i = 1; i < 12; i++)
{
kships[i] = (new SpaceShip(i, i, (i < 6) ? i * 1000 + 1000 : i * i * 1000, Content.Load<Texture2D>("Pictures/SpaceShips/k" + i.ToString())));
rships[i] = (new SpaceShip(i, i, (i < 6) ? i * 1000 + 1000 : i * i * 1000, Content.Load<Texture2D>("Pictures/SpaceShips/r" + i.ToString())));
}
menuButtons = new TextButton[3];
for (int i = 0; i < 3; i++)
{
menuButtons[i] = new TextButton();
menuButtons[i].rpos = new Rectangle(200, 100 * i + 270, 400, 80);
menuButtons[i].color = Color.LightGray;
menuButtons[i].pos = new Vector2(250, 120 * i + 150);
menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b1");
}
menuButtons[0].name = "New Game";
menuButtons[1].name = " Options";
menuButtons[2].name = " Exit";
BGpos = new Rectangle(0, 0, 800, 600);
titlepos = new Rectangle(100, 50, 593, 133);
cursor = Content.Load<Texture2D>("Pictures/buttons/Pointer");
base.Initialize();
}
protected override void LoadContent()
{
PlanetBG = Content.Load<Texture2D>("Pictures/Planets/planetbg");
SolarBG = Content.Load<Texture2D>("Pictures/Planets/space");
BG = Content.Load<Texture2D>("Pictures/Planets/Galaxy");
BG2 = Content.Load<Texture2D>("Pictures/Planets/Galaxy2");
title = Content.Load<Texture2D>("Pictures/buttons/title");
spriteBatch = new SpriteBatch(GraphicsDevice);
Font = Content.Load<SpriteFont>("Fonts/scorefont");
Button = Content.Load<Texture2D>("Pictures/Buttons/b1");
MenuSound = Content.Load<SoundEffect>("Sounds/Star Wars");
MenuSoundInstance = MenuSound.CreateInstance();
MenuSoundInstance.IsLooped = true;
MenuSoundInstance.Play();
for(int i=0;i<galaxy.systems.Length;i++)
{
galaxy.systems[i].Pic = Content.Load<Texture2D>("Pictures/Planets/solar_system2");
for (int j = 0; j < galaxy.systems[i].planets.Length; j++)
galaxy.systems[i].planets[j].Pic = Content.Load<Texture2D>("Pictures/Planets/" + rand.Next(1,10).ToString());
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
mouse1 = Mouse.GetState();
cursorpos = new Vector2(mouse1.X, mouse1.Y);
CurrentKeyboard = Keyboard.GetState();
for (int j = 0; j < galaxy.systems.Length; j++)
for (int i = 0; i < galaxy.systems[j].planets.Length; i++)
{
if (galaxy.systems[j].planets[i].Pos.Contains(mouse1.X, mouse1.Y) && mouse1.LeftButton == ButtonState.Pressed && NewGame == false && galaxy.systems[j].SolarIsTrue == false)
{
galaxy.systems[j].planets[i].PlanetIsTrue = true;
galaxy.systems[j].SolarIsTrue = false;
NewGame = false;
break;
}
}
for (int j = 0; j < galaxy.systems.Length; j++)
{
if (galaxy.systems[j].Pos.Contains(mouse1.X, mouse1.Y) && mouse1.LeftButton == ButtonState.Pressed && NewGame == true)
{
galaxy.systems[j].SolarIsTrue = true;
break;
}
}
for (int i = 0; i < 3; i++)
{
if (menuButtons[i].rpos.Contains(mouse1.X, mouse1.Y) && MenuIsTrue == true && NewGame == false)
{
if (mouse1.LeftButton == ButtonState.Pressed )
{
if (i == 2 && MenuIsTrue == true) Exit();
if (i == 1 && MenuIsTrue == true) { MenuIsTrue = false; }
if (i == 0 && MenuIsTrue == true) { MenuIsTrue = false; MenuSoundInstance.Stop(); NewGame = true; }
menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b1");
menuButtons[i].pos = new Vector2(330, 100 * i + 292);
}
else
{
menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b2");
menuButtons[i].pos = new Vector2(330, 100 * i + 290);
}
}
else
{
menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b1");
menuButtons[i].pos = new Vector2(330, 100 * i + 292);
}
}
for (int i = 0; i < galaxy.systems.Length; i++)
{
if (galaxy.systems[i].SolarIsTrue == true && CurrentKeyboard.IsKeyDown(Keys.Escape)&&LastKeyboard.IsKeyUp(Keys.Escape))
{
galaxy.systems[i].SolarIsTrue = false;
NewGame = true;
break;
开发者_Python百科 }
}
for (int j = 0; j < galaxy.systems.Length; j++)
if (galaxy.systems[j].SolarIsTrue == true && CurrentKeyboard.IsKeyDown(Keys.Escape) && LastKeyboard.IsKeyUp(Keys.Escape))
{
galaxy.systems[j].SolarIsTrue = false;
NewGame = true;
break;
}
for (int j = 0; j < galaxy.systems.Length; j++)
for (int i = 0; i < galaxy.systems[j].planets.Length; i++)
{
if (CurrentKeyboard.IsKeyDown(Keys.Escape) && LastKeyboard.IsKeyUp(Keys.Escape) && galaxy.systems[j].planets[i].PlanetIsTrue == true)
{
galaxy.systems[j].planets[i].PlanetIsTrue = false;
galaxy.systems[j].SolarIsTrue = true;
break;
}
}
if (CurrentKeyboard.IsKeyDown(Keys.Escape) && LastKeyboard.IsKeyUp(Keys.Escape) && NewGame == true)
{
MenuIsTrue = true;
NewGame = false;
MenuSoundInstance.Play();
}
LastKeyboard = Keyboard.GetState();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Navy);
Random rand = new Random();
spriteBatch.Begin();
spriteBatch.Draw(BG, BGpos, Color.White);
if (NewGame)
{
spriteBatch.Draw(BG2, BGpos, Color.White);
for (int i = 0; i < galaxy.systems.Length; i++)
{
spriteBatch.Draw(galaxy.systems[i].Pic, galaxy.systems[i].Pos, Color.Wheat);
}
}
for (int i = 0; i < galaxy.systems.Length; i++)
{
if (galaxy.systems[i].SolarIsTrue)
{
spriteBatch.Draw(SolarBG, new Vector2(0, 0), Color.White);
for (int j = 0; j < galaxy.systems[i].planets.Length; j++)
spriteBatch.Draw(galaxy.systems[i].planets[j].Pic, galaxy.systems[i].planets[j].Pos, Color.White);
}
}
if(MenuIsTrue)
{
spriteBatch.Draw(BG, BGpos, Color.White);
spriteBatch.Draw(title, titlepos, Color.White * 0.7f);
for (int i = 0; i < 3; i++)
{
spriteBatch.Draw(menuButtons[i].pic, menuButtons[i].rpos, menuButtons[i].color * 0.7f);
spriteBatch.DrawString(Font, menuButtons[i].name, menuButtons[i].pos, menuButtons[i].color * 0.7f);
}
}
for (int i = 0; i < galaxy.systems.Length; i++)
for (int j = 0; j < galaxy.systems[i].planets.Length; j++)
if (galaxy.systems[i].planets[j].PlanetIsTrue == true)
{
spriteBatch.Draw(PlanetBG, new Vector2(0, 0), Color.White);
}
MouseState mouse = Mouse.GetState();
spriteBatch.Draw(cursor, cursorpos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
For all mouse button events, you should be keeping track of the previous update frame's state, so that (in your case) you can trigger your menu event only when the mouse button is initially pressed. So, instead of if (mouse1.LeftButton == ButtonState.Pressed)
you would have something like the following:
if (mouse1.LeftButton == ButtonState.Pressed && prevMouse1.LeftButton != ButtonState.Pressed)
{
// ...
}
prevMouse1 = mouse1; // Update the state for the next frame.
I would recommend wrapping this functionality in its own class.
Also consider that the current standard mouse-based behavior for user interfaces is that a control is activated only if the mouse button is both pressed and released when the cursor is over the control--and only after the mouse is released.
精彩评论