Xna 4.0 WP,SpriteBatch-动画

Xna 4.0 WP,SpriteBatch-动画,xna,windows-phone,Xna,Windows Phone,我有以下代码: public class Game1 : Microsoft.Xna.Framework.Game { SpriteBatch spriteBatch; Vector2 m = new Vector2(0, 0); Texture2D polska; int i = 1; //Game class boilerplate code snipped for brevity. protecte

我有以下代码:

public class Game1 : Microsoft.Xna.Framework.Game
{          
    SpriteBatch spriteBatch;
    Vector2 m = new Vector2(0, 0);  
    Texture2D polska;        
    int i = 1;

    //Game class boilerplate code snipped for brevity.

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
    }

    protected override void Draw(GameTime gameTime)
    {
        polska = Content.Load<Texture2D>(i.ToString());
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        spriteBatch.Draw(polska, m, null, Color.White);
        spriteBatch.End();

        if (i == 169)
            i = 1;
        else
            i++;

        base.Draw(gameTime);              
    }          
}
公共类游戏1:Microsoft.Xna.Framework.Game
{          
SpriteBatch SpriteBatch;
向量2 m=新向量2(0,0);
纹理2d波尔斯卡;
int i=1;
//游戏类样板代码为简洁而剪裁。
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
polska=Content.Load(i.ToString());
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
spriteBatch.Draw(波尔斯卡,m,空,彩色,白色);
spriteBatch.End();
如果(i==169)
i=1;
其他的
i++;
基础。抽签(游戏时间);
}          
}
问题是,当我在手机上运行此功能时,它只显示1个图像,应该显示169个不同的图像(它们的名称为
[number].png


怎么了?

你真的不应该在
Draw
方法中加载内容,因为它每秒更新>30次,加载纹理需要时间。相反,在
LoadContent
方法中加载所有图像一次(使用数组或列表存储它们),然后在
draw
方法中绘制正确的图像:

Texture2D[] polska = new Texture2D[169];//array of textures

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    for (int j = 0; j < 169; j++)
        polska[j] = Content.Load<Texture2D>(j.ToString());//load all the images here


    // TODO: use this.Content to load your game content here
}


protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    spriteBatch.Begin();
    spriteBatch.Draw(polska[i],m,null,Color.White);//draw the current (i) image
    spriteBatch.End();
    if (i == 169)
    {
        i = 1;
    }
    else
    {
        i++;
    }
    base.Draw(gameTime);

}
Texture2D[]polska=新的Texture2D[169]//纹理数组
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
对于(int j=0;j<169;j++)
polska[j]=Content.Load(j.ToString());//在此处加载所有图像
//TODO:使用此.Content在此处加载游戏内容
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
spriteBatch.Draw(polska[i],m,null,Color.White);//绘制当前(i)图像
spriteBatch.End();
如果(i==169)
{
i=1;
}
其他的
{
i++;
}
基础。抽签(游戏时间);
}
嗯,如果有什么不清楚的话


注:正如@StevenHansen指出的,你应该调整纹理的大小,这样你就不会出现内存不足的问题。

我错了,它只显示90帧。试着在程序开始时设置
i=91
,看看会发生什么。我做了91-在90时停止,如果我做了89,则循环/尝试使用较小的图像。使用gimp缩小尺寸。是的。在
LoadContent
过程中加载所有图像,不要调用
Content。在完全处理完图像之前,请卸载它们。是的,这就是方法。确保你的内存不会溢出。是的,也许我会说一些关于纹理大小的事情(我看到了你的评论):)david,你能告诉我我是否声明了一个数组吗?这个数组在内存中-所以(大约169*2mb)是一种只占用2mb内存的方法吗?对不起,我不太确定你在问什么你想知道这个阵列有多大吗?或者如何缩小它?或者什么?我问,如何缩小一个用过的记忆并且不丢失任何图像分辨率。