XNA SpriteSheet无法正常工作

时间:2013-03-24 02:27:57

标签: windows-phone-7 xna sprite-sheet

我添加了逻辑来显示Sprite动画,但它根本没有在屏幕上显示。我做错了什么?

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace MyXNAGame.Game_Classes
{
  public class Player : Sprite
  {
    private int _frameCount = 6;
    private int _frameIndex;
    private Rectangle[] _frames;
    public float _jumpVelocity = 12f;
    public PlayerState _playerState;

    public Rectangle BoundingBox
    {
        get { return new Rectangle {X = (int) Position.X, Y = (int) Position.Y, Height = Texture.Height, Width = Texture.Width}; }
    }

    public void Initialize()
    {
        _frames = new Rectangle[_frameCount];
        int width = Texture.Width / _frameCount;
        for (int i = 0; i < _frameCount; i++)
        {
            _frames[i] = new Rectangle(i*width, 0, width, Texture.Height);
        }
    }

    public void Load(ContentManager contentManager, string assetName)
    {
        Texture = contentManager.Load<Texture2D>(assetName);
    }

    public void Update(GameTime gameTime)
    {
        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gestureSample = TouchPanel.ReadGesture();
            if (gestureSample.GestureType == GestureType.Tap)
            {
                if (_playerState == PlayerState.Running)
                {
                    _playerState = PlayerState.NormalJump;
                }
            }
            if (gestureSample.GestureType == GestureType.Hold)
            {
                if (_playerState == PlayerState.Running)
                {
                    _playerState = PlayerState.LongJump;
                }
            }
        }

        // NormalJump Logic
        switch (_playerState)
        {
            case PlayerState.NormalJump:
                Position.Y -= _jumpVelocity;
                _jumpVelocity -= 0.5f;
                if (_jumpVelocity == 0)
                {
                    _playerState = PlayerState.Falling;
                }
                break;
            case PlayerState.LongJump:
                Position.Y -= _jumpVelocity;
                _jumpVelocity -= 0.5f;
                if (_jumpVelocity == 0)
                {
                    _playerState = PlayerState.Falling;
                }
                break;
            case PlayerState.Falling:
                Position.Y += _jumpVelocity;
                _jumpVelocity += 0.5f;
                break;
            case PlayerState.Running:
                _frameIndex++;
                if (_frameIndex > 5)
                {
                    _frameIndex = 0;
                }

                break;
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, Position, _frames[_frameIndex], Color.White, 0, new Vector2(0, 0), new Vector2(0, 0), SpriteEffects.None, 0);
    }
}

}`

有人能看到明显的错误吗?我正在使用WP7

1 个答案:

答案 0 :(得分:1)

我将Draw()方法中的'Scale'参数从新Vector(0,0)更改为新Vector(1,1),显然,Scale为0时根本不会显示任何内容。