Click here to Skip to main content
15,921,382 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: implementing multi linked list correctly_? Pin
David Crow2-May-11 9:13
David Crow2-May-11 9:13 
GeneralRe: implementing multi linked list correctly_? Pin
quartaela2-May-11 9:37
quartaela2-May-11 9:37 
QuestionHow does Google Chrome Installer gets installed with UAC Prompt Pin
Raj Aryan 10011-May-11 22:29
Raj Aryan 10011-May-11 22:29 
AnswerRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Hans Dietrich1-May-11 22:53
mentorHans Dietrich1-May-11 22:53 
GeneralRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Raj Aryan 10011-May-11 23:00
Raj Aryan 10011-May-11 23:00 
AnswerRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Hans Dietrich1-May-11 23:06
mentorHans Dietrich1-May-11 23:06 
GeneralRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Raj Aryan 10011-May-11 23:13
Raj Aryan 10011-May-11 23:13 
AnswerRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Hans Dietrich2-May-11 5:36
mentorHans Dietrich2-May-11 5:36 
GeneralRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Raj Aryan 10012-May-11 5:49
Raj Aryan 10012-May-11 5:49 
AnswerRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Hans Dietrich2-May-11 6:04
mentorHans Dietrich2-May-11 6:04 
GeneralRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Raj Aryan 10012-May-11 6:09
Raj Aryan 10012-May-11 6:09 
AnswerRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Luc Pattyn1-May-11 22:57
sitebuilderLuc Pattyn1-May-11 22:57 
GeneralRe: How does Google Chrome Installer gets installed with UAC Prompt Pin
Raj Aryan 10011-May-11 23:25
Raj Aryan 10011-May-11 23:25 
Questionerror message Pin
Cyclone_S1-May-11 21:22
Cyclone_S1-May-11 21:22 
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 top wall.  
				if(ball1.gameBall->Location.Y < 0)
				{
					ball1.gameBall->Location = Point(ball1.gameBall->Location.X, 0);
					ball1.yVel = -ball1.yVel;
				}

				// Check for bottom wall.  

				if(ball1.gameBall->Location.Y > this->Height - ball1.gameBall->Size.Height - 45) 
				{
					ball1.gameBall->Location = Point(ball1.gameBall->Location.X, this->Height - ball1.gameBall->Size.Height - 45);  
					ball1.yVel = -ball1.yVel;
				}

				// Right wall.
				if(ball1.gameBall->Location.X > this->Width - ball1.gameBall->Size.Width-25)
				{
					ball1.gameBall->Location = Point(ball1.gameBall->Location.X,ball1.gameBall->Location.Y);
					ball1.xVel = -ball1.xVel;
				}

				// Left wall.
				if(ball1.gameBall->Location.X <= 0)
				{
					ball1.gameBall->Location = Point(ball1.gameBall->Location.X,ball1.gameBall->Location.Y);
					ball1.xVel = -ball1.xVel;
				}

				// Check for player paddle.  
				if(ball1.gameBall->Bounds.IntersectsWith(paddlePlayer->Bounds))
				{
					ball1.gameBall->Location = Point(ball1.gameBall->Location.X,ball1.gameBall->Location.Y);  
				    ball1.yVel = -ball1.yVel;
				}
			}


};

[STAThread]
int main()
{	
    Application::Run(gcnew Form1());
}

AnswerRe: error message Pin
ShilpiP1-May-11 22:04
ShilpiP1-May-11 22:04 
AnswerRe: error message Pin
Niklas L2-May-11 7:59
Niklas L2-May-11 7:59 
GeneralRe: error message Pin
Cyclone_S3-May-11 11:21
Cyclone_S3-May-11 11:21 
QuestionMFC: ownderdrawn menu, how can I change the default menu border? Pin
Erik1-May-11 19:53
Erik1-May-11 19:53 
AnswerRe: MFC: ownderdrawn menu, how can I change the default menu border? Pin
Hans Dietrich1-May-11 20:14
mentorHans Dietrich1-May-11 20:14 
QuestionPrinting out characters from a file Pin
Francis Paran1-May-11 14:14
Francis Paran1-May-11 14:14 
QuestionRe: Printing out characters from a file Pin
David Crow1-May-11 14:55
David Crow1-May-11 14:55 
AnswerRe: Printing out characters from a file Pin
Francis Paran2-May-11 9:45
Francis Paran2-May-11 9:45 
Questionhow to convert images through C++ Pin
aesthetic.crazy1-May-11 7:21
aesthetic.crazy1-May-11 7:21 
AnswerRe: how to convert images through C++ Pin
enhzflep1-May-11 7:35
enhzflep1-May-11 7:35 
GeneralRe: how to convert images through C++ Pin
Rajesh R Subramanian1-May-11 9:15
professionalRajesh R Subramanian1-May-11 9:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.