Click here to Skip to main content
15,946,988 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 9:56
Cyclone_S20-Mar-11 9:56 
AnswerRe: Instances of an object Pin
Luc Pattyn20-Mar-11 10:47
sitebuilderLuc Pattyn20-Mar-11 10:47 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 14:47
Cyclone_S20-Mar-11 14:47 
AnswerRe: Instances of an object Pin
Luc Pattyn20-Mar-11 14:50
sitebuilderLuc Pattyn20-Mar-11 14:50 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 15:30
Cyclone_S20-Mar-11 15:30 
GeneralRe: Instances of an object Pin
John Schroedl21-Mar-11 4:02
professionalJohn Schroedl21-Mar-11 4:02 
GeneralRe: Instances of an object Pin
Cyclone_S21-Mar-11 15:44
Cyclone_S21-Mar-11 15:44 
GeneralRe: Instances of an object Pin
Cyclone_S23-Mar-11 13:21
Cyclone_S23-Mar-11 13:21 
I have one little issue. I can't seem to delete the enemy object. I have tried "delete o1" and dispose "o1->Box3->Dispose" and none work. Any ideas? Thanks.

System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) // A timer that checks for missile collisions.
			{
				for each (missile^ m in Missiles) // Go through the "Missiles" list and check for certain conditions such as a missile collision.
				{
					if(IsColliding(m)) // Run the collision detection function.
					{
						collision = false;
						if(m->Timer1->Enabled || m->Timer2->Enabled || m->Timer3->Enabled || m->Timer4->Enabled){collision_counter++;}
						Label1->Text = collision_counter.ToString();
						if(collision_counter == 1){} // Destroy the enemy object.
						m->Timer1->Stop(); // Stop the missiles from animatiing.
						m->Timer2->Stop();
						m->Timer3->Stop();
						m->Timer4->Stop();
					}
				}
				Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen)); // Remove any m("missile" class object instances) from the "Missile" list.
			}



Entire Code

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Collections::Generic;

public ref class missile // missile class
{
	public:
			PictureBox^ Box1; // Create the missile using a Picturebox control.			
			Timer^ Timer1;
			Timer^ Timer2;
			Timer^ Timer3;
			Timer^ Timer4;

			missile( Form ^ form ) // Class Constructor.
			{
				Timer1 = gcnew Timer();
				Timer2 = gcnew Timer();
				Timer3 = gcnew Timer();
				Timer4 = gcnew Timer();
				Timer1->Interval = 1;
				Timer1->Start();
				Timer2->Interval = 1;
				Timer3->Interval = 1;
				Timer4->Interval = 1;
				Box1 = gcnew PictureBox();
				Box1->Width = 10;
				Box1->Height = 10;
				Box1->Left = 150;
				Box1->Top = 335;
				Box1->BackColor = System::Drawing::Color::Blue;
				form->Controls->Add(Box1);
				Timer1->Tick += gcnew System::EventHandler(this, &missile::timer1_Tick);
				Timer2->Tick += gcnew System::EventHandler(this, &missile::timer2_Tick);
				Timer3->Tick += gcnew System::EventHandler(this, &missile::timer3_Tick);
				Timer4->Tick += gcnew System::EventHandler(this, &missile::timer4_Tick);
			}
			
			System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
			{
				Box1->Top--; // Move the missiles up using a timer.
			}

			System::Void timer2_Tick(System::Object^  sender, System::EventArgs^  e) 
			{
				Box1->Left++; // Move the missiles right using a timer.
			}

			System::Void timer3_Tick(System::Object^  sender, System::EventArgs^  e) 
			{
				Box1->Top++; // Move the missiles down using a timer.
			}

			System::Void timer4_Tick(System::Object^  sender, System::EventArgs^  e) 
			{
				Box1->Left--; // Move the missiles left using a timer.
			}
};

