XNA SpriteSheet 不工作

Posted

技术标签:

【中文标题】XNA SpriteSheet 不工作【英文标题】:XNA SpriteSheet not working 【发布时间】:2013-03-13 17:39:43 【问题描述】:

我添加了显示 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】:

我将 Draw() 方法中的“Scale”参数从 new Vector(0,0) 更改为 new Vector(1,1),显然,Scale 为 0 将不会显示任何内容。

【讨论】:

以上是关于XNA SpriteSheet 不工作的主要内容,如果未能解决你的问题,请参考以下文章

XNA Sprite 帧不一致

在 XNA 中读取 spritesheet XML 文件

JAVA:尝试读取 SpriteSheet 时未成形的 Sprite

尝试从 spritesheet 为 sprite Phaser 3 设置动画 - 出现错误无法读取未定义的属性“框架”

Sprite Sheet - 在旧 sprite 之上绘制新 sprite

XNA 减速动画