C# XNA Problem
Jag är nybörjare i XNA (och programmering) och jag har stött på ett problem. Jag har ett simpelt spel med en basklass (Sprite) och två subklasser (UserControlledSprite och AutomatedSprite). Jag har en Game Component som är kopplad till Game1 klassen och som ska hantera allt med sprites (anropa deras Update/Draw etc).
All kod är skriven efter en tutorial som jag följer, men jag måste skrivit fel någonstans eftersom att när snubben som gjort tutorial:en lyckas starta spelet så får jag istället error i min Game Component som jag inte vet hur jag ska lösa. Så här ser det ut:
Hela Game Component klassen:
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 AnimatedSprites
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class SpriteManager : Microsoft.Xna.Framework.DrawableGameComponent
{
SpriteBatch spriteBatch;
UserControlledSprite player;
List<Sprite> spriteList = new List<Sprite>();
public SpriteManager(Game game)
: base(game)
{
// TODO: Construct any child components here
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = new UserControlledSprite(Game.Content.Load<Texture2D>(@Images/threerings), Vector2.Zero, new Point(75, 75), 10, new Point(0, 0), new Point(6, 8), new Vector2(6, 6));
spriteList.Add(new AutomatedSprite(Game.Content.Load<Texture2D>(@Images/skullball), new Vector2(150,150), new Point(75,75), 10, new Point(0,0), new Point(6,8), Vector2.Zero));
spriteList.Add(new AutomatedSprite(Game.Content.Load<Texture2D>(@Images/skullball), new Vector2(300, 150), new Point(75, 75), 10, new Point(0, 0), new Point(6, 8), Vector2.Zero));
spriteList.Add(new AutomatedSprite(Game.Content.Load<Texture2D>(@Images/skullball), new Vector2(150, 300), new Point(75, 75), 10, new Point(0, 0), new Point(6, 8), Vector2.Zero));
spriteList.Add(new AutomatedSprite(Game.Content.Load<Texture2D>(@Images/skullball), new Vector2(600, 400), new Point(75, 75), 10, new Point(0, 0), new Point(6, 8), Vector2.Zero));
base.LoadContent();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
player.Update(gameTime, Game.Window.ClientBounds);
foreach (Sprite s in spriteList)
{
s.Update(gameTime, Game.Window.ClientBounds);
if (s.collisionRect.Intersects(player.collisionRect)) Game.Exit();
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
player.Draw(gameTime, spriteBatch);
spriteBatch.End();
foreach (Sprite s in spriteList) s.Draw(gameTime, spriteBatch);
base.Draw(gameTime);
}
}
}