|
I think you should simplify the collision function. I don't think you need two loops.
If you want to check whether the head is colliding with any of the other segments, then just do:
for (int ThisSegment = 0; ThisSegment < (size-1); ThisSegment ++ )
{
if ( ThisSegment == head ) continue;
if ( segments[ThisSegment]->panel->Left == segments[head]->panel->Left &&
segments[ThisSegment]->panel->Top == segments[head]->panel->Top )
{
Timer->Stop();
}
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks. Thats much simpler code then what I had.
|
|
|
|
|
Hi Richard. I was wondering if you would be able to help me with a bit of code. I posted it here. Thank You.
[Algorthm][^]
|
|
|
|
|
I had MFC dll which exposes some clases. I would like to load this dll dynamically.
hResult = LoadLibrary(_T("c:\test.dll));
if (0 != hResult)
{
_InitSomeDLLFun = (InitSomeDLL)::GetProcAddress(hResult, "InitSomeDLL");
// always Undefind value is returned by GetProcAddress
if(_InitSomeDLL)
{
_InitSomeDLLFun();
}
err = GetLastError();
}
Please advice
|
|
|
|
|
What error is returned? Have you checked that InitSomeDLL is exported by your DLL and is not a mangled C++ name?
I must get a clever new signature for 2011.
|
|
|
|
|
The LoadLibrary() call was sucessfull, but
GetProcAddress() always returns <undefind value="">
the dlls are unmanaged dlls.
Please advice
|
|
|
|
|
Very interesting, but you didn't answer my questions.
I must get a clever new signature for 2011.
|
|
|
|
|
GetLastError() after the GetProcAddress returnts error code: 127
|
|
|
|
|
Did you bother to look up error code 127 to see what it indicates? I guess not; see here[^] for more information. Now you know what's wrong you can probably make a start at fixing it.
I must get a clever new signature for 2011.
|
|
|
|
|
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 )
{
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 )
{
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()
{
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);
}
};
[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.
|
|
|
|
|
 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 )
{
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 )
{
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<missile^>^ Missiles;
Form1()
{
Missiles = gcnew List<missile^>();
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;
missile^ m = gcnew missile(this);
m->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;
Missiles->Add(m);
}
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); }
}
bool IsMissileOffScreen(missile^ m)
{
return m->Box1->Bottom < 0;
};
System::Void timer3_Tick(System::Object^ sender, System::EventArgs^ e)
{
for each (missile^ m in Missiles)
{
m->update();
if(x==true && o1 != nullptr)
{
if (m->Box1->Top == o1->Box3->Bottom)
{
m->Box1->BackColor = Color::Green;
}
}
}
Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));
}
bool isColliding(PictureBox^ box1, PictureBox^ box3)
{
Rectangle r2 = o1->Box3->Bounds;
for each (missile^ m in Missiles)
{
Rectangle r1 = m->Box1->Bounds;
if (r1.IntersectsWith(r2))
return true;
}
return false;
}
};
|
|
|
|
|
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<missile^>(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
|
|
|
|
|
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)
{
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;}
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
Yea it mostly makes sense. I apreciate the help.
|
|
|
|
|
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)
{
for each (missile^ m in Missiles)
{
if(IsColliding(m))
{
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){}
m->Timer1->Stop();
m->Timer2->Stop();
m->Timer3->Stop();
m->Timer4->Stop();
}
}
Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));
}
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
{
public:
PictureBox^ Box1;
Timer^ Timer1;
Timer^ Timer2;
Timer^ Timer3;
Timer^ Timer4;
missile( Form ^ form )
{
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::Object^ sender, System::EventArgs^ e)
{
Box1->Top--;
}
System::Void timer2_Tick(System::Object^ sender, System::EventArgs^ e)
{
Box1->Left++;
}
System::Void timer3_Tick(System::Object^ sender, System::EventArgs^ e)
{
Box1->Top++;
}
System::Void timer4_Tick(System::Object^ sender, System::EventArgs^ e)
{
Box1->Left--;
}
};
public ref class object
{
public:
PictureBox^ Box3;
object( Form ^ form )
{
Box3 = gcnew PictureBox();
Box3->Width = 100;
Box3->Height = 100;
Box3->Left = 145;
Box3->Top = 125;
Box3->BackColor = System::Drawing::Color::Red;
form->Controls->Add(Box3);
}
};
public ref class laser
{
public:
PictureBox^ Box4;
Timer^ Timer1;
laser( Form ^ form )
{
Timer1 = gcnew Timer();
Timer1->Interval = 1;
Timer1->Start();
Box4 = gcnew PictureBox();
Box4->Width = 8;
Box4->Height = 50;
Box4->Left = 145;
Box4->Top = 125;
Box4->BackColor = System::Drawing::Color::Red;
form->Controls->Add(Box4);
Timer1->Tick += gcnew System::EventHandler(this, &laser::timer1_Tick);
}
System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
Box4->Top++;
}
};
public ref class Form1 : public Form
{
public:
PictureBox^ Box2;
object^ o1;
Timer^ Timer1;
Timer^ Timer2;
List<missile^>^ Missiles;
List<laser^>^ Lasers;
bool collision;
bool tLeft;
bool tBottom;
bool tTop;
bool tRight;
int collision_counter;
Label^ Label1;
Form1()
{
Label1 = gcnew Label();
Label1->Text = "0";
tBottom = true;
Missiles = gcnew List<missile^>();
Lasers = gcnew List<laser^>();
Timer1 = gcnew Timer();
Timer1->Interval = 1;
Timer1->Start();
Timer2 = gcnew Timer();
Timer2->Interval = 1000;
Box2 = gcnew PictureBox();
Box2->BackColor = Color::Blue;
Box2->Top = 340;
Box2->Left = (this->Width / 2) + 15;
Box2->Width = 40;
Box2->Height = 10;
this->Controls->Add(Box2);
this->Controls->Add(Label1);
this->Width = 400;
this->Height = 400;
this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyDown);
Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
Timer2->Tick += gcnew System::EventHandler(this, &Form1::timer2_Tick);
}
System::Void Form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
if(o1 != nullptr)
{
if(e->KeyCode == Keys::Up)
{
missile^ m = gcnew missile(this);
if(tBottom == true)
{
m->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;
m->Timer1->Start();m->Timer2->Stop();m->Timer3->Stop();m->Timer4->Stop();
}
else if(tLeft == true)
{
m->Timer1->Stop(); m->Timer2->Start();m->Timer3->Stop();m->Timer4->Stop();
m->Box1->Left = Box2->Left + Box2->Width;
m->Box1->Top = Box2->Top + (Box2->Width / 2) + 10;
}
else if(tTop == true)
{
m->Timer1->Stop(); m->Timer2->Stop();m->Timer3->Start();m->Timer4->Stop();
m->Box1->Left = Box2->Left + (Box2->Width / 2) - 6;
m->Box1->Top = Box2->Bottom - 10;
}
else if(tRight == true)
{
m->Timer1->Stop(); m->Timer2->Stop();m->Timer3->Stop();m->Timer4->Start();
m->Box1->Left = Box2->Left;
m->Box1->Top = Box2->Bottom - 25;
}
Missiles->Add(m);
}
if(e->KeyCode == Keys::Left)
{
if(tBottom == true)
{
Box2->Left -= 4;
if(Box2->Left <=4)
{
Box2->Width = 10;Box2->Height = 40;
Box2->Top -= 30; Box2->Left += 1;
tBottom = false; tLeft = true;
}
}
if(tLeft == true)
{
Box2->Top -= 4;
if(Box2->Top <=4)
{
Box2->Width = 40;Box2->Height = 10;
tLeft = false; tTop = true;
}
}
if(tTop == true)
{
Box2->Left += 4;
if(Box2->Right >= 380)
{
Box2->Width = 10;Box2->Height = 40;
Box2->Left += 28; Box2->Top -= 4;
tTop = false; tRight = true;
}
}
if(tRight == true)
{
Box2->Top += 4;
if(Box2->Bottom >= 350)
{
Box2->Width = 40;Box2->Height = 10;
Box2->Left -= 31; Box2->Top += 30;
tRight = false; tBottom = true;
}
}
}
if(e->KeyCode == Keys::Right)
{
if(tBottom == true)
{
Box2->Left += 4;
if(Box2->Right >= 380)
{
Box2->Width = 10;Box2->Height = 40;
Box2->Top -= 26; Box2->Left += 28;
tBottom = false; tRight = true;
}
}
if(tRight == true)
{
Box2->Top -= 4;
if(Box2->Top <=4)
{
Box2->Left -= 26;
Box2->Width = 40;Box2->Height = 10;
tRight = false; tTop = true;
}
}
if(tTop == true)
{
Box2->Left -= 4;
if(Box2->Left <= 4)
{
Box2->Top -= 4;
Box2->Width = 10;Box2->Height = 38;
tTop = false; tLeft = true;
}
}
if(tLeft == true)
{
Box2->Top += 4;
if(Box2->Bottom >= 350)
{
Box2->Left -= 0; Box2->Top += 25;
Box2->Width = 40;Box2->Height = 10;
tLeft = false; tBottom = true;
}
}
}
}
if(e->KeyCode == Keys::Down) {o1 = gcnew object(this);Timer2->Start();}
}
bool IsMissileOffScreen(missile^ m)
{
return m->Box1->Bottom < 0;
}
bool IsColliding(missile^ m)
{
if((m->Box1->Right >= o1->Box3->Left && m->Box1->Right <= o1->Box3->Right) || (m->Box1->Left >= o1->Box3->Left && m->Box1->Left <= o1->Box3->Right))
{
if((m->Box1->Top >= o1->Box3->Top && m->Box1->Top <= o1->Box3->Bottom) || (m->Box1->Bottom >= o1->Box3->Top && m->Box1->Bottom <= o1->Box3->Bottom))
collision = true;
}
else if((m->Box1->Top >= o1->Box3->Top && m->Box1->Top <= o1->Box3->Bottom) || (m->Box1->Bottom >= o1->Box3->Top && m->Box1->Bottom <= o1->Box3->Bottom))
{
if((m->Box1->Right >= o1->Box3->Left && m->Box1->Right <= o1->Box3->Right) || (m->Box1->Left >= o1->Box3->Left && m->Box1->Left <= o1->Box3->Right))
collision = true;
}
return collision;
}
System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
for each (missile^ m in Missiles)
{
if(IsColliding(m))
{
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){}
m->Timer1->Stop();
m->Timer2->Stop();
m->Timer3->Stop();
m->Timer4->Stop();
}
}
Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));
}
System::Void timer2_Tick(System::Object^ sender, System::EventArgs^ e)
{
laser^ lD = gcnew laser(this);
lD->Box4->Top = o1->Box3->Bottom;
Lasers->Add(lD);
}
};
[STAThread]
int main()
{
Application::Run(gcnew Form1());
}
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
Thanks for the suggestions. The "o1 = NULL;" didn't work but I found a work around.
|
|
|
|
|
i wrote a dll with an external function:
__declspec(dllexport) HWND _stdcall StartWindow(char *WindowTitle, char *WindowText, int Style)
{
System::String ^ TextString = gcnew System::String(WindowText);
System::String ^ TitleString = gcnew System::String(WindowTitle);
Application::EnableVisualStyles();
ProgressBarWindow ^form = gcnew ProgressBarWindow();
form->textBox1->Text = TextString;
form->Text = TitleString;
if (Style == 1){
form->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Marquee;
}
else if (Style == 2){
form->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Continuous;
}
form->Show();
HWND WindowHndl = (HWND)form->Handle.ToPointer();
HWND textboxHandle = (HWND)form->textBox1->Handle.ToPointer();
return WindowHndl;
}
it returns the WindowHandle but i need to return the textboxHandle too!
how can i do that?
|
|
|
|