[英]Windows Phone XNA animation
我有VS2012,并尝试为Windows Phone 7/8制作一个简单的xna应用程序。 我有这样的事情:
public partial class GamePage : PhoneApplicationPage
{
ContentManager contentManager;
GameTimer timer;
SpriteBatch spriteBatch;
public Texture2D firstSprite { get; set; }
public Vector2 transition = new Vector2(0, 0);
public GamePage()
{
InitializeComponent();
contentManager = (Application.Current as App).Content;
timer = new GameTimer();
timer.UpdateInterval = TimeSpan.FromTicks(333333);
timer.Update += OnUpdate;
timer.Draw += OnDraw;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
firstSprite = this.contentManager.Load<Texture2D>("ikona");
timer.Start();
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
timer.Stop();
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
base.OnNavigatedFrom(e);
}
float d = 0;
private void OnUpdate(object sender, GameTimerEventArgs e)
{
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
if (tl.State == TouchLocationState.Pressed && tl.Position.X < 240)
{
d = -10;
}
if (tl.State == TouchLocationState.Pressed && tl.Position.X > 240)
{
d = 10;
}
if (tl.State == TouchLocationState.Released)
d = 0;
}
transition += new Vector2(d, 0);
}
private void OnDraw(object sender, GameTimerEventArgs e)
{
SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(firstSprite, transition, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0);
spriteBatch.End();
}
}
动画精灵是个好方法吗? 在每个设备上会以相同的方式制作动画吗? 当我在lumia920上进行测试时,它并不是很平滑,所以我认为我做得不好。
第二件事。 我想在按下屏幕的左半部分时向左移动图片,而在按下右侧的时候向右移动。 它部分起作用,但是当我按向左(向左移动),然后同时按向右(向右移动),然后向右释放时,图片停止。 我以为它会再次向左移动。 我该如何实现?
/ edit /那么触摸处理呢?
我不确定它的动画效果,因为我不熟悉TouchCollection。
但是对于大多数解决方案,使用XNA进行动画处理都是这样的:
您拥有的纹理就是一个精灵表,我认为其中包含动画精灵的所有帧。
您使用矩形,它将在屏幕上保留位置和大小。
您还可以使用一种称为“源矩形”的东西,它是代表精灵内一个区域的矩形,它将在您的位置矩形中延伸。 这是一张图片:
矩形A是您的位置+大小矩形,矩形B是您的源矩形。
要创建动画,请说以下(伪代码):
int timer = 0;
Rectangle position = new Rectangle(100, 100, 80, 80); // position 100x, 100y, and size is 80 width and height.
Rectangle source = new Rectangle(0, 0, 80, 80); // Position on the spritesheet is 0, 0 which should be first frame, and the frames are all 80*80 pixels in size.
private void OnUpdate(object sender, GameTimerEventArgs e)
{
timer += e.ElapsedTime.TotalMilliseconds;
if(timer > 30) // If it has been 0.03 seconds = 33 frames per second
{
timer = 0; // reset timer
source.X += 80; // Move x value to next frame
if(source.X > widthOfYourSpriteSheet) source.X = 0; // Reset the animation to the beginning when we are at the end
}
}
绘制函数的外观几乎相同,除了您要放入其中的sourcerectangle参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.