2014-02-10 4 views
2

C# 및 그래픽 라이브러리 SFML을 사용하여 게임을 만드는 과정에서 "이름 '전투기가 현재 컨텍스트에 존재하지 않습니다."라는 오류가 발생했습니다. 위의 오류를 반환하므로 SFML 정적 이벤트 메서드 MouseButtonPressed()에서 개체를 참조하는 방법을 잘 모르겠습니다.동일한 클래스의 main 메소드 외부에있는 객체를 참조하는 방법은 무엇입니까?

나는 코드에서 수행 한 (그리고 아래 주석 처리 한) 객체를 정적으로 선언 할 수 있다는 것을 알고 있습니다. 그러나 게임 내내 알 수없는 개체가 만들어 지므로 여러 인스턴스 필드를 만들지는 않습니다. 가장 좋은 방법은 무엇입니까?

관련 코드 :

namespace Game 
{ 
    public class Program 
    { 
     //I don't want to have to do this 
     //static Unit fighter; 

     Texture textureFighter; 
     static void MouseButtonPressed(object sender, MouseButtonEventArgs e) 
     { 
      if (e.Button == Mouse.Button.Left) 
      { 
       fighter.Move(new Vector2f(Mouse.GetPosition().X, Mouse.GetPosition().Y)); 
      }   
     } 
     public static void Main() 
     { 
      Program myProgram = new Program(); 

      myProgram.textureFighter = new Texture(@"resources\ship_fighter.png"); 

      Unit fighter = new Unit(new Vector2f(100, 100), 0) 
      { 
       Texture = myProgram.textureFighter 
      }; 
      ... 
     } 

    } 
} 
+0

MouseButtonPressed 루틴이 배치 할 전투기 개체를 어떻게 알 수 있습니까? – Sparky

+0

그들은 그렇게하지 않을 것입니다. 그것이 질문입니다. – user3236245

+1

왜 이것을하고 싶지 않습니까? 변수에 액세스하려면 작업중인 범위에서 변수를 볼 수 있어야합니다. –

답변

1

나는 이것이 당신이 의미 이러한 구현 있는지 확실하지 않다 당신이 코드를 시도 할 수?

namespace Game 
{ 
    public class Program 
    { 
     static Program myProgram = new Program(); 
     Unit fighter; 

     Texture textureFighter; 
     static void MouseButtonPressed(object sender, MouseButtonEventArgs e) 
     { 
      if (e.Button == Mouse.Button.Left) 
      { 
       myProgram.fighter.Move(new Vector2f(Mouse.GetPosition().X, Mouse.GetPosition().Y)); 
      }   
     } 
     public static void Main() 
     { 
      myProgram.textureFighter = new Texture(@"resources\ship_fighter.png"); 

      myProgram.fighter = new Unit(new Vector2f(100, 100), 0) 
      { 
       Texture = myProgram.textureFighter 
      }; 
      ... 
     } 

    } 
} 
관련 문제