4 Replies - 13092 Views - Last Post: 20 March 2011 - 05:15 PM

#1 Cyclone  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 13-February 09

Instances of an object

Posted 19 March 2011 - 04:41 PM

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.

The code below uses .net but this is more of c++ question.

#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

This post has been edited by Cyclone: 19 March 2011 - 04:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Instances of an object

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Instances of an object

Posted 19 March 2011 - 05:02 PM

Well the problem here is this line...

if(e->KeyCode == Keys::Down) {o1 = gcnew object(this); }



Do you know what this is actually doing? For each keydown it is creating a new object and putting it into o1... clobbering whatever object o1 originally pointed to. This means when you create one object and then press down again, it creates another object and now o1 refers to the last object made, the first one you created before is then lost.

This is the reason why you only see the last one change color. At any given time o1 is ONLY referring to the last object created. The rest of the other missiles you created are not pointed to by o1 anymore.

Now knowing this, you probably want to create a list of missile objects and when they collide, you can still have a reference to them to then change their color. Then of course when they change color, you can also use that reference to Dispose() of them.

:)
Was This Post Helpful? 0
  • +
  • -

#3 Cyclone  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 13-February 09

Re: Instances of an object

Posted 19 March 2011 - 05:09 PM

Thanks for the speedy reply. :)

Yea I know what it's doing, well mostly.

Are you saying I need to create a seperate instance for each missile. like m1,m2,m3 for as many I want the program to handle?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6057
  • View blog
  • Posts: 23,494
  • Joined: 23-August 08

Re: Instances of an object

Posted 20 March 2011 - 05:26 AM

Moved to CLI C++
Was This Post Helpful? 0
  • +
  • -

#5 Cyclone  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 13-February 09

Re: Instances of an object

Posted 20 March 2011 - 05:15 PM

Sorry always get confused which is the best forum using mixed code.

As for my problem I got it working using lists.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1