Instances of an object [modified]
-
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->
-
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->
Hi, welcome to CodeProject. I have several comments on your code: 1. you did something wrong when posting the code as all > signs became double-HTML-encoded (see the >), making things pretty hard to read. 2. I don't understand at all why this would be a C/C++ question, it looks like C++/CLI to me; that is .NET's version of C++, and it has its own forum on CodeProject. The current one is for native, unmanaged C or C++ code. 3. a timer interval of 1 msec is not what you are going to get, you may want to read my article here: Timer surprises, and how to avoid them[^]. 4. You are using a PictureBox for each missile; I wouldn't do that. All the PB does for you is remember its Location and show an image somewhere on your form. You could as well paint the image yourself, by overriding the Form's OnPaint method (or, what I prefer: by adding a Panel and overriding its OnPaint). So in more detail I would: - define a little Missile class with a Point and a Color member, and a PaintMe(Graphics g, Point at) method. - instantiate Missiles and hold them in a generic List. - in the Form/Panel's OnPaint enumerate those Missiles and have each of them paint itself. Doing so would allow you to run hundreds of missiles without things getting slow. 5. Collision detection could benefit from the same list of Missiles (that is assuming multiple missiles are moving at the same time, either controlled by your key stuff, or maybe just by a timer). You could again enumerate the Missile instances, and for each of them check for a collision and act accordingly (change color, explode, break in two, whatever). :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hi, welcome to CodeProject. I have several comments on your code: 1. you did something wrong when posting the code as all > signs became double-HTML-encoded (see the >), making things pretty hard to read. 2. I don't understand at all why this would be a C/C++ question, it looks like C++/CLI to me; that is .NET's version of C++, and it has its own forum on CodeProject. The current one is for native, unmanaged C or C++ code. 3. a timer interval of 1 msec is not what you are going to get, you may want to read my article here: Timer surprises, and how to avoid them[^]. 4. You are using a PictureBox for each missile; I wouldn't do that. All the PB does for you is remember its Location and show an image somewhere on your form. You could as well paint the image yourself, by overriding the Form's OnPaint method (or, what I prefer: by adding a Panel and overriding its OnPaint). So in more detail I would: - define a little Missile class with a Point and a Color member, and a PaintMe(Graphics g, Point at) method. - instantiate Missiles and hold them in a generic List. - in the Form/Panel's OnPaint enumerate those Missiles and have each of them paint itself. Doing so would allow you to run hundreds of missiles without things getting slow. 5. Collision detection could benefit from the same list of Missiles (that is assuming multiple missiles are moving at the same time, either controlled by your key stuff, or maybe just by a timer). You could again enumerate the Missile instances, and for each of them check for a collision and act accordingly (change color, explode, break in two, whatever). :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Sorry about that. I fixed the post. How would I get my code working using my current aproach. I will try to redo the code after I know whats wrong with it\fix it. By enumerate do you mean to specify each missile individualy like
missile^ m1;
missile^ m2;
missile^ m3;m1 = gcnew missile(this);
m2 = gcnew missile(this);
m3 = gcnew missile(this);
etc -
Sorry about that. I fixed the post. How would I get my code working using my current aproach. I will try to redo the code after I know whats wrong with it\fix it. By enumerate do you mean to specify each missile individualy like
missile^ m1;
missile^ m2;
missile^ m3;m1 = gcnew missile(this);
m2 = gcnew missile(this);
m3 = gcnew missile(this);
etcNot really. This is how it would look in C#; C++/CLI allows similar stuff, I'm less fluent in it:
// declare a list of missiles
List<Missile> missiles=new List<Missile>(); // a List is like an auto-dimensioning array!// create missiles and load the list
for(int i=0; i<10; i++) {
Missile m=new Missile();
m.Location=new Point(100+33*i, 200*10*i);
m.SetColor(Color.Yellow);
missiles.Add(m);
}// do something to those missiles, one by one, by enumerating them
foreach(Missile m in missiles) {
if (m.IsHit()) m.SetColor(Color.Green);
}which illustrates you don't need individual variables for collectible items at all. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Not really. This is how it would look in C#; C++/CLI allows similar stuff, I'm less fluent in it:
// declare a list of missiles
List<Missile> missiles=new List<Missile>(); // a List is like an auto-dimensioning array!// create missiles and load the list
for(int i=0; i<10; i++) {
Missile m=new Missile();
m.Location=new Point(100+33*i, 200*10*i);
m.SetColor(Color.Yellow);
missiles.Add(m);
}// do something to those missiles, one by one, by enumerating them
foreach(Missile m in missiles) {
if (m.IsHit()) m.SetColor(Color.Green);
}which illustrates you don't need individual variables for collectible items at all. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.