2012-06-15 3 views
0

최근 MonoMac에서 XNA를 사용하기 시작했습니다. 수업에 문제가 있습니다. 내부에 텍스처와 위치 정보가 포함 된 클래스를 만들고 싶습니다. 또한 그리기 기능을 만들고 싶습니다.XNA는 SpriteBatch와 Content를 참조로 사용합니다.

내 아이디어는 spriteBatch와 Content arround를 전달하여 텍스처를로드하고 나중에 그릴 수 있도록하는 것이 었습니다. 그러나 Content 객체는이 객체에 전달되지 않고 어떤 텍스처도로드하지 않습니다. 클래스 외부에서 Content를 시도하면 텍스처가 잘로드되어 텍스처가 있어야합니다.

public class TPlayer { 
     public Texture2D[] textures; 
     public ContentManager Content; 
     public SpriteBatch spriteBatch; 
     public int currentFrame; 
     public Rectangle position; 
     public TPlayer(ContentManager ccontent, SpriteBatch cspritebatch){ 
      this.Content = ccontent; 
      this.spriteBatch = cspritebatch; 
      this.Content.RootDirectory = "Content"; 
      this.currentFrame = 0; 
      this.position = new Rectangle(250,20,100,150); 
      this.LoadContent(); 
     } 

     protected void LoadContent(){ 
      this.textures[0] = this.Content.Load<Texture2D>("Textures/ToughStanding"); 
      this.textures[1] = this.Content.Load<Texture2D>("Textures/ToughWalking1"); 
      this.textures[2] = this.Content.Load<Texture2D>("Textures/ToughWalking2"); 
     } 

     public void Draw(){ 
      spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.AlphaBlend); 
      this.spriteBatch.Draw (textures[0], this.position, Color.White); 
      this.spriteBatch.End(); 
     } 

이 내가 인스턴스를 만드는 방법은 다음과 같습니다

Player = new TPlayer(this.Content,this.spriteBatch); 

어쩌면 임 잘못된 모델을 사용하려고 ... 어쩌면 내가 클래스 내부의 SpriteBatch 및 콘텐츠를 사용하려면 야해하지만,보다 내가 할 수의 SpriteBatch 및 콘텐츠 글로벌? 당신이 당신의 자신의 문제를 해결 한 이후

당신의 도움이

+0

TPlayer 생성자에 들어가기 전에 Content.RootDirectory의 값은 무엇입니까? 생성자 내부에서 값을 설정하고 있는지 확인해야합니다. – Andy

+0

또한 (당신의 문제와 관련없는) 팁으로서 나는 플레이어를 끌어 들이고, spritebatch.begin을 호출하고, 각 무승부 방법보다는 루프의 양쪽을 끝내는 루프가 있다고 생각합니다. 상당히. – Andy

+0

아니, 나는 그렇게 할 필요가 없다고 생각한다. 전에 "Content"로 설정되었습니다. 나는 그저 문제를 일으킬 수있는 곳을 추측하려고 노력했기 때문에 나는 그것을 시도했다. spritebatch advice에 감사드립니다 ... 지금부터 한 번만 Begin and End를 호출 할 것입니다. – Simon

답변

2

주셔서 감사합니다 (좋은 일을!) 나는 또한 문제가 해결됩니다 스프라이트 프레임을 그리기의 약간 적은 리소스를 많이 사용하는 방법을 제안하고이 공간을 사용합니다 애니메이션의 각 프레임에 대해 별도의 텍스처를 사용하는 대신 하나의 텍스처에 결합 할 수 있습니다. 텍스처 교환은 실제로 비교적 느린 작업입니다.

대신 Standing, Walking1, Walking2에 대한 3 개의 텍스처 대신 현재 프레임 속성을 사용하여 전환 할 수있는 큰 텍스처를 만들 수 있습니다.이 텍스처를 모두 3 개 보유 할 수 있습니다. 페인트 또는 모든 도면 패키지에서 간단하게 3 개 프레임과 사본의 크기가/장소에 붙여 넣을 빈 이미지 당신은 각각의 위치를 ​​유지하기 위해 사각형 배열을 만들 수 있습니다

enter image description here

(각각의 시작/끝 위치의 메모를 복용) 스프라이트 시트에.

그러면 스프라이트에 애니메이션을 적용하면 현재 사각형이 어떤 것인지 쉽게 알 수 있습니다.

전체 게임이 스프라이트 및 플레이어 클래스와 함께 작동하는 방법을 보여주는 빠른 게임 클래스를 첨부했습니다. 스프라이트 클래스는 플레이어뿐 아니라 모든 스프라이트의 기반이 될 수 있습니다.

#region Using Directives. 
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; 
#endregion 

namespace sprites 
{ 
    public class Sprite 
    { 
     // Texture instances. 
     public Texture2D spriteSheet; 
     protected Rectangle[] spriteSheetRegions; 
     protected Rectangle currentSpriteSheetRegion; 

     // player instances. 
     public Rectangle location; 

     // call this to change the image that's drawn for the sprite. 
     public void SetSpriteSheetIndex(int index) 
     { 
      currentSpriteSheetRegion = spriteSheetRegions[index]; 
     } 
    } 

    public class TPlayer : Sprite 
    { 
     public TPlayer() 
     { 
      // Since your sprite sheets for the player are fixed we can set them up here. 
      spriteSheetRegions = new Rectangle[] 
      { 
       new Rectangle (0,0, 50,50),  // standing. 
       new Rectangle (50,0, 100, 50), // tough walking 1 
       new Rectangle (100,0, 150, 50), // tough walking 2 
      }; 
      currentSpriteSheetRegion = spriteSheetRegions[0]; 
     } 

     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(spriteSheet, location, currentSpriteSheetRegion, Color.White); 
     } 
    } 

    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 

     List<TPlayer> players; 

     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 

     protected override void Initialize() 
     { 
      // Create the players and add 3 of them. 
      players = new List<TPlayer>(); 
      players.Add(new TPlayer() { location = new Rectangle(10, 10, 100, 100) }); 
      players.Add(new TPlayer() { location = new Rectangle(110, 10, 100, 100) }); 
      players.Add(new TPlayer() { location = new Rectangle(220, 10, 100, 100) }); 

      base.Initialize(); 
     } 

     protected override void LoadContent() 
     { 
      // Create a new SpriteBatch, which can be used to draw textures. 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      // Load up the players content. 
      Texture2D playerSpriteSheet = Content.Load<Texture2D>("PlayerSpriteSheet"); 

      // each player gets a reference to the same texture so there is no duplication. 
      for (int i = 0; i < players.Count; i++) 
       players[i].spriteSheet = playerSpriteSheet; 
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 

      // draw the players. 
      spriteBatch.Begin(); 
      for (int i = 0; i < players.Count; i++) 
       players[i].Draw(spriteBatch); 
      spriteBatch.End(); 

      base.Draw(gameTime); 
     } 
    } 
} 

플레이어의 현재 프레임을 변경하려면 SetSpriteSheetIndex를 호출합니다.

플레이어 0의 그림을 힘들게 걷는 1 프레임으로 설정하려면 전화를 겁니다.

players[0].SetSpriteSheetIndex(1); 
+0

고맙습니다 ... 나는 지금부터 당신의 길을 할 것입니다. – Simon