Instances of an object

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#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
That code looks like C#...
It's C++/CLI not C#
You'd probably have more luck with a C++/CLI forum, as C++/CLI is pretty different from C++.
closed account (3hM2Nwbp)
Well, since you're using a single managed pointer to a missile, you're effectively losing your pointers when you create a new one. This would normally create a memory leak, but the managed memory should be reclaimed automagically when the last reference to it falls out of scope (edit - or something similar to that concept - you never know with Microsoft's usual obfuscated tactics :P ). You could create a list of missile pointers and iterate through them on your game's tick.

Hope it helps,
Luc
Last edited on
closed account (z05DSL3A)
benjelly wrote:
That code looks like C#...

That comment looks like someone doesn't know what the fuck they're talking about.
Come on man. I also don't know what the f**k I am talking about, but C#, C++/CLI, what's the difference. It all ends up as MSIL. All those are front ends for the same high level language. Give the guy a break.
closed account (z05DSL3A)
Give the guy a break; no, don't think I will.
Jesus Grey Wolf chill out. Go smoke a joint or get laid.
Last edited on
I didn't see a C++/CLI forum...

Hopefully someone can answer this how do I delete an object such as m1 in my code?

thanks.
closed account (3hM2Nwbp)
Look up about 6 posts.
Thanks. At first I didn't know what you meant by list. Now I do and got it working. Thanks.
Topic archived. No new replies allowed.