【问题标题】:My LoadContent Function does not load my sprites(Building a Pacman Game)我的 LoadContent 函数没有加载我的精灵(构建吃豆人游戏)
【发布时间】:2016-03-31 18:33:28
【问题描述】:

好吧,我在 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);
    }
}
}

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    查看你的 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      相关资源
      最近更新 更多