Windows Phone 7上的呼叫失败,但适用于Windows和Xbox 360

时间:2012-04-30 07:26:29

标签: c# windows-phone-7 xna windows-phone-7.1 xna-4.0

所以,我在这里遇到一个奇怪的问题。我在Windows,Xbox 360和Windows Phone 7上使用相同的XNA代码。我可以使其大部分工作,但当Level尝试在Windows Phone 7版本上调用TileMap时,它永远不会到达TileMap。但是,在Windows和Xbox 360上,它工作得很好。我还有什么需要做的吗?

更多细节。在Windows Phone版本上,我从调试器获取某些值的“无法评估表达式”消息。这不会发生在Windows版本上。同样,如果我让它继续足够长时间,我会得到一个OutOfMemoryException,即使我调用的图像大小约为KB,而.map文件只会创建一个3x3矩阵。

GameplayScreen.cs

    private Level level;
    ...
    public override void LoadContent()
    {
        if (Content == null)
            Content = new ContentManager(ScreenManager.Game.Services, "Content");
        //Create Sprite Batch for drawing textures
        //spriteBatch = new SpriteBatch(GraphicsDevice);

        //Load fonts
        hudFont = Content.Load<SpriteFont>("Fonts/hud");

        lifeIcon = Content.Load<Texture2D>("Sprites/Player/lifeIco");

        //Load overlays
        //winOverlay = Content.Load<Texture2D>("Overlays/win");
        //loseOverlay = Content.Load<Texture2D>("Overlays/lose");

        try
        {
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(Content.Load<Song>("Sounds/music"));
        }
        catch { }

        LoadNextLevel();

        //Camera.InitializeScreen(graphics);

        base.LoadContent();
    } //end LoadContent

    /// <summary>
    /// Loads the next level.
    /// </summary>
    public void LoadNextLevel()
    {
        //Set the next level
        levelIndex = (levelIndex + 1) % numberOfLevels;

        if (level != null)
            level.Dispose();

        level = new Level(ScreenManager.Game.Services, levelIndex);

    } //end LoadNextLevel()

Level.cs

    private TileMap levelMap;
    public Level(IServiceProvider service, int levelIndex)
    {
        //Create content manager for current level.
        content = new ContentManager(service, "Content");

        //Load level map
        //This is where the code hangs
        levelMap = new TileMap(this, levelIndex, content.Load<Texture2D>(@"Tiles/tiles"));


        //Load sounds
        //exampleSound = content.Load<SoundEffect>("Sounds/example");

        //Initialize Camera
        Camera.InitializeLevel(levelMap);
        player = new Player(this, start);
    } //end Level

TileMap.cs

    public TileMap(Level level, int levelIndex, Texture2D tileSheet)
    {
        // Never reaches here on Windows Phone 7 Version
        // Comes right here on Windows and Xbox 360 Version
        this.tileSheet = tileSheet;

        string levelPath = string.Format("Content/Levels/{0}.map", levelIndex);

我已经建议并试图做的一些事情:

我试图在调用Tile Map之外调用“Tiles / tiles”,我试图用DXT压缩来压缩tile。 “Tiles / tiles”加载完全正常,但调用仍然失败,我仍然得到OutOfMemoryException。我的内容的总文件大小是1.23 MB。

1 个答案:

答案 0 :(得分:0)

在Level.cs中,不是将内容作为服务传递,而是预先传递所有加载的东西:{Texture2D,Sound}。 在XNA中,您通常会在主游戏类或组件中加载所有内容。我是95%这是&#34;无法评估表达式&#34;。

的原因

另外,请查看.NET Compact Framework 2.0并了解垃圾收集器的工作原理:

http://blogs.msdn.com/b/stevenpr/archive/2004/07/26/197254.aspx