public ref class object // The enemy 'object' class.
{
	public:
			PictureBox^ Box3; // Create the enemies using a Picturebox control.
			

			object( Form ^ form ) // Class Constructor.
			{   
				Box3 = gcnew PictureBox();
				Box3->Width = 100;
				Box3->Height = 100;
				Box3->Left = 145;
				Box3->Top = 125;
				Box3->BackColor = System::Drawing::Color::Red;
				form->Controls->Add(Box3);
			}
};

public ref class laser // Enemy lasers.
{
	public:
			PictureBox^ Box4;
			Timer^ Timer1;

			laser( Form ^ form ) // Class Constructor.
			{
				Timer1 = gcnew Timer();
				Timer1->Interval = 1;
				Timer1->Start();
				Box4 = gcnew PictureBox();
				Box4->Width = 8;
				Box4->Height = 50;
				Box4->Left = 145;
				Box4->Top = 125;
				Box4->BackColor = System::Drawing::Color::Red;
				form->Controls->Add(Box4);
				Timer1->Tick += gcnew System::EventHandler(this, &laser::timer1_Tick);
			}

			System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
			{
				Box4->Top++; // Move the missiles down using a timer.
			}
};

public ref class Form1 : public Form // The main form.
{
	public:
			PictureBox^ Box2; // Create the turret using a Picturebox control.
			object^ o1;
			Timer^ Timer1; // A timer which checks for missile collisions.
			Timer^ Timer2; // A timer which fires lasers from the enemy objects.
			List<missile^>^ Missiles; // A list that contains the missile objects.
			List<laser^>^ Lasers; // A list that contains the laser objects.
			bool collision;
			bool tLeft;
			bool tBottom;
			bool tTop;
			bool tRight;
			int collision_counter;
			Label^ Label1;

			Form1() // Class constructor.
			{	
				Label1 = gcnew Label();
				Label1->Text = "0";
				tBottom = true;
				Missiles = gcnew List<missile^>();
				Lasers = gcnew List<laser^>();
				Timer1 = gcnew Timer();
				Timer1->Interval = 1;
				Timer1->Start();
				Timer2 = gcnew Timer();
				Timer2->Interval = 1000;
				Box2 = gcnew PictureBox();
				Box2->BackColor = Color::Blue;
				Box2->Top = 340;
				Box2->Left = (this->Width / 2) + 15;
				Box2->Width = 40;
				Box2->Height = 10;
				this->Controls->Add(Box2);
				this->Controls->Add(Label1);
				this->Width = 400;
				this->Height = 400;
				this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyDown);
				Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
				Timer2->Tick += gcnew System::EventHandler(this, &Form1::timer2_Tick);
			}

			System::Void Form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
			{	
				if(o1 != nullptr)
				{
				if(e->KeyCode == Keys::Up) // Fire the missiles.
				{	
					missile^ m = gcnew missile(this);
					if(tBottom == true)
					{
						m->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;
						m->Timer1->Start();m->Timer2->Stop();m->Timer3->Stop();m->Timer4->Stop();
					}
					else if(tLeft == true)
					{
						m->Timer1->Stop(); m->Timer2->Start();m->Timer3->Stop();m->Timer4->Stop();
						m->Box1->Left = Box2->Left + Box2->Width;
						m->Box1->Top = Box2->Top + (Box2->Width / 2) + 10;
					}
					else if(tTop == true)
					{
						m->Timer1->Stop(); m->Timer2->Stop();m->Timer3->Start();m->Timer4->Stop();
						m->Box1->Left = Box2->Left + (Box2->Width / 2) - 6;
						m->Box1->Top = Box2->Bottom - 10;
					}
					else if(tRight == true)
					{
						m->Timer1->Stop(); m->Timer2->Stop();m->Timer3->Stop();m->Timer4->Start();
						m->Box1->Left = Box2->Left;
						m->Box1->Top = Box2->Bottom - 25;
					}
					Missiles->Add(m); // Add the m (missile object instances) to the list.
				}
				
				// Move the turret.
				if(e->KeyCode == Keys::Left) // Move the turret along all 4 sides of window.
				{

					if(tBottom == true)
					{
						Box2->Left -= 4;
						if(Box2->Left <=4)
						{
							Box2->Width = 10;Box2->Height = 40;
							Box2->Top -= 30; Box2->Left += 1;
							tBottom = false; tLeft = true;
						}
					}
					
					if(tLeft == true)
					{
						Box2->Top -= 4;
						if(Box2->Top <=4)
						{
							Box2->Width = 40;Box2->Height = 10;
							tLeft = false; tTop = true;
						}
					}

					if(tTop == true)
					{
						Box2->Left += 4;
						if(Box2->Right >= 380)
						{
							Box2->Width = 10;Box2->Height = 40;
							Box2->Left += 28; Box2->Top -= 4;
							tTop = false; tRight = true;
						}
					}

					if(tRight == true)
					{
						Box2->Top += 4;
						if(Box2->Bottom >= 350)
						{
							Box2->Width = 40;Box2->Height = 10;
							Box2->Left -= 31; Box2->Top += 30;
							tRight = false; tBottom = true;
						}
					}
				}

				if(e->KeyCode == Keys::Right)
				{
					if(tBottom == true)
					{
						Box2->Left += 4;
						if(Box2->Right >= 380)
						{
							Box2->Width = 10;Box2->Height = 40;
							Box2->Top -= 26; Box2->Left += 28;
							tBottom = false; tRight = true;
						}
					}
					
					if(tRight == true)
					{
						Box2->Top -= 4;
						if(Box2->Top <=4)
						{
							Box2->Left -= 26;
							Box2->Width = 40;Box2->Height = 10;
							tRight = false; tTop = true;
						}
					}

					if(tTop == true)
					{
						Box2->Left -= 4;
						if(Box2->Left <= 4)
						{
							
							Box2->Top -= 4;
							Box2->Width = 10;Box2->Height = 38;
							tTop = false; tLeft = true;
						}
					}

					if(tLeft == true)
					{
						Box2->Top += 4;
						if(Box2->Bottom >= 350)
						{
							Box2->Left -= 0; Box2->Top += 25;
							Box2->Width = 40;Box2->Height = 10;
							tLeft = false; tBottom = true;
						}
					}
				}
				}
				// Start the game.
				if(e->KeyCode == Keys::Down) {o1 = gcnew object(this);Timer2->Start();}
			}

			bool IsMissileOffScreen(missile^ m) // Check is a missile goes off screen.
			{
				return m->Box1->Bottom < 0;
			}

			bool IsColliding(missile^ m) // Check is a missile collides with the box.
			{
				if((m->Box1->Right >= o1->Box3->Left && m->Box1->Right <= o1->Box3->Right) ||  (m->Box1->Left >= o1->Box3->Left && m->Box1->Left <= o1->Box3->Right))
				{
					if((m->Box1->Top >= o1->Box3->Top && m->Box1->Top <= o1->Box3->Bottom) || (m->Box1->Bottom >= o1->Box3->Top && m->Box1->Bottom <= o1->Box3->Bottom))
					collision = true;
				}
				else if((m->Box1->Top >= o1->Box3->Top && m->Box1->Top <= o1->Box3->Bottom) || (m->Box1->Bottom >= o1->Box3->Top && m->Box1->Bottom <= o1->Box3->Bottom))
				{
					if((m->Box1->Right >= o1->Box3->Left && m->Box1->Right <= o1->Box3->Right) || (m->Box1->Left >= o1->Box3->Left && m->Box1->Left <= o1->Box3->Right))
					collision = true;
				}
				return collision; // Returns true if there is a collision.
			}
		
			System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) // A timer that checks for missile collisions.
			{
				for each (missile^ m in Missiles) // Go through the "Missiles" list and check for certain conditions such as a missile collision.
				{
					if(IsColliding(m)) // Run the collision detection function.
					{
						collision = false;
						if(m->Timer1->Enabled || m->Timer2->Enabled || m->Timer3->Enabled || m->Timer4->Enabled){collision_counter++;}
						Label1->Text = collision_counter.ToString();
						if(collision_counter == 1){} // Destroy the enemy object.
						m->Timer1->Stop(); // Stop the missiles from animatiing.
						m->Timer2->Stop();
						m->Timer3->Stop();
						m->Timer4->Stop();
					}
				}
				Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen)); // Remove any m("missile" class object instances) from the "Missile" list.
			}

			System::Void timer2_Tick(System::Object^  sender, System::EventArgs^  e) // A timer that fires lasers from the enemy object.
			{
				
				laser^ lD = gcnew laser(this);
				lD->Box4->Top = o1->Box3->Bottom;



				Lasers->Add(lD); // Add the l (laser object instances) to the list.
			}
};


