Получая ошибку при следовании за репетитором, может ли кто-нибудь, пожалуйста, помочь мне.
Я следую за репетитором, чтобы создать игру, но я получаю ошибку в этой строке
для (int i = 0; i < звезды.Длина /2; i++)
C# говорит мне, что звезды равны нулю.
Ошибка System.NullReferenceException: ‘ссылка на объект не установлена на экземпляр объекта’.
Я проверил код и не вижу никакой разницы между моим кодом и вашим кодом. Может быть, есть что-то, что мне нужно сделать с формой, чтобы остановить эту ошибку.
Код включен.
Обратите внимание, что код не является полным, но это не мешает ему выдавать мне ошибку при компиляции кода.
Brian_TheLion
Я использую последнюю версию Visual Studio, и учебник предназначен для #7 Visual Studio, так что, возможно, это причина ошибки или есть что-то, что мне нужно было сделать, чего не хватает в учебнике. Я проверил свой код с кодом в учебнике, но это то же самое.
Надеюсь, ты мне поможешь.
Что я уже пробовал:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WMPLib; namespace SpaceShooter { public partial class Form1 : Form { WindowsMediaPlayer gameMedia; WindowsMediaPlayer shootgMedia; PictureBox[] stars; PictureBox[] munitions; //gun fire at enemy int backgroundspeed; int playerSpeed; int munitionsSpeed; Random rnd; public Form1() { InitializeComponent(); } //window_Load methoid private void Form1_Load(object sender, EventArgs e) { backgroundspeed = 4; playerSpeed = 4; munitionsSpeed = 20; munitions = new PictureBox[3]; //Load images Image munition = Image.FromFile(@"asserts\munitation.png"); for(int i = 0; i < munitions.Length; i++) { munitions[i] = new PictureBox(); munitions[i].Size = new Size(8, 8); munitions[i].Image = munition; munitions[i].SizeMode = PictureBoxSizeMode.Zoom; munitions[i].BorderStyle = BorderStyle.None; this.Controls.Add(munitions[i]); } //Create WMP gameMedia = new WindowsMediaPlayer(); shootgMedia = new WindowsMediaPlayer(); //Load all songs gameMedia.URL = "songs\\GameSong.mp3"; shootgMedia.URL = "songs\\shoot.mp3"; //Setup Songs settings gameMedia.settings.setMode("loop", true); gameMedia.settings.volume = 5; shootgMedia.settings.volume = 1; stars = new PictureBox[10]; rnd = new Random(); for (int i = 0; i < stars.Length; i++) { stars[i] = new PictureBox(); { stars[i].BorderStyle = BorderStyle.None; stars[i].Location = new Point(rnd.Next(20, 580), rnd.Next(-10, 400)); } if (i % 2 == 1) { stars[i].Size = new Size(2, 2); stars[i].BackColor = Color.Wheat; } else { stars[i].Size = new Size(3, 3); stars[i].BackColor = Color.DarkGray; } this.Controls.Add(stars[i]); } gameMedia.controls.play(); //might be correct place for this } //Move Game Timer_Tick private void MoveBgTimer_Tick(object sender, EventArgs e) { for (int i = 0; i < stars.Length /2; i++) //ERROR on this line. stars = null { stars[i].Top += backgroundspeed; if (stars[i].Top >= this.Height) { stars[i].Top = -stars[i].Height; } } for (int i = stars.Length /2; i <stars.Length; i++) { stars[i].Top += backgroundspeed - 2; if (stars[i].Top >= this.Height) { stars[i].Top = -stars[i].Height; } } } private void LeftMoveTimer_Tick(object sender, EventArgs e) { if(Player.Left > 10) { Player.Left -= playerSpeed; } } private void RightMoveTimer_Tick(object sender, EventArgs e) { if (Player.Right <580) { Player.Left += playerSpeed; } } private void DownMoveTimer_Tick(object sender, EventArgs e) { if (Player.Top <400) { Player.Top += playerSpeed; } } private void UpMoveTimer_Tick(object sender, EventArgs e) { if (Player.Top >10) { Player.Top -= playerSpeed; } } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Right) { RightMoveTimer.Start(); } if (e.KeyCode == Keys.Left) { LeftMoveTimer.Start(); } if (e.KeyCode == Keys.Down) { DownMoveTimer.Start(); } if (e.KeyCode == Keys.Up) { UpMoveTimer.Start(); } } private void Form1_KeyUp(object sender, KeyEventArgs e) { RightMoveTimer.Stop(); LeftMoveTimer.Stop(); DownMoveTimer.Stop(); UpMoveTimer.Stop(); } private void MoveMunitionTimer_Tick(object sender, EventArgs e) { for (int i = 0; i < munitions.Length; i++) { if (munitions[i].Top > 0) { munitions[i].Visible = true; munitions[i].Top -= munitionsSpeed; } else { munitions[i].Visible = false; munitions[i].Location = new Point(Player.Location.X + 20, Player.Location.Y + 30); } } } } }