我的LoadContent函数没有加载我的精灵(建立一个吃豆人游戏)

时间:2016-03-31 18:33:29

标签: c# xna

好的,所以我在youtube上关于使用XNA创建Pacman游戏的教程是链接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);
    }
}
}

1 个答案:

答案 0 :(得分:2)

看看您的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);
}
相关问题