所以我几个月前才开始学习C#代码,我想在一个完整的步行周期中获得一个精灵。
我在PNG图像上有我的步行周期。这显然是一系列单独的图像,显示了步行周期的不同阶段。现在我已将其纳入我的项目/解决方案中,我将在Visual Studio 2010中将其作为Windows Game 4.0进行。
我相当远,所以我开始尝试做srcRect和destRect设置。但是我设法进入屏幕的是图像从左到右快速地向右移动然后无限向右移动。我通过使用srcRect.X + = srcRect.width行来实现这一点。
但我想做的是让这一举动慢得多。我希望当向右推动拇指操纵杆时,destRect会在屏幕上向右移动,当发生这种情况时,我希望它能够在我的步行循环中轻弹一下。
我变得非常混乱,我是初学者所以任何帮助都会非常感激!以下是我目前的代码!
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;
namespace fra_walk
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GamePadState pad1, oldpad1;
Rectangle srcRect;
struct Rect
{
public Rectangle destRect;
public Rectangle frame;
public Point velocity;
}
Rect destRect;
struct Wright
{
public Texture2D txr;
public Rectangle frame;
}
Wright walkright;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
destRect.velocity = new Point(0, 0);
destRect.frame = new Rectangle(400, 400, 83, 170);
srcRect = new Rectangle(0, 0, 64, 170);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
walkright.txr = Content.Load<Texture2D>("walk right");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
GamePadState pad1 = GamePad.GetState(PlayerIndex.One);
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
srcRect.X += srcRect.Width;
// if ((pad1.ThumbSticks.Left.X == 1.0f)
// && (oldpad1.ThumbSticks.Left.X == 1.0f))
// {
// srcRect.X += destRect.frame.Width;
// }
oldpad1 = pad1;
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(walkright.txr, destRect.frame, srcRect, Color.White);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
答案 0 :(得分:0)
一个简单的方法来做你想要的可能是实现一个计数器。
将public double animCounter = 0添加到代码顶部。
然后围绕srcRect.X + = srcRect.Width; line添加一些效果:
if (animCounter > 0)
{
animCounter -= gameTime.ElapsedGameTime.TotalSeconds;
}
else
{
srcRect.X += srcRect.Width;
animCounter = 0.5f;
}
同样,这是做你想做的事情的简单方法,但不一定是最好的。例如,+ = srcRect.Width将快速超过精灵表的宽度,因此您需要检查并更正它。正如其中提到的那样,你应该在Google上搜索一些教程。
这个开头并不错: http://msdn.microsoft.com/en-us/library/bb203866.aspx