大家好我在C#中制作一个关于忍者的游戏,这一行给了我关于引用没有设置为对象实例的运行时错误消息 这是给出错误的行:
string status = "Score: " + knife.KnivesLeft.ToString();
以下是完整的游戏代码:
using SnapsLibrary;
using System;
using System.Collections.Generic;
public class Knifer
{
interface IGameSprite
{
void Reset();
void Update();
}
public abstract class MovingSprite : IGameSprite
{
public ImageSprite spriteValue;
public double xSpeedValue, ySpeedValue;
public MovingSprite(ImageSprite sprite, double xSpeed, double ySpeed)
{
spriteValue = sprite;
xSpeedValue = xSpeed;
ySpeedValue = ySpeed;
}
public virtual void Update()
{
spriteValue.X = spriteValue.X + xSpeedValue;
spriteValue.Y = spriteValue.Y + ySpeedValue;
}
public virtual void Reset()
{
}
}
public class KnifeSprite : MovingSprite
{
public bool IsFlying = false;
public NinjaSprite ninjaValue;
public KnifeSprite knifeValue;
public KniferGame gameValue;
public int KnivesLeft;
public KnifeSprite(KniferGame game, ImageSprite sprite,NinjaSprite Petya, double xSpeed, double ySpeed) : base(sprite: sprite, xSpeed: xSpeed, ySpeed: ySpeed)
{
gameValue = game;
ninjaValue = Petya;
spriteValue = sprite;
xSpeedValue = xSpeed;
ySpeedValue = ySpeed;
spriteValue.CenterX = SnapsEngine.GameViewportWidth / 2;
spriteValue.Bottom = SnapsEngine.GameViewportHeight;
}
public override void Update()
{
if (SnapsEngine.GetFireGamepad())
IsFlying = true;
if (!IsFlying)
return;
base.Update();
if(spriteValue.IntersectsWith(ninjaValue.spriteValue))
{
ninjaValue.Kill();
}
if (spriteValue.Bottom < 0)
{
IsFlying = false;
KnivesLeft = KnivesLeft - 1;
if (KnivesLeft == 0)
{
gameValue.EndCurrentGame();
}
spriteValue.CenterX = SnapsEngine.GameViewportWidth / 2;
spriteValue.Bottom = SnapsEngine.GameViewportHeight;
}
}
public override void Reset()
{
spriteValue.CenterX = SnapsEngine.GameViewportWidth / 2;
spriteValue.Bottom = SnapsEngine.GameViewportHeight;
KnivesLeft = 3;
}
public void KnifeCountdown()
{
KnivesLeft = KnivesLeft - 1;
if (KnivesLeft==0)
{
gameValue.EndCurrentGame();
}
}
}
public class NinjaSprite:IGameSprite
{
public ImageSprite spriteValue;
public int NinjasRemaining;
public int countToFly;
public int NinjaSpeed;
public double originalX, originalY;
public int countdown;
public int countToFlyOriginal;
public NinjaSprite(ImageSprite sprite,int countdown,int speed,int remaining)
{
NinjasRemaining = remaining;
NinjaSpeed = speed;
countToFly = countdown;
spriteValue = sprite;
spriteValue.Right = 40;
spriteValue.Bottom = SnapsEngine.GameViewportHeight/3;
originalX = spriteValue.Right;
originalY = sprite.Bottom;
countToFlyOriginal = countToFly;
}
public void Reset()
{
spriteValue.Right = originalX;
spriteValue.Bottom = originalY;
countToFly = countToFlyOriginal;
spriteValue.Show();
}
public void Kill()
{
spriteValue.Hide();
NinjasRemaining = NinjasRemaining - 1;
if (NinjasRemaining == 0)
SnapsEngine.PlayGameSoundEffect("ding");
Reset();
Update();
}
public void Update()
{
countToFly = countToFly - 1;
if (countToFly<=0)
{
spriteValue.X = spriteValue.X + NinjaSpeed;
}
if (spriteValue.X >SnapsEngine.GameViewportWidth)
Reset();
}
}
public class KniferGame
{
List<IGameSprite> sprites = new List<IGameSprite>();
GameStates state;
ImageSprite titleScreen;
KnifeSprite knife;
ImageSprite IntermissionScreen;
ImageSprite gameOverScreen;
TextBlockSprite messageBack,message;
int gameScore;
enum GameStates
{
TitleScreen,
GameActive,
Intermission,
GameOver
}
public void ShowTitleScreen()
{
//gameOverScreen.Hide();
titleScreen.Show();
state = GameStates.TitleScreen;
}
void ResetGame()
{
gameScore = 0;
foreach (IGameSprite sprite in sprites)
sprite.Reset();
}
void SetupGame()
{
SnapsEngine.StartGameEngine(fullScreen: false, framesPerSecond: 60);
ImageSprite backgroundImage = new ImageSprite(imageURL: "ms-appx:///Images/wood tile background1000x1000.png");
SnapsEngine.AddSpriteToGame(backgroundImage);
backgroundImage.ScaleSpriteWidth(SnapsEngine.GameViewportWidth);
ImageSprite NinjaImage = new ImageSprite(imageURL: "ms-appx:///Images/ninja copy.png");
NinjaImage.ScaleSpriteWidth(SnapsEngine.GameViewportWidth / 10);
SnapsEngine.AddSpriteToGame(NinjaImage);
NinjaSprite ninja1 = new NinjaSprite(NinjaImage,120,10,3);
sprites.Add(ninja1);
ImageSprite KnifeImage = new ImageSprite(imageURL: "ms-appx:///Images/knife1.png");
KnifeImage.ScaleSpriteWidth(SnapsEngine.GameViewportWidth / 20);
SnapsEngine.AddSpriteToGame(KnifeImage);
KnifeSprite knife = new KnifeSprite(game:this,sprite:KnifeImage, Petya:ninja1, xSpeed:0, ySpeed:-20);
sprites.Add(knife);
titleScreen = new ImageSprite(imageURL: "ms-appx:///Images/SpaceRocketsInSpaceTitleScreen.png");
titleScreen.Hide();
SnapsEngine.AddSpriteToGame(titleScreen);
titleScreen.Width = SnapsEngine.GameViewportWidth;
titleScreen.Height = SnapsEngine.GameViewportHeight;
IntermissionScreen = new ImageSprite(imageURL: "ms-appx:///Images/wood tile background1000x1000.png");
IntermissionScreen.Hide();
SnapsEngine.AddSpriteToGame(IntermissionScreen);
IntermissionScreen.Width = SnapsEngine.GameViewportWidth;
IntermissionScreen.Height = SnapsEngine.GameViewportHeight;
gameOverScreen = new ImageSprite(imageURL: "ms-appx:///Images/SpaceRocketsInSpaceGameOverScreen.png");
gameOverScreen.Hide();
SnapsEngine.AddSpriteToGame(gameOverScreen);
gameOverScreen.Width = SnapsEngine.GameViewportWidth;
gameOverScreen.Height = SnapsEngine.GameViewportHeight;
messageBack = new TextBlockSprite(text: "", fontSize: 60,
fontFamily: "Impact", color: SnapsColor.Black);
SnapsEngine.AddSpriteToGame(messageBack);
message = new TextBlockSprite(text: "", fontSize: 60,
fontFamily: "Impact", color: SnapsColor.Red);
SnapsEngine.AddSpriteToGame(message);
}
public void EndCurrentGame()
{
titleScreen.Hide();
gameOverScreen.Show();
state = GameStates.GameOver;
gameOverTimer = 0;
}
public void StartNewGame()
{
ResetGame();
//gameOverScreen.Hide();
titleScreen.Hide();
state = GameStates.GameActive;
}
int intermissionTimer;
public void UpdateIntermission()
{
titleScreen.Hide();
IntermissionScreen.Show();
state = GameStates.Intermission;
gameOverTimer = 0;
intermissionTimer = intermissionTimer + 1;
if (intermissionTimer > 100)
{
IntermissionScreen.Hide();
StartNewGame();
}
}
public void UpdateTitle()
{
if (SnapsEngine.GetFireGamepad())
{
UpdateIntermission();
//StartNewGame();
}
}
int gameOverTimer=0;
public void UpdateGameOver()
{
gameOverTimer = gameOverTimer + 1;
if (gameOverTimer > 300)
{
ShowTitleScreen();
}
}
public void UpdateGame()
{
foreach (IGameSprite sprite in sprites)
sprite.Update();
string status = "Score: " + knife.KnivesLeft.ToString();
message.Text = status;
messageBack.Text = status;
messageBack.Bottom = SnapsEngine.GameViewportHeight - 74;
messageBack.CenterX = SnapsEngine.GameViewportWidth / 6;
message.Bottom = SnapsEngine.GameViewportHeight - 70;
message.CenterX = SnapsEngine.GameViewportWidth / 6 + 4;
}
public void PlayGame()
{
SetupGame();
ResetGame();
ShowTitleScreen();
while (true)
{
switch (state)
{
case GameStates.TitleScreen:
UpdateTitle();
break;
case GameStates.Intermission:
UpdateIntermission();
break;
case GameStates.GameActive:
UpdateGame();
break;
case GameStates.GameOver:
UpdateGameOver();
break;
}
SnapsEngine.DrawGamePage();
}
}
}
public void StartProgram()
{
KniferGame game = new KniferGame();
game.PlayGame();
}
}
答案 0 :(得分:3)
问题出在SetupGame()
。
KnifeSprite knife = new KnifeSprite(game:this,sprite:KnifeImage, Petya:ninja1, xSpeed:0, ySpeed:-20);
您正在声明名为knife
的新本地变量,并且从不填充名为knife
的字段。所以它永远保持为空。
将其更改为:
this.knife = new KnifeSprite(game:this,sprite:KnifeImage, Petya:ninja1, xSpeed:0, ySpeed:-20);