[STAThread]
int main()
{	
    Application::Run(gcnew Form1()); // Run the application\create the main form.
}

AnswerRe: Instances of an object Pin
Luc Pattyn23-Mar-11 16:01
sitebuilderLuc Pattyn23-Mar-11 16:01 
GeneralRe: Instances of an object Pin
John Schroedl23-Mar-11 18:22
professionalJohn Schroedl23-Mar-11 18:22 
GeneralRe: Instances of an object Pin
Cyclone_S25-Mar-11 14:31
Cyclone_S25-Mar-11 14:31 
GeneralRe: Instances of an object Pin
Cyclone_S26-Mar-11 9:38
Cyclone_S26-Mar-11 9:38 
Questionreturn 2 HWND Pin
Masternoob00718-Mar-11 1:19
Masternoob00718-Mar-11 1:19 
AnswerRe: return 2 HWND Pin
Richard MacCutchan18-Mar-11 2:50
mveRichard MacCutchan18-Mar-11 2:50 
Question[C++/CLI] Progressbar doesn't work [modified] Pin
Masternoob00716-Mar-11 2:08
Masternoob00716-Mar-11 2:08 
AnswerRe: [C++/CLI] Progressbar doesn't work Pin
Luc Pattyn16-Mar-11 2:27
sitebuilderLuc Pattyn16-Mar-11 2:27 
GeneralRe: [C++/CLI] Progressbar doesn't work Pin
Masternoob00716-Mar-11 2:37
Masternoob00716-Mar-11 2:37 
Questionproblem in using library of LFSR Pin
smishtiaqhussain12-Mar-11 9:24
smishtiaqhussain12-Mar-11 9:24 
AnswerRe: problem in using library of LFSR Pin
Richard MacCutchan12-Mar-11 9:48
mveRichard MacCutchan12-Mar-11 9:48 
GeneralRe: problem in using library of LFSR Pin
smishtiaqhussain12-Mar-11 10:49
smishtiaqhussain12-Mar-11 10:49 
GeneralRe: problem in using library of LFSR Pin
Richard MacCutchan12-Mar-11 22:29
mveRichard MacCutchan12-Mar-11 22:29 
AnswerRe: problem in using library of LFSR Pin
Luc Pattyn12-Mar-11 9:59
sitebuilderLuc Pattyn12-Mar-11 9:59 
GeneralRe: problem in using library of LFSR Pin
jschell13-Mar-11 10:14
jschell13-Mar-11 10:14 
QuestionProblems using managed C++ DLL's on WinXP and Win2003 Pin
Ole Morten Heien10-Mar-11 21:36
Ole Morten Heien10-Mar-11 21:36 
AnswerRe: Problems using managed C++ DLL's on WinXP and Win2003 Pin
Richard MacCutchan10-Mar-11 22:48
mveRichard MacCutchan10-Mar-11 22:48 

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.