2013-10-17 4 views
0

마우스 입력 클래스를 사용하기 위해 간단한 클릭 게임을 만들고 있습니다.마우스 클릭 값이 업데이트되지 않습니다.

그래서 버튼 클래스 (고양이의 임의의 그림입니다 ^^)와 카운터 클래스 (클릭 한 횟수를 셀 수 있습니다)가 있고 게임 자체는 "쿠키 리 커"

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); 
    } 
} 
, 게임의 유형 D

하지만 그것은 공공, 비록 다른 클래스에서 사용할 때 변수가 업데이트되지 않습니다 여기 내 수업입니다

해야 할 일 마우스가 Game1.cs 대신에 클릭합니까?

이 글을 작성하는 데 짧은 시간을 보냈습니다.

미리 감사드립니다.

답변

1

다른 클래스에서 변수를 사용할 때 변수가 업데이트되지 않는 이유는 무엇입니까? 이 변수를 잘못 수집하는 방법에 접근해야합니다.

어떤 변수입니까? 어떤 특별한 호출이 그것을 업데이트해야하지만, 의도 한대로하지는 않습니까?

나는 업데이트하고 처음은 Game1.cs 파일의 객체로 catCounter 클래스를 초기화뿐만 아니라은 Game1.cs 내부의 관련 방법 내에서이 객체에 Update()Draw()를 호출하지만, 단지 언급하여 구성 요소를 그릴 가정 그게 실종 된 경우에 대비해.

Update 방법이 catButton 인 것은 주목할 만합니다. 그냥 그 안에있는 두 줄을 제거하십시오. 아무 곳에 나 마우스 상태를 다시 사용하지 않아도 아무런 문제가 없습니다. 또한 catCounter 업데이트에서 해당 범위 내에서 currentMouseState을 로컬 변수로 다시 정의하면 public MouseState currentMouseState이 유용하지 않고 사용되지 않습니다.

어느 쪽이든, XNA 구성 요소에 대해 몇 가지 유의할 사항이 있습니다. 수업은 현재 주어진 XNA 구조를 따라야하기 때문에, 당신은 당신의 클래스에 대한 DrawableGameComponent을 상속 고려해야합니다

class catCounter: DrawableGameComponent 

을 당신이 할 수 있도록 허용 : 전화를 할 필요가 없도록

Components.Add(new catCounter()); 

그것을 만들 것입니다 어떤 (Initialize, LoadContent, Update, Draw과 같은) 특정 메서드를 재정의해야하지만 수동으로 Update()Draw() 메서드를 수동으로 업데이트해야합니다.

문제가있는 곳을 알려주지 않을 때 문제가있는 곳을 이해하는 것은 유감입니다.

+0

+1 DrawableGameComponent – pinckerman

0

좋아, 더 좋은 방법은 clicked이라는 변수를 만들고 False으로 설정하는 것입니다.그런 다음

:

public void Update(GameTime gameTime) 
    { 
     MouseState currentMouseState = Mouse.GetState(); 

     if (currentMouseState.LeftButton == ButtonState.Pressed) 
     { 
      if (clicked == False && button.buttonRectangle.Contains(new Point(currentMouseState.X, currentMouseState.Y))) 
      { 
       amountOfCats++; 
       clicked = True; 
      } 
     } 
     else 
     { 
      clicked = False 
     } 

     // Not needed - previousMouseState = currentMouseState; 
    } 

NB : 말라가 bool 변수를 클릭 것을 잊지.

이렇게하면 사용자가 마우스를 누르고있는 것을 방지 할 수 있으며 두 번째 마우스 상태 변수에 의존하지 않습니다.

코드가 작동하지 않는 이유는 Mouse.GetState()이 자동으로 업데이트 되었기 때문입니다. 따라서 previousMouseState은 항상 현재 마우스 상태입니다.

도움이 되었기를 바랍니다.

모나

관련 문제