주셔서 감사합니다 (좋은 일을!) 나는 또한 문제가 해결됩니다 스프라이트 프레임을 그리기의 약간 적은 리소스를 많이 사용하는 방법을 제안하고이 공간을 사용합니다 애니메이션의 각 프레임에 대해 별도의 텍스처를 사용하는 대신 하나의 텍스처에 결합 할 수 있습니다. 텍스처 교환은 실제로 비교적 느린 작업입니다.
대신 Standing, Walking1, Walking2에 대한 3 개의 텍스처 대신 현재 프레임 속성을 사용하여 전환 할 수있는 큰 텍스처를 만들 수 있습니다.이 텍스처를 모두 3 개 보유 할 수 있습니다. 페인트 또는 모든 도면 패키지에서 간단하게 3 개 프레임과 사본의 크기가/장소에 붙여 넣을 빈 이미지 당신은 각각의 위치를 유지하기 위해 사각형 배열을 만들 수 있습니다
![enter image description here](https://i.stack.imgur.com/fgB1g.png)
(각각의 시작/끝 위치의 메모를 복용) 스프라이트 시트에.
그러면 스프라이트에 애니메이션을 적용하면 현재 사각형이 어떤 것인지 쉽게 알 수 있습니다.
전체 게임이 스프라이트 및 플레이어 클래스와 함께 작동하는 방법을 보여주는 빠른 게임 클래스를 첨부했습니다. 스프라이트 클래스는 플레이어뿐 아니라 모든 스프라이트의 기반이 될 수 있습니다.
#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);
TPlayer 생성자에 들어가기 전에 Content.RootDirectory의 값은 무엇입니까? 생성자 내부에서 값을 설정하고 있는지 확인해야합니다. – Andy
또한 (당신의 문제와 관련없는) 팁으로서 나는 플레이어를 끌어 들이고, spritebatch.begin을 호출하고, 각 무승부 방법보다는 루프의 양쪽을 끝내는 루프가 있다고 생각합니다. 상당히. – Andy
아니, 나는 그렇게 할 필요가 없다고 생각한다. 전에 "Content"로 설정되었습니다. 나는 그저 문제를 일으킬 수있는 곳을 추측하려고 노력했기 때문에 나는 그것을 시도했다. spritebatch advice에 감사드립니다 ... 지금부터 한 번만 Begin and End를 호출 할 것입니다. – Simon