Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Instances of an object

Instances of an object

Scheduled Pinned Locked Moved Managed C++/CLI
graphicsgame-dev
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyclone_S
    wrote on last edited by
    #1

    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(
    
    J 1 Reply Last reply
    0
    • C Cyclone_S

      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(
      
      J Offline
      J Offline
      John Schroedl
      wrote on last edited by
      #2

      I think your real slowdown comes from having a timer in each individual missiles. They keep ticking even after they've left the screen. And you never stop the timer and unsubscribe the tick event in each missile. This keeps the timers and missiles all alive all the time. I've tweaked your code some and removed those separate timers and just update all the missiles from the Form's timer. Also, I remove the missiles which have gone off-screen so those won't be updated after they're gone . This should allow for more missiles and better perf/memory usage. There are clearly other improvements you could do such as having the logic to determine whether a missile is off-screen determined by the missile class. I hope this helps you get going... John

      using namespace System;

      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;

      using namespace System::Collections::Generic;

      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;

      	missile( Form ^ form ) // Class Constructor.			
      	{				
      		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);				
      	}		
      	
      	void update()
      	{
      		Box1->Top--;
      	}
      

      };

      public ref class Form1 : public System::Windows::Forms::Form
      {
      public:
      PictureBox^ Box2;
      object^ o1;
      Timer^ Timer3;
      bool x;
      List^ Missiles;

      Form1() // Class constructor.			
      {		
      	Missiles = gcnew List();
      
      	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(t
      
      J C 2 Replies Last reply
      0
      • J John Schroedl

        I think your real slowdown comes from having a timer in each individual missiles. They keep ticking even after they've left the screen. And you never stop the timer and unsubscribe the tick event in each missile. This keeps the timers and missiles all alive all the time. I've tweaked your code some and removed those separate timers and just update all the missiles from the Form's timer. Also, I remove the missiles which have gone off-screen so those won't be updated after they're gone . This should allow for more missiles and better perf/memory usage. There are clearly other improvements you could do such as having the logic to determine whether a missile is off-screen determined by the missile class. I hope this helps you get going... John

        using namespace System;

        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;

        using namespace System::Collections::Generic;

        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;

        	missile( Form ^ form ) // Class Constructor.			
        	{				
        		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);				
        	}		
        	
        	void update()
        	{
        		Box1->Top--;
        	}
        

        };

        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
        PictureBox^ Box2;
        object^ o1;
        Timer^ Timer3;
        bool x;
        List^ Missiles;

        Form1() // Class constructor.			
        {		
        	Missiles = gcnew List();
        
        	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(t
        
        J Offline
        J Offline
        John Schroedl
        wrote on last edited by
        #3

        Also, if you want to detect a collision, change the logic in the timer tick:

        	System::Void timer3\_Tick(System::Object^  sender, System::EventArgs^  e)			
        	{		
        		for each (missile^ m in Missiles)
        		{
        			m->update();
        
        			if(x==true && o1 != nullptr)				
        			{			
        				if (o1->Box3->Bounds.IntersectsWith(m->Box1->Bounds))
        				{
        					m->Box1->BackColor = Color::Green;
        				}				
        			}	
        		}
        
        		Missiles->RemoveAll(gcnew Predicate(this, &Form1::IsMissileOffScreen));
        	}			
        

        Other advice: It would be courteous to let us know that you've posted about this in the C++ forum as well to prevent duplication of efforts. John

        C 1 Reply Last reply
        0
        • J John Schroedl

          Also, if you want to detect a collision, change the logic in the timer tick:

          	System::Void timer3\_Tick(System::Object^  sender, System::EventArgs^  e)			
          	{		
          		for each (missile^ m in Missiles)
          		{
          			m->update();
          
          			if(x==true && o1 != nullptr)				
          			{			
          				if (o1->Box3->Bounds.IntersectsWith(m->Box1->Bounds))
          				{
          					m->Box1->BackColor = Color::Green;
          				}				
          			}	
          		}
          
          		Missiles->RemoveAll(gcnew Predicate(this, &Form1::IsMissileOffScreen));
          	}			
          

          Other advice: It would be courteous to let us know that you've posted about this in the C++ forum as well to prevent duplication of efforts. John

          C Offline
          C Offline
          Cyclone_S
          wrote on last edited by
          #4

          Thanks for the help. how would I go about deleting m1,m2 in my original code? Not sure how to delete the m1,m2 objects. I currently have something like this.

          if(e->KeyCode == Keys::Up) // Check if m1,m2 objects exist or not. If they don't then create them.
          {
          mcount += 1;
          l1->Text = mcount.ToString();
          if(mcount == 1 && !m1){mcount1 = true; m1 = gcnew missile(this);m1->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;}
          if(mcount == 2 && !m2){mcount=0;mcount2 = true;m2 = gcnew missile(this);m2->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;}
          if(mcount == 2){mcount=0;}

          L 1 Reply Last reply
          0
          • C Cyclone_S

            Thanks for the help. how would I go about deleting m1,m2 in my original code? Not sure how to delete the m1,m2 objects. I currently have something like this.

            if(e->KeyCode == Keys::Up) // Check if m1,m2 objects exist or not. If they don't then create them.
            {
            mcount += 1;
            l1->Text = mcount.ToString();
            if(mcount == 1 && !m1){mcount1 = true; m1 = gcnew missile(this);m1->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;}
            if(mcount == 2 && !m2){mcount=0;mcount2 = true;m2 = gcnew missile(this);m2->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;}
            if(mcount == 2){mcount=0;}

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            The easiest way is to keep only real missiles in your collection, which means: - the collection starts off empty; - when you create a missile, add it to the collection; - when you destroy a missile, remove it from the collection. Avoid overall variables (such as mcount in your example), as they typically are redundant and hence just can cause bugs and inconsistencies. :)

            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.

            C 1 Reply Last reply
            0
            • L Luc Pattyn

              The easiest way is to keep only real missiles in your collection, which means: - the collection starts off empty; - when you create a missile, add it to the collection; - when you destroy a missile, remove it from the collection. Avoid overall variables (such as mcount in your example), as they typically are redundant and hence just can cause bugs and inconsistencies. :)

              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.

              C Offline
              C Offline
              Cyclone_S
              wrote on last edited by
              #6

              Thanks. John a have a few questions about your code. 1.

              Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));

              How exactly does this work. 2. What does the ^ character do. Thanks again.

              L 1 Reply Last reply
              0
              • C Cyclone_S

                Thanks. John a have a few questions about your code. 1.

                Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));

                How exactly does this work. 2. What does the ^ character do. Thanks again.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Oops. You replied to me, not to John. I suggest you try again. :)

                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.

                1 Reply Last reply
                0
                • J John Schroedl

                  I think your real slowdown comes from having a timer in each individual missiles. They keep ticking even after they've left the screen. And you never stop the timer and unsubscribe the tick event in each missile. This keeps the timers and missiles all alive all the time. I've tweaked your code some and removed those separate timers and just update all the missiles from the Form's timer. Also, I remove the missiles which have gone off-screen so those won't be updated after they're gone . This should allow for more missiles and better perf/memory usage. There are clearly other improvements you could do such as having the logic to determine whether a missile is off-screen determined by the missile class. I hope this helps you get going... John

                  using namespace System;

                  using namespace System::ComponentModel;
                  using namespace System::Collections;
                  using namespace System::Windows::Forms;
                  using namespace System::Data;
                  using namespace System::Drawing;

                  using namespace System::Collections::Generic;

                  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;

                  	missile( Form ^ form ) // Class Constructor.			
                  	{				
                  		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);				
                  	}		
                  	
                  	void update()
                  	{
                  		Box1->Top--;
                  	}
                  

                  };

                  public ref class Form1 : public System::Windows::Forms::Form
                  {
                  public:
                  PictureBox^ Box2;
                  object^ o1;
                  Timer^ Timer3;
                  bool x;
                  List^ Missiles;

                  Form1() // Class constructor.			
                  {		
                  	Missiles = gcnew List();
                  
                  	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(t
                  
                  C Offline
                  C Offline
                  Cyclone_S
                  wrote on last edited by
                  #8

                  Thanks. John a have a few questions about your code. 1. Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));How exactly does this work. 2. What does the ^ character do. Thanks again.

                  J 1 Reply Last reply
                  0
                  • C Cyclone_S

                    Thanks. John a have a few questions about your code. 1. Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));How exactly does this work. 2. What does the ^ character do. Thanks again.

                    J Offline
                    J Offline
                    John Schroedl
                    wrote on last edited by
                    #9

                    Let's start with #2: The ^ character indicates a handle which is a reference to a managed object on the CLI heap. The Wikipedia article isn't too good at explaining in my opinion: http://en.wikipedia.org/wiki/C%2B%2B/CLI#Handles[^] but you may want to look at it anyway. This intro may be a bit friendlier: http://www.functionx.com/cppcli/handles/Lesson06c.htm[^] For #1: I'm creating a Predicate which is really just a function which returns True or False based on my own rules. Often predicates are used for sorting. So my predicate function (IsMissileOffScreen) returns True if I see the missile has left the screen. In RemoveAll, my predicate is called and passed one missile at a time as a parameter (the type of the parameter is missile^ which I indicate in the < > arguments. If I return True from IsMissileOffScreen, the RemoveAll() call will remove the missile from the Missiles list. So after this line, we're left with only on-screen missiles in the Missiles list. Make sense? John

                    C 2 Replies Last reply
                    0
                    • J John Schroedl

                      Let's start with #2: The ^ character indicates a handle which is a reference to a managed object on the CLI heap. The Wikipedia article isn't too good at explaining in my opinion: http://en.wikipedia.org/wiki/C%2B%2B/CLI#Handles[^] but you may want to look at it anyway. This intro may be a bit friendlier: http://www.functionx.com/cppcli/handles/Lesson06c.htm[^] For #1: I'm creating a Predicate which is really just a function which returns True or False based on my own rules. Often predicates are used for sorting. So my predicate function (IsMissileOffScreen) returns True if I see the missile has left the screen. In RemoveAll, my predicate is called and passed one missile at a time as a parameter (the type of the parameter is missile^ which I indicate in the < > arguments. If I return True from IsMissileOffScreen, the RemoveAll() call will remove the missile from the Missiles list. So after this line, we're left with only on-screen missiles in the Missiles list. Make sense? John

                      C Offline
                      C Offline
                      Cyclone_S
                      wrote on last edited by
                      #10

                      Yea it mostly makes sense. I apreciate the help.

                      1 Reply Last reply
                      0
                      • J John Schroedl

                        Let's start with #2: The ^ character indicates a handle which is a reference to a managed object on the CLI heap. The Wikipedia article isn't too good at explaining in my opinion: http://en.wikipedia.org/wiki/C%2B%2B/CLI#Handles[^] but you may want to look at it anyway. This intro may be a bit friendlier: http://www.functionx.com/cppcli/handles/Lesson06c.htm[^] For #1: I'm creating a Predicate which is really just a function which returns True or False based on my own rules. Often predicates are used for sorting. So my predicate function (IsMissileOffScreen) returns True if I see the missile has left the screen. In RemoveAll, my predicate is called and passed one missile at a time as a parameter (the type of the parameter is missile^ which I indicate in the < > arguments. If I return True from IsMissileOffScreen, the RemoveAll() call will remove the missile from the Missiles list. So after this line, we're left with only on-screen missiles in the Missiles list. Make sense? John

                        C Offline
                        C Offline
                        Cyclone_S
                        wrote on last edited by
                        #11

                        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::Ob
                        
                        L 1 Reply Last reply
                        0
                        • C Cyclone_S

                          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::Ob
                          
                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          why would each missile need four timers? you can have multiple statements in a single Tick handler, and you can skip part of the handler if not all code needs the same period, as in (C# code again):

                          int tick=0;

                          public void TickHandler(object sender, EventArgs e) {
                          tick++;
                          doSomethingEachTime();
                          if (tick%10==0) doSomethingEveryTenthTime();
                          }

                          And I already told you an interval of 1 msec does not really exist for the timers you are using, see the article I provided a link to. :)

                          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.

                          J 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            why would each missile need four timers? you can have multiple statements in a single Tick handler, and you can skip part of the handler if not all code needs the same period, as in (C# code again):

                            int tick=0;

                            public void TickHandler(object sender, EventArgs e) {
                            tick++;
                            doSomethingEachTime();
                            if (tick%10==0) doSomethingEveryTenthTime();
                            }

                            And I already told you an interval of 1 msec does not really exist for the timers you are using, see the article I provided a link to. :)

                            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.

                            J Offline
                            J Offline
                            John Schroedl
                            wrote on last edited by
                            #13

                            Luc is correct, the missile really should not maintain four timers. As for why you cannot seem to delete the object, there are a few things here: When you detect the collision, you need to remove o1->Box3 from the form children so that it disappears from the screen. After that, simply set o1 = null; It's probably smart to make a function in the object class like explode() which does the removal of Box3. Also, you have lots of code which assumes o1 is non-null. You need to check o1 for null before using it. And one more suggestion is to rename your class from "object" to something like "target" since it's easy to confuse with System::Object. John

                            C 2 Replies Last reply
                            0
                            • J John Schroedl

                              Luc is correct, the missile really should not maintain four timers. As for why you cannot seem to delete the object, there are a few things here: When you detect the collision, you need to remove o1->Box3 from the form children so that it disappears from the screen. After that, simply set o1 = null; It's probably smart to make a function in the object class like explode() which does the removal of Box3. Also, you have lots of code which assumes o1 is non-null. You need to check o1 for null before using it. And one more suggestion is to rename your class from "object" to something like "target" since it's easy to confuse with System::Object. John

                              C Offline
                              C Offline
                              Cyclone_S
                              wrote on last edited by
                              #14

                              I agree with the feed back. The code would benift with a rewrite. The game is finished now but the next program I will try to pay more attention while coding. Thanks for the help.

                              1 Reply Last reply
                              0
                              • J John Schroedl

                                Luc is correct, the missile really should not maintain four timers. As for why you cannot seem to delete the object, there are a few things here: When you detect the collision, you need to remove o1->Box3 from the form children so that it disappears from the screen. After that, simply set o1 = null; It's probably smart to make a function in the object class like explode() which does the removal of Box3. Also, you have lots of code which assumes o1 is non-null. You need to check o1 for null before using it. And one more suggestion is to rename your class from "object" to something like "target" since it's easy to confuse with System::Object. John

                                C Offline
                                C Offline
                                Cyclone_S
                                wrote on last edited by
                                #15

                                Thanks for the suggestions. The "o1 = NULL;" didn't work but I found a work around.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups