这是我的精灵为我的游戏移动的代码。我在线查看了教程和课程,以帮助我解决这个问题。我需要知道每次按下某个键时如何更改角色精灵。 (例如,左键:左侧面向精灵,右侧键:右侧面向精灵)我已经看过线但是我;我不知道如何将它集成到我的代码中。我相信我可以使用我的“MageChar”类中的枚举列表来改变精灵,但我不知道如何开始。任何帮助表示赞赏。
游戏类
namespace RPG
{
public class MageChar : charMovement
{
//variable where sprite file name is stored
const string MageAssetName = "MageChar";
//starting x position
const int StartPositionX = 0;
//starting y position
const int StartPositionY = 0;
//speed that the sprite will move on screen
const int MageSpeed = 160;
//move sprite 1 up/down when the arrow key is pressed
const int MoveUp = 1;
const int MoveDown = -1;
const int MoveLeft = -1;
const int MoveRight = 1;
//used to store the current state of the sprite
enum State
{
Walking
}
//set to current state of the sprite. initally set to walking
State CurrentState = State.Walking;
//stores direction of sprite
Vector2 Direction = Vector2.Zero;
//stores speed of sprite
Vector2 Speed = Vector2.Zero;
//stores previous state of keyboard
KeyboardState PreviousKeyboardState;
public void LoadContent(ContentManager theContentManager)
{
//sets position to the top left corner of the screen
Position = new Vector2(StartPositionX, StartPositionY);
base.LoadContent(theContentManager, MageAssetName);
}
//checks state of the keyboard
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
//run if the sprite is walking
if (CurrentState == State.Walking)
{
//sets direction and speed to zero
Speed = Vector2.Zero;
Direction = Vector2.Zero;
//if left key is pressed, move left
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
//speed of sprite movement
Speed.X = MageSpeed;
//moves the sprite left
Direction.X = MoveLeft;
}
//if right key is pressed, move right
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
//speed of sprite movement
Speed.X = MageSpeed;
//moves the sprite right
Direction.X = MoveRight;
}
//if up key is pressed, move up
if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
{
//speed of sprite movement
Speed.Y = MageSpeed;
//moves sprite up
Direction.Y = MoveUp;
}
//if down key is pressed, move down
else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
//speed of sprite movement
Speed.Y = MageSpeed;
//moves sprite down
Direction.Y = MoveDown;
}
}
}
public void Update(GameTime theGameTime)
{
//obtains current state of the keyboard
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
//calls in UpdateMovement and passes in current keyboard state
UpdateMovement(aCurrentKeyboardState);
//set previous state to current state
PreviousKeyboardState = aCurrentKeyboardState;
//call update method of the charMovement class
base.Update(theGameTime, Speed, Direction);
}
}
}
CharMovement
namespace RPG
{
public class charMovement
{
//The asset name for the Sprite's Texture
public string charSprite;
//The Size of the Sprite (with scale applied)
public Rectangle Size;
//The amount to increase/decrease the size of the original sprite.
private float mScale = 1.0f;
//The current position of the Sprite
public Vector2 Position = new Vector2(0,0);
//The texture object used when drawing the sprite
private Texture2D charTexture;
//Load the texture for the sprite using the Content Pipeline
public void LoadContent(ContentManager theContentManager, string theCharSprite)
{
//loads the image of the sprite
charTexture = theContentManager.Load<Texture2D>("charSprite");
charSprite = theCharSprite;
//creates a new rectangle the size of the sprite
Size = new Rectangle(0, 0, (int)(charTexture.Width * mScale), (int)(charTexture.Height * mScale));
}
//Update the Sprite and change it's position based on the set speed, direction and elapsed time.
public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
{
Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
}
//Draw the sprite to the screen
public void Draw(SpriteBatch theSpriteBatch)
{
//draw the sprite to the screen inside a rectangle
theSpriteBatch.Draw(charTexture, Position,
new Rectangle(0, 0, charTexture.Width, charTexture.Height),
Color.White, 0.0f, Vector2.Zero, mScale, SpriteEffects.None, 0);
}
}
}
MageChar
namespace RPG
{
public class MageChar : charMovement
{
//variable where sprite file name is stored
const string MageAssetName = "MageChar";
//starting x position
const int StartPositionX = 0;
//starting y position
const int StartPositionY = 0;
//speed that the sprite will move on screen
const int MageSpeed = 160;
//move sprite 1 up/down when the arrow key is pressed
const int MoveUp = 1;
const int MoveDown = -1;
const int MoveLeft = -1;
const int MoveRight = 1;
//used to store the current state of the sprite
enum State
{
Walking
}
//set to current state of the sprite. initally set to walking
State CurrentState = State.Walking;
//stores direction of sprite
Vector2 Direction = Vector2.Zero;
//stores speed of sprite
Vector2 Speed = Vector2.Zero;
//stores previous state of keyboard
KeyboardState PreviousKeyboardState;
public void LoadContent(ContentManager theContentManager)
{
//sets position to the top left corner of the screen
Position = new Vector2(StartPositionX, StartPositionY);
base.LoadContent(theContentManager, MageAssetName);
}
//checks state of the keyboard
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
//run if the sprite is walking
if (CurrentState == State.Walking)
{
//sets direction and speed to zero
Speed = Vector2.Zero;
Direction = Vector2.Zero;
//if left key is pressed, move left
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
//speed of sprite movement
Speed.X = MageSpeed;
//moves the sprite left
Direction.X = MoveLeft;
}
//if right key is pressed, move right
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
//speed of sprite movement
Speed.X = MageSpeed;
//moves the sprite right
Direction.X = MoveRight;
}
//if up key is pressed, move up
if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
{
//speed of sprite movement
Speed.Y = MageSpeed;
//moves sprite up
Direction.Y = MoveUp;
}
//if down key is pressed, move down
else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
//speed of sprite movement
Speed.Y = MageSpeed;
//moves sprite down
Direction.Y = MoveDown;
}
}
}
public void Update(GameTime theGameTime)
{
//obtains current state of the keyboard
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
//calls in UpdateMovement and passes in current keyboard state
UpdateMovement(aCurrentKeyboardState);
//set previous state to current state
PreviousKeyboardState = aCurrentKeyboardState;
//call update method of the charMovement class
base.Update(theGameTime, Speed, Direction);
}
}
}
答案 0 :(得分:0)
首先,您需要创建精灵纹理:将角色的所有图像放在一个纹理中(如下所示:https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTl7XCFrVrnkzcNtpokcpJf3SKxcALw-IpbuSgVP4xNRBgtsnN4)。让我们说现在你的角色有两种不同的状态 - 朝左和朝右,两种纹理都是64x64。所以我们有这样的事情:
-----------------
| | |
| <-- | --> |
| | |
-----------------
纹理总尺寸:128x64
您需要向状态枚举添加更多值。假设你的状态枚举将有两个值 Left 和 Right 而不是 Walking 。 现在,您可以使用第三个参数在绘图方法中切换帧:
if (CurrentState == State.Left)
theSpriteBatch.Draw(charTexture, Position,
new Rectangle(0, 0, 64, 64), // first frame
Color.White, 0.0f, Vector2.Zero, mScale, SpriteEffects.None, 0);
else if (CurrentState == State.Right)
theSpriteBatch.Draw(charTexture, Position,
new Rectangle(64, 0, 64, 64), // second frame
Color.White, 0.0f, Vector2.Zero, mScale, SpriteEffects.None, 0);
您也可以使用方向或任何您想要的而不是状态。将来你会想要为整个步行动画和其他单个角色的动作设置不同的纹理。
有关精灵动画的更多信息:http://thundernoodle.net/notblog/2011/10/17/how-to-animate-a-sprite-using-a-sprite-sheet-in-xna/
您也可以根据建议为不同状态切换纹理。为此,只需加载两个不同的纹理,例如charTexture_Left和charTexture_Right,并根据CurrentState在draw方法中使用它们。
要使CurrentState更改以及用户箭头输入,请将CurrentState添加到UpdateMovement方法:
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
//speed of sprite movement
Speed.X = MageSpeed;
//moves the sprite left
Direction.X = MoveLeft;
CurrentState = State.Left; // set current state to left
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
//speed of sprite movement
Speed.X = MageSpeed;
//moves the sprite right
Direction.X = MoveRight;
CurrentState = State.Right; // set current state to right
}