error message
-
Hi, I am trying to make a brick game. I want to have a class for the players ball. The ball will split in 3 when a certain item is collected. The code compiles fine but when it runs it gives me the error "Object reference not set to an instance of an object." Also what's the best way of writing the code for 3 ball objects without having to duplicate code 3 times. Thanks.
#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;public ref class Ball
{
public:
PictureBox^ gameBall;
int speed;
double xVel;
double yVel;Ball() // Class constructor. { } void Add\_to\_form( Form ^ form ) { // Variables speed = 5; xVel = speed; yVel = speed; gameBall = gcnew PictureBox(); gameBall->BackColor = Color::LimeGreen; gameBall->Size = Drawing::Size(20,20); gameBall->Location = Drawing::Point(form->Width/2-35,form->Height/2-50); form->Controls->Add(gameBall); }
};
public ref class Form1 : public Form
{
public:
PictureBox^ paddlePlayer;
Timer^ gameTimer;
Ball ball1; // Create ball1.Form1() // Class constructor. { this->Size = Drawing::Size(640,480); this->BackColor= Color::SkyBlue; this->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pongMain\_MouseMove); gameTimer = gcnew Timer(); gameTimer->Interval = 20; gameTimer->Start(); gameTimer->Tick += gcnew System::EventHandler(this, &Form1::gameTimer\_Tick); paddlePlayer = gcnew PictureBox(); paddlePlayer->BackColor = Color::LimeGreen; paddlePlayer->Size = Drawing::Size(128,16); paddlePlayer->Location = Drawing::Point(this->Width/2,this->Height-65); this->Controls->Add(paddlePlayer); this->Controls->Add(ball1.gameBall); } System::Void pongMain\_MouseMove( Object^, System::Windows::Forms::MouseEventArgs^ e) { if(e->X < this->Width-paddlePlayer->Width - 18) { paddlePlayer->Location = Drawing::Point(e->X,paddlePlayer->Location.Y); } } System::Void gameTimer\_Tick(System::Object^ sender, System::EventArgs^ e) { //Move the game ball. ball1.gameBall->Location = Point(ball1.gameBall->Location.X + Convert::ToUInt32(ball1.xVel), ball1.gameBall->Location.Y + Convert::ToUInt32(ball1.yVel)); // Check for t
-
Hi, I am trying to make a brick game. I want to have a class for the players ball. The ball will split in 3 when a certain item is collected. The code compiles fine but when it runs it gives me the error "Object reference not set to an instance of an object." Also what's the best way of writing the code for 3 ball objects without having to duplicate code 3 times. Thanks.
#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;public ref class Ball
{
public:
PictureBox^ gameBall;
int speed;
double xVel;
double yVel;Ball() // Class constructor. { } void Add\_to\_form( Form ^ form ) { // Variables speed = 5; xVel = speed; yVel = speed; gameBall = gcnew PictureBox(); gameBall->BackColor = Color::LimeGreen; gameBall->Size = Drawing::Size(20,20); gameBall->Location = Drawing::Point(form->Width/2-35,form->Height/2-50); form->Controls->Add(gameBall); }
};
public ref class Form1 : public Form
{
public:
PictureBox^ paddlePlayer;
Timer^ gameTimer;
Ball ball1; // Create ball1.Form1() // Class constructor. { this->Size = Drawing::Size(640,480); this->BackColor= Color::SkyBlue; this->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pongMain\_MouseMove); gameTimer = gcnew Timer(); gameTimer->Interval = 20; gameTimer->Start(); gameTimer->Tick += gcnew System::EventHandler(this, &Form1::gameTimer\_Tick); paddlePlayer = gcnew PictureBox(); paddlePlayer->BackColor = Color::LimeGreen; paddlePlayer->Size = Drawing::Size(128,16); paddlePlayer->Location = Drawing::Point(this->Width/2,this->Height-65); this->Controls->Add(paddlePlayer); this->Controls->Add(ball1.gameBall); } System::Void pongMain\_MouseMove( Object^, System::Windows::Forms::MouseEventArgs^ e) { if(e->X < this->Width-paddlePlayer->Width - 18) { paddlePlayer->Location = Drawing::Point(e->X,paddlePlayer->Location.Y); } } System::Void gameTimer\_Tick(System::Object^ sender, System::EventArgs^ e) { //Move the game ball. ball1.gameBall->Location = Point(ball1.gameBall->Location.X + Convert::ToUInt32(ball1.xVel), ball1.gameBall->Location.Y + Convert::ToUInt32(ball1.yVel)); // Check for t
-
Hi, I am trying to make a brick game. I want to have a class for the players ball. The ball will split in 3 when a certain item is collected. The code compiles fine but when it runs it gives me the error "Object reference not set to an instance of an object." Also what's the best way of writing the code for 3 ball objects without having to duplicate code 3 times. Thanks.
#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;public ref class Ball
{
public:
PictureBox^ gameBall;
int speed;
double xVel;
double yVel;Ball() // Class constructor. { } void Add\_to\_form( Form ^ form ) { // Variables speed = 5; xVel = speed; yVel = speed; gameBall = gcnew PictureBox(); gameBall->BackColor = Color::LimeGreen; gameBall->Size = Drawing::Size(20,20); gameBall->Location = Drawing::Point(form->Width/2-35,form->Height/2-50); form->Controls->Add(gameBall); }
};
public ref class Form1 : public Form
{
public:
PictureBox^ paddlePlayer;
Timer^ gameTimer;
Ball ball1; // Create ball1.Form1() // Class constructor. { this->Size = Drawing::Size(640,480); this->BackColor= Color::SkyBlue; this->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pongMain\_MouseMove); gameTimer = gcnew Timer(); gameTimer->Interval = 20; gameTimer->Start(); gameTimer->Tick += gcnew System::EventHandler(this, &Form1::gameTimer\_Tick); paddlePlayer = gcnew PictureBox(); paddlePlayer->BackColor = Color::LimeGreen; paddlePlayer->Size = Drawing::Size(128,16); paddlePlayer->Location = Drawing::Point(this->Width/2,this->Height-65); this->Controls->Add(paddlePlayer); this->Controls->Add(ball1.gameBall); } System::Void pongMain\_MouseMove( Object^, System::Windows::Forms::MouseEventArgs^ e) { if(e->X < this->Width-paddlePlayer->Width - 18) { paddlePlayer->Location = Drawing::Point(e->X,paddlePlayer->Location.Y); } } System::Void gameTimer\_Tick(System::Object^ sender, System::EventArgs^ e) { //Move the game ball. ball1.gameBall->Location = Point(ball1.gameBall->Location.X + Convert::ToUInt32(ball1.xVel), ball1.gameBall->Location.Y + Convert::ToUInt32(ball1.yVel)); // Check for t
-
Just create three instances of your Ball class. Instead of managing a single Ball instance, use a list of Balls, even when you only have one entry. Iterate over the list of balls each frame.