我正在做一個簡單的點擊遊戲,只是爲了讓我的手在鼠標輸入類。因此,我有一個按鈕類(這只是一個貓的^^隨機圖片)和一個計數器類(它統計了我點擊了多少次),遊戲本身將是一個「Cookie點擊者」類型的遊戲; d鼠標點擊值不會更新
但是,當我用它從另一個類,即使它是公共的變量將不會更新,這裏是我的課:
class catButton
{
Texture2D buttonTexture;
Vector2 buttonPosition;
public Rectangle buttonRectangle;
public MouseState currentMouseState;
public MouseState previousMouseState;
public void Initialize()
{
buttonPosition = new Vector2(150, 300);
}
public void LoadContent(ContentManager Content)
{
buttonTexture = Content.Load<Texture2D>("cat");
buttonRectangle = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y, buttonTexture.Width, buttonTexture.Height);
}
public void Update(GameTime gameTime)
{
previousMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
}
public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
{
spriteBatch.Draw(buttonTexture, buttonRectangle, Color.White);
}
}
和我的櫃檯:
class catCounter
{
SpriteFont catfont;
Vector2 counterPosition;
catButton button = new catButton();
public MouseState currentMouseState;
public MouseState previousMouseState;
KeyboardState keyState = Keyboard.GetState();
public float amountOfCats = 0;
public catCounter()
{
}
public void LoadContent(ContentManager Content)
{
counterPosition = new Vector2(205, 155);
catfont = Content.Load<SpriteFont>("counter");
}
public void Update(GameTime gameTime)
{
MouseState currentMouseState = Mouse.GetState();
if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released && button.buttonRectangle.Contains(new Point(currentMouseState.X, currentMouseState.Y)))
{
amountOfCats++;
}
previousMouseState = currentMouseState;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(catfont, "Cats: " + amountOfCats, counterPosition, Color.White);
}
}
我應該怎麼做鼠標點擊Game1.cs
而不是?
我很短時間寫這個抱歉,如果你必須知道什麼告訴我!
在此先感謝!
+1 for DrawableGameComponent – pinckerman