簡體   English   中英

Windows Phone XNA動畫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM