2013-11-25 6 views
1

loadContent 메서드 외부에서 아무 텍스처 나 인식되지 않는 문제가 있습니다."textureName"이 현재 컨텍스트에 존재하지 않습니다.

protected override void LoadContent() 
{ 
    spriteBatch = new SpriteBatch(GraphicsDevice); 
    Texture2D tileStart = Content.Load<Texture2D>("tile_start"); 
    Texture2D tileCrossJunction = Content.Load<Texture2D>("tile_crossjunction"); 
    Texture2D tileTJunction = Content.Load<Texture2D>("tile_t-junction"); 
    Texture2D tileCorner = Content.Load<Texture2D>("tile_corner"); 
    Texture2D tileHallway = Content.Load<Texture2D>("tile_hallway"); 
    Texture2D tileDeadEnd = Content.Load<Texture2D>("tile_deadend"); 
    Texture2D sqrPlayer = Content.Load<Texture2D>("sqr_player"); 
    Texture2D sqrBaddieSmall = Content.Load<Texture2D>("sqr_baddie_small"); 
    Texture2D sqrBaddie = Content.Load<Texture2D>("sqr_baddie"); 
    Texture2D sqrBaddieLarge = Content.Load<Texture2D>("sqr_baddie_large"); 
} 

이 방법 없음 문제,하지만 ... 내 그리기 방법이 텍스처 중 하나를 참조하려고 할 때이 오류가

protected override void Draw(GameTime gameTime) 
{ 
    GraphicsDevice.Clear(Color.DarkGray); 
    base.Draw(gameTime); 
    spriteBatch.Begin(); 
    spriteBatch.Draw(tileStart, new Vector2(0,0), Color.White); 
    spriteBatch.End(); 
} 

는 "tileStart는 현재 컨텍스트에 존재하지 않습니다 . "

일반적으로 tileStartLoadContent 메서드 내에서 선언되는 변수이므로 다른 곳에서는 사용할 수 없으므로 인식되지 않는다고 말할 수 있습니다. 제가 혼란스러워하는 이유는 제가 읽은 모든 튜토리얼은이 정확한 문법을 ​​보여 주며 그러한 경우에 잘 작동하는 것처럼 보입니다. 분명히 여기에 내가 이해하지 못하는 다른 것이 있습니다.

여러분이 제공 할 수있는 도움이 있으면 대단히 감사하겠습니다.

+0

사용중인 튜토리얼에 대한 링크를 게시하는 경우이를 파악하려고합니다. 그 외, 위에 게시 한 코드가 정확한 경우 (내 대답 참조) 단순히 불가능합니다. – Colton

답변

0

C#에서는 변수의 "범위"가 잘 정의되어 있습니다. 코드에서 텍스처는 메서드 "LoadContent"의 범위 내에서 만들어지고 메서드가 완료되면 삭제됩니다. 당신이해야 할 것은 이렇게 같은 "클래스"수준에서 텍스처를 장소 :

//outside of the method, and in general, should be placed near the top of the class  
Texture2D tileStart; 
Texture2D tileCrossJunction; 
Texture2D tileTJunction; 
Texture2D tileCorner; 
Texture2D tileHallway; 
Texture2D tileDeadEnd; 
Texture2D sqrPlayer; 
Texture2D sqrBaddieSmall; 
Texture2D sqrBaddie; 
Texture2D sqrBaddieLarge; 

protected override void LoadContent() 
{ 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    //be sure to remove Texture2D from these 
    //this will insure that the "class" level variables are called 
    tileStart = Content.Load<Texture2D>("tile_start"); 
    tileCrossJunction = Content.Load<Texture2D>("tile_crossjunction"); 
    tileTJunction = Content.Load<Texture2D>("tile_t-junction"); 
    tileCorner = Content.Load<Texture2D>("tile_corner"); 
    tileHallway = Content.Load<Texture2D>("tile_hallway"); 
    tileDeadEnd = Content.Load<Texture2D>("tile_deadend"); 
    sqrPlayer = Content.Load<Texture2D>("sqr_player"); 
    sqrBaddieSmall = Content.Load<Texture2D>("sqr_baddie_small"); 
    sqrBaddie = Content.Load<Texture2D>("sqr_baddie"); 
    sqrBaddieLarge = Content.Load<Texture2D>("sqr_baddie_large"); 
} 

당신이 그 일을하면, 변수의 "범위"클래스 수준에있을 것입니다, 당신은 할 수 수업 내에서 다른 방법으로 사용할 수 있습니다.

즉, 해당 메서드 외부에서 메서드 내에서 선언 된 변수에 액세스 할 수있는 방법이 없습니다 (물론 다른 메서드에 매개 변수로 전달하지 않고). 찾고있는 자습서는 짧은 손, 그리고 당신이 그것을 "적절하게"할 것을 기대합니다.

+0

고마워요. 매력처럼 일했다. 나는 'LoadContent'메소드 내에서 선언 할 텍스쳐를 저장하기 위해 사용했던 변수들에 대해 인상을 받았다. 실수를 수정하기 위해, 나는 메인 클래스에서 네임 스페이스를 미리 선언했다. 올바른 방향으로 나를 가리켜 주신 것에 대해 다시 한번 감사 드리며 앞으로도 비슷한 간단한 문제를 많이 가질 것입니다. – KWiP

관련 문제