|
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?
|
|
|
|
|
You cannot return more than one item. Get the caller to send you the a pointer (to a HWND) where you can store the handle; something like:
HWND hTextBox;
HWND hWnd = StartWindow("Title", "Text", WM_STYLES, &hTextBox);
and then your function would do
__declspec(dllexport) HWND _stdcall StartWindow(char *WindowTitle, char *WindowText, int Style, HWND* pHwnd)
{
*pHwnd = textboxHandle;
I must get a clever new signature for 2011.
|
|
|
|
|
I create a Window with a ProgressBar (Marquee Style) but it doesnt work, id doesnt show anything!
here is my code:
ProgressBarWindow ^form = gcnew ProgressBarWindow();
form->textBox1->Text = TextString;
form->Text = TitleString;
form->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Marquee;
form->Show();
The textbox and the title are filled correctly in the form!
modified on Wednesday, March 16, 2011 8:37 AM
|
|
|
|
|
there must be a lot more code related to the PrograssBar. It needs some properties set including Size and Location (or Bounds), it needs to be added to the Form's Controls property, etc. You didn't show these (if added through Visual Designer, they would be in a separate file, where textBox1 initialization would be too), so I can't tell what is wrong.
FWIW: MSDN on ProgressBar holds a remark "The Marquee style is honored only when visual styles are enabled. The Continuous style is honored when visual styles are not enabled." So check your app's Main() method.
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.
|
|
|
|
|
i forgot "Application::EnableVisualStyles();" in my main....
thx for your help 
|
|
|
|
|
I am writing a code in visual studio 2008 c++.I have to use linear feedback shift register,so i have downloaded the library from
http://lfsr-generator.sourceforge.net.
On the web procedure is mentioned for installing it, that contains some commands like given below.
$ ./configure
$ make
# make install
I have written these command in cmd,but i didn't works there.Please tell me how I can install it,or how i can use this library in my code.
Thanks in advance
Ishtiaq
|
|
|
|
|
I think you may find that this project has been designed for UNIX only. You will probably need something like cygwin[^] to run it on a Windows system.
I must get a clever new signature for 2011.
|
|
|
|
|
Thanks for the reply. I think cygwin would be the right option for me. Could you please tell me how to use cygwin? Or if you know of any online material that would help me get going on using it.
Thanks.
|
|
|
|
|
smishtiaqhussain wrote: Could you please tell me how to use cygwin? Or if you know of any online material that would help me get going on using it.
I gave you the link to the cygwin site in my previous answer, now it's up to you to go and learn about it.
I must get a clever new signature for 2011.
|
|
|
|
|
why would you go and suffer all the trouble, when a shift register just takes a few lines of code anyway? write a little class that does what you need and add it to your current project. No third party stuff, no separate project, no hassle. Or am I missing something here?
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 Pattyn wrote: why would you go and suffer all the trouble, when a shift register just takes a few lines of code anyway?
Because it isn't just a register shift.
|
|
|
|
|
Hi all.
I have a strange problem. I have tryed local .NET forums and MSDN with no luck but I have faith in you guys
Anyway, her's the situation:
We have a Win32 application written in Clarion for Windows. This dev tool produces standard Win32 binary similar to C++. The Clarion Compiler is actually a C++ compiler as well as compiling Clarion code so the result is the same
We allso have a large .NET library we want to utilize in this Clarion software. But since Clarion is not .NET compatible or is able to compile managed code, I had to figure out what to do. The answer was quiet simple actually. C++/CLI was what I was looking for. I then created a small C++ DLL that wrapped my .NET library. This works perfectly and everyone is smiling......until we installed the software on our Windows 2003 Terminal server..... DANG!! Error 0XC0000005!! The Error shows at loading time, before any code is started at all. And of colurse, both FW4 and VC100 is installed...
I have searched the web for hours and hours looking for a solution. Nothing
My postings on local forums and MSDN all results in answers like
"You have a bug"
"Run the debugger and check where it stops"
etc. etc.
Noone seems to care of my initial posting that clearly tells that the error shows at startup/loading of the DLL.
I actually gave up this and changed my .NET application into an EXE file, calling it from Clarion and passing parameters to reach stuff. A silly and lousy workaround, but at least, the program does what it is supposed to do. But I thought, what the heck... I'l give CodeProject a go. Maybe someone here have seen this some time...
-----------------------------
Speaking nordic language?
Why not visiting irc.c-c.no and join #C# or #VisualBasic
-----------------------------
|
|
|
|