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

Managed C++/CLI

 
GeneralRe: Animating a snake (aka Snafu) Pin
Richard Andrew x6427-Mar-11 15:24
professionalRichard Andrew x6427-Mar-11 15:24 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S28-Mar-11 13:21
Cyclone_S28-Mar-11 13:21 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S30-Mar-11 13:22
Cyclone_S30-Mar-11 13:22 
GeneralRe: Animating a snake (aka Snafu) Pin
Richard Andrew x6430-Mar-11 14:41
professionalRichard Andrew x6430-Mar-11 14:41 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S31-Mar-11 9:14
Cyclone_S31-Mar-11 9:14 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S16-Apr-11 8:44
Cyclone_S16-Apr-11 8:44 
Questionissue while loading MFC extension dll dynamically Pin
ptr_Electron24-Mar-11 23:37
ptr_Electron24-Mar-11 23:37 
AnswerRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 0:02
mveRichard MacCutchan25-Mar-11 0:02 
GeneralRe: issue while loading MFC extension dll dynamically Pin
ptr_Electron25-Mar-11 0:21
ptr_Electron25-Mar-11 0:21 
GeneralRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 1:03
mveRichard MacCutchan25-Mar-11 1:03 
GeneralRe: issue while loading MFC extension dll dynamically Pin
ptr_Electron25-Mar-11 2:50
ptr_Electron25-Mar-11 2:50 
GeneralRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 4:24
mveRichard MacCutchan25-Mar-11 4:24 
QuestionInstances of an object Pin
Cyclone_S19-Mar-11 15:47
Cyclone_S19-Mar-11 15:47 
Hi,

I'm currently making a simple game. Pressing the down arrow places a box on the form. Pressing up shoots missiles(blue boxes) and creates multiple instances of m1 which is part of the missle class.

What I want to do is change the color of each missle as it collides with the red box. So far only the last missile changes color. The other instances of the missle class get ignored.




#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

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

public ref class missile
{
	public:
			PictureBox^ Box1;
			Timer^ Timer1;

			missile( Form ^ form ) // Class Constructor.
			{
				Timer1 = gcnew Timer;
				Timer1->Interval = 1;
				Timer1->Start();
				Box1 = gcnew PictureBox();
				Box1->Left = 150;
				Box1->Top = 240;
				Box1->Width = 10;
				Box1->Height = 10;
				Box1->BackColor = System::Drawing::Color::Blue;
				form->Controls->Add(Box1);
				Timer1->Tick += gcnew System::EventHandler(this, &missile::timer1_Tick);
			}

		System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
		{
			Box1->Top -= 1;
		}
};


public ref class Form1 : public Form 
{
	public:
			PictureBox^ Box2;
			missile^ m1;
			object^ o1;
			Timer^ Timer3;
			bool x;

			Form1() // Class constructor.
			{	
				x=false;
				Timer3 = gcnew Timer();
				Timer3->Interval = 1;
				Timer3->Start();
				Box2 = gcnew PictureBox();
				Box2->BackColor = Color::Blue;
				Box2->Top = 240;
				Box2->Left = (this->Width / 2) - 40;
				Box2->Width = 40;
				Box2->Height = 10;
				this->Controls->Add(Box2);
				this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyDown);
				Timer3->Tick += gcnew System::EventHandler(this, &Form1::timer3_Tick);
			}

			System::Void Form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
			{	
				if(e->KeyCode == Keys::Up){x=true;m1 = gcnew missile(this);m1->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;}
				if(e->KeyCode == Keys::Left){Box2->Left -= 4;}
				if(e->KeyCode == Keys::Right){Box2->Left += 4;}
				if(e->KeyCode == Keys::Down) {o1 = gcnew object(this); }
			}

			System::Void timer3_Tick(System::Object^  sender, System::EventArgs^  e)
			{
				if(x==true)
				{
					if(m1->Box1->Top == o1->Box3->Bottom){m1->Box1->BackColor = Color::Green;}
				}
			}

			bool isColliding(PictureBox^ box1, PictureBox^ box3)
			{
				Rectangle r1 = m1->Box1->Bounds;
				Rectangle r2 = o1->Box3->Bounds;
				return r1.IntersectsWith(r2); //return true if they collide
			}
			
};


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



I also am trying to figure out how to remove the instances from memory after a set amount of time as too many instances slows the program.

Thanks for the help.
AnswerRe: Instances of an object Pin
John Schroedl19-Mar-11 17:51
professionalJohn Schroedl19-Mar-11 17:51 
GeneralRe: Instances of an object Pin
John Schroedl19-Mar-11 18:01
professionalJohn Schroedl19-Mar-11 18:01 
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 
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 

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.