Permalänk
Medlem

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); } } }

Dold text
Permalänk

Kan du skicka med koden till de andra klasserna också?

Visa signatur

Asrock Z68 Extreme 7 gen 3 | EVGA GTX580 SLI | Intel i7 2600k | Corsair Vengeance 16GB
Corsair H80 | Corsair Force 3 120gb | HX1050 |NZXT Phantom | Samsung 22" SA300

Sony VAIO pro 13
i7 8GB 256GB

Permalänk
Medlem

Tja ett null referense problem. Du kan börja med att sätta en breakpoint där och sedan kolla så att värdena du försöker skicka in inte är null.

Visa signatur

Speldator: i7-8700k, 32GB DDR4, RTX2080
Server 1: SB 2500k, MZI -P67GD55, 32GB DDR3, Corsair MX 240GB SSD
Surface Pro 2017, Konsoler: Typ alla, Oculus Rift

Permalänk
Medlem
Skrivet av tgwallenborg:

Kan du skicka med koden till de andra klasserna också?

Sprite

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace AnimatedSprites { 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 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)); } } 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.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); } } }

Dold text

UserControlledSprite

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 AnimatedSprites { class UserControlledSprite : Sprite { MouseState prevMouseState; 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; GamePadState gamepadState = GamePad.GetState(PlayerIndex.One); if(gamepadState.ThumbSticks.Left.X != 0) inputDirection.X += gamepadState.ThumbSticks.Left.X; if(gamepadState.ThumbSticks.Left.Y != 0) inputDirection.Y -= gamepadState.ThumbSticks.Left.Y; return inputDirection * speed; } } public override void Update(GameTime gameTime, Rectangle clientBounds) { // Move the sprite based on direction position += direction; // If player moved the mouse, move the sprite MouseState currMouseState = Mouse.GetState(); if (currMouseState.X != prevMouseState.X || currMouseState.Y != prevMouseState.Y) { position = new Vector2(currMouseState.X, currMouseState.Y); } prevMouseState = currMouseState; // If sprite is off the screen, move it back within the game window 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.Y = clientBounds.Height - frameSize.Y; base.Update(gameTime, clientBounds); } } }

Dold text

AutomatedSprite

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace AnimatedSprites { class AutomatedSprite : Sprite { public AutomatedSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed) : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed) { } public AutomatedSprite(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 { return speed; } } public override void Update(GameTime gameTime, Rectangle clientBounds) { position += direction; base.Update(gameTime, clientBounds); } } }

Dold text