我的LoadContent函数不加载我的精灵(构建吃豆人游戏)



好吧,我在youtube上学习关于使用XNA创建吃豆人游戏的教程,这里是链接https://www.youtube.com/watch?v=TN3NYT_glmg.我看了3个教程,遇到了一个问题。我完全按照那个家伙的做法,但有一个问题,我的精灵不会在屏幕上画出来,我回溯了教程,试图找出我遗漏了什么,但我找不到我遗漏的任何东西。我很感谢你的帮助,我需要完成这个游戏,以便了解游戏编程的实际工作原理。这是代码

对象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace WindowsGame1
{
class Obj
{

    public Texture2D Texture = null; //texture of object;
    public string TextureName = string.Empty;//name of texture
    public Vector2 center = Vector2.Zero;//Center of texture
    public Vector2 position = Vector2.Zero;//Postion of object
    public float Rotation = 0.0f; //Rotation of Object
    public float scale = 1.0f; //Scale of the object
    public float speed = 0.0f; //speed of the object
    public bool isAlive = true;
    public Obj(Vector2 pos)
    {
        position = pos;
    }
    public Obj()
    {
    }

    public virtual void LoadContent(ContentManager Content)//Loads Content
    {
        Texture = Content.Load<Texture2D>("Sprites/"+this.TextureName);
        center = new Vector2(Texture.Width / 2, Texture.Height / 2);
    }
    public virtual void Update(GameTime gameTime)//Updates the gametime
    {
    }
    public virtual void Draw(SpriteBatch spriteBatch)//Draws object/sprites
    {
        if (isAlive)
            return;

        spriteBatch.Draw(Texture, position, null, Color.White, MathHelper.ToRadians(Rotation), center, scale,SpriteEffects.None, 0);
    }
}
}

物品类别

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.Graphics;
 namespace WindowsGame1
{
class Items
{
    public static List<Obj> Objlist = new List<Obj>();//Create list of    objects
    public static Pacman Pacman;
    public static void Initialize()
    {
        Objlist.Add(Pacman=new Pacman(new Vector2(250,250), "Pacman1"));
    }
}
}

吃豆人类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
 namespace WindowsGame1
 {
class Pacman:Obj
{
    public Pacman(Vector2 pos, string textureName)
    {
        TextureName = textureName;
        position = pos;
        isAlive = true;

    }
}
}

最后游戏类

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        Items.Initialize();
        base.Initialize();
    }
    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        foreach (Obj o in Items.Objlist)
        {
            o.LoadContent(Content);
        }
        }
    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }
    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        foreach (Obj o in Items.Objlist)
        {
            o.Update(gameTime);//updates all ojects
        }

        base.Update(gameTime);
    }
    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)//Used to draw the game
    {
        GraphicsDevice.Clear(Color.SkyBlue);//This line here is used to change the color of the screen
        spriteBatch.Begin();
        foreach (Obj o in Items.Objlist)
        {
            o.Draw(spriteBatch);
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
}

查看您的Draw代码:

public virtual void Draw(SpriteBatch spriteBatch)//Draws object/sprites
{
    if (isAlive)
        return;

    spriteBatch.Draw(Texture, position, null, Color.White, MathHelper.ToRadians(Rotation), center, scale,SpriteEffects.None, 0);
}

如果物体是活的,你就不画它…

应该是:

public virtual void Draw(SpriteBatch spriteBatch)//Draws object/sprites
{
    if (!isAlive)
        return;

    spriteBatch.Draw(Texture, position, null, Color.White, MathHelper.ToRadians(Rotation), center, scale,SpriteEffects.None, 0);
}

最新更新