C# 鼠标点击问题
我的鼠标点击有问题,我的代码就是这样的,我的问题是,当我点击某个东西时,它应该进入下一页并绘制其他一些东西,例如,当我点击一个系统图标时,它应该进入系统图片屏幕并绘制它的行星,当我再次点击一个行星时,它应该出现在行星表面上,但不幸的是,如果一个行星的坐标与一个行星的坐标相同系统,当我点击该系统时,它会直接进入行星表面。我使用了很多方法,例如在更新开始和结束时使用了两个mousestate,但都不起作用。有人能帮我吗C# 鼠标点击问题,c#,xna,C#,Xna,我的鼠标点击有问题,我的代码就是这样的,我的问题是,当我点击某个东西时,它应该进入下一页并绘制其他一些东西,例如,当我点击一个系统图标时,它应该进入系统图片屏幕并绘制它的行星,当我再次点击一个行星时,它应该出现在行星表面上,但不幸的是,如果一个行星的坐标与一个行星的坐标相同系统,当我点击该系统时,它会直接进入行星表面。我使用了很多方法,例如在更新开始和结束时使用了两个mousestate,但都不起作用。有人能帮我吗 using System; using System.Colle
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;
}
}
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);
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
名称空间航天器
{
///
///这是游戏的主要类型
///
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
纹理2D按钮,背景,背景2,标题,光标,SolarBG,PlanetBG;
矢量2游标;
矩形标题页,BGpos;
SpriteFont字体;
声效MenuSound;
SoundEffectInstance菜单SoundInstance;
bool MenuIsTrue=真;
bool NewGame=false;
Random rand=新的Random();
鼠标1、鼠标2、鼠标3、鼠标4、鼠标5;
键盘状态当前键盘,最新键盘;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
graphics.PreferredBackBufferHeight=600;
graphics.PreferredBackBufferWidth=800;
Content.RootDirectory=“Content”;
}
text按钮[]菜单按钮;
银河系;
受保护的覆盖无效初始化()
{
int systemnumber=rand.Next(2,8);
int planetnumber=rand.Next(3,10);
Player player1=新玩家();
Player player2=新玩家();
int PicNumber=rand.Next(1,10);
星系=新星系(系统编号);
宇宙飞船[]k飞船=新宇宙飞船[12];
宇宙飞船[]飞船=新的宇宙飞船[12];
对于(int i=1;i<12;i++)
{
kships[i]=(新宇宙飞船(i,i,(i<6)?i*1000+1000:i*i*1000,Content.Load(“图片/宇宙飞船/k”+i.ToString()));
rships[i]=(新宇宙飞船(i,i,(i<6)?i*1000+1000:i*i*1000,Content.Load(“图片/宇宙飞船/r”+i.ToString()));
}
菜单按钮=新建文本按钮[3];
对于(int i=0;i<3;i++)
{
菜单按钮[i]=新建文本按钮();
菜单按钮[i].rpos=新矩形(200100*i+27040080);
菜单按钮[i].color=color.LightGray;
菜单按钮[i].pos=newvector2(250120*i+150);
菜单按钮[i].pic=Content.Load(“图片/按钮/b1”);
}
菜单按钮[0]。name=“新游戏”;
菜单按钮[1].name=“Options”;
菜单按钮[2]。name=“退出”;
BGpos=新矩形(0,0,800600);
titlepos=新矩形(100,50593133);
光标=Content.Load(“图片/按钮/指针”);
base.Initialize();
}
受保护的覆盖void LoadContent()
{
PlanetBG=Content.Load(“图片/行星/PlanetBG”);
SolarBG=Content.Load(“图片/行星/空间”);
BG=Content.Load(“图片/行星/星系”);
BG2=Content.Load(“图片/行星/银河2”);
title=Content.Load(“图片/按钮/标题”);
spriteBatch=新spriteBatch(图形设备);
Font=Content.Load(“字体/分数字体”);
按钮=内容。加载(“图片/按钮/b1”);
MenuSound=Content.Load(“声音/星球大战”);
MenuSoundInstance=MenuSound.CreateInstance();
MenuSoundInstance.isLopped=true;
MenuSoundInstance.Play();
对于(int i=0;i对于所有鼠标按钮事件,您应该跟踪上一个更新帧的状态,以便(在您的情况下)只有在最初按下鼠标按钮时才能触发菜单事件。因此,如果(mouse1.LeftButton==ButtonState.pressed)
您将有如下内容:
if (mouse1.LeftButton == ButtonState.Pressed && prevMouse1.LeftButton != ButtonState.Pressed)
{
// ...
}
prevMouse1 = mouse1; // Update the state for the next frame.
我建议将此功能包装到它自己的类中
还考虑到当前基于标准鼠标的用户界面行为是,只有当鼠标指针在控件上时按下并释放鼠标按钮时才激活控件,并且只有在鼠标被释放后才激活。