【发布时间】:2014-09-03 21:55:39
【问题描述】:
这是一个 c#/XNA 4.0 类的作业,不,我不是 HWgrade-zombie。我会让你知道我问这个问题的情况,我做了多少“研究”,我希望学习而不是获得分数,但它会被认为是无用的并被删除。
无论如何,我遇到的问题是我试图加载到 Game1.cs 中的“textureImage”中的内容(精灵表),然后将其传递给“Sprite.cs”进行绘制,通过“userControlledSprite.cs”,当我编译我的程序时没有被绘制。作为 c# 和 XNA 的新手,我理解这个问题(内容加载,但没有正确传递),但我不知道如何解决它。
以下是我的程序包含的全部三个类:
Game1.cs -- 调用 userControlledSprite。
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 Proj06
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
static Texture2D textureImage;
static Point frameSize = new Point(52,50);
static Point currentFrame = new Point(0, 0);
static Point sheetSize = new Point(4, 1);
static Vector2 position = Vector2.Zero;
static int collisionOffset = 1;
static Vector2 speed;
userControlledSprite UserControlledSprite1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
textureImage = Content.Load<Texture2D>(@"images/Picture");
UserControlledSprite1 = new userControlledSprite(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed);
}
protected override void UnloadContent()
{
/// Ignore this void
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
UserControlledSprite1.Draw(gameTime, spriteBatch);
base.Draw(gameTime);
}
}
}
Sprite.cs -- 绘制精灵并为其设置动画,以及检查碰撞。
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 Proj06
{
abstract class Sprite
{
Texture2D textureImage;
protected Point frameSize;
Point currentFrame;
Point sheetSize;
int collisionOffset;
int timeSinceLastFrame = 0;
int millisecondsPerFrame;
const int defaultMillisecondsPerFrame = 16;
protected Vector2 speed;
protected Vector2 position;
public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)
: this(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, defaultMillisecondsPerFrame)
{
}
public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
int millisecondsPerFrame)
{
this.textureImage = textureImage;
this.position = position;
this.frameSize = frameSize;
this.collisionOffset = collisionOffset;
this.currentFrame = currentFrame;
this.sheetSize = sheetSize;
this.speed = speed;
this.millisecondsPerFrame = millisecondsPerFrame;
}
public virtual void Update(GameTime gameTime, Rectangle clientBounds)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame = 0;
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
++currentFrame.Y;
if (currentFrame.Y >= sheetSize.Y)
{
currentFrame.Y = 0;
}
}
}
}
public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(textureImage,
position,
new Rectangle(currentFrame.X * frameSize.X,
currentFrame.Y * frameSize.Y,
frameSize.X, frameSize.Y),
Color.White, 0, Vector2.Zero,
1f, SpriteEffects.None, 0);
spriteBatch.End();
}
public abstract Vector2 direction
{
get;
}
public Rectangle collisionRect
{
get
{
return new Rectangle(
(int)position.X + collisionOffset,
(int)position.Y + collisionOffset,
frameSize.X - (collisionOffset * 2),
frameSize.Y - (collisionOffset * 2));
}
}
}
}
and userControlledSprite.cs -- 处理精灵的移动和位置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Proj06
{
class userControlledSprite : Sprite
{
public userControlledSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed)
{
}
public userControlledSprite(Texture2D textureImage, Vector2 position,
Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
Vector2 speed, int millisecondsPerFrame)
: base(textureImage, position, frameSize, collisionOffset, currentFrame,
sheetSize, speed, millisecondsPerFrame)
{
}
public override Vector2 direction
{
get
{
Vector2 inputDirection = Vector2.Zero;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
inputDirection.X -= 1;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
inputDirection.X += 1;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
inputDirection.Y -= 1;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
inputDirection.Y += 1;
return inputDirection * speed;
}
}
public override void Update(GameTime gameTime, Rectangle clientBounds)
{
position += direction;
if (position.X < 0)
position.X = 0;
if (position.Y < 0)
position.Y = 0;
if (position.X > clientBounds.Width - frameSize.X)
position.X = clientBounds.Width - frameSize.X;
if (position.Y > clientBounds.Height - frameSize.Y)
position.X = clientBounds.Height - frameSize.Y;
base.Update(gameTime, clientBounds);
}
}
}
我将继续在线搜索任何有用的资源,如果我发现任何内容,我会发布编辑。感谢您的宝贵时间。
【问题讨论】:
-
您没有从 Game1 类调用 draw 方法,并且 Sprite 也没有扩展 DrawableGameComponent(然后添加到 Game.Components)来注册您覆盖的绘制方法。你至少需要做其中一件事。看起来您还缺少 spriteBatch.Begin 和 spriteBatch.End 调用。在您的 Draw/Update 方法中设置断点以查看它们是否被调用。
标签: c# xna argument-passing