【问题标题】:Windows Phone XNA animationWindows Phone XNA 动画
【发布时间】:2014-03-18 16:44:19
【问题描述】:

我有 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上测试的时候不是很流畅,所以我觉得我做的很糟糕。

第二件事。当我按下屏幕的左半边时,我想将图片向左移动,当我按下右侧时,我想向右移动。它部分有效,但是当我按下左(它向左移动),然后同时按下右(它向右移动),然后向右释放时,画面停止。我以为它会再次向左移动。我怎样才能做到这一点?

/编辑/ 那么触摸处理呢?

【问题讨论】:

    标签: c# windows-phone-7 windows-phone-8 xna


    【解决方案1】:

    我不确定它的动画效果,因为我不熟悉 TouchCollection。
    但是对于大多数解决方案来说,使用 XNA 制作动画效果如下:
    你有你的纹理,我假设它是一个精灵表,其中包含动画精灵的所有帧。
    您使用 Rectangle 来保存屏幕上的位置和大小。
    你还使用了一个叫做源矩形的东西,它是一个矩形,代表你的精灵内部的一个区域,它将在你的位置矩形中伸展。这是一张图片:
    矩形 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 参数。

    【讨论】:

    • 那么,我应该为每个 spritesheet 保存不同的“计时器”变量吗?
    • 这取决于您和 spritesheet 的设计方式。就我个人而言,我会跟踪 spritesheet 中每个动画的帧之间的时间。
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多