Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, February 8, 2007 8:25 AM
Hi, I've encountered a weird problem.
I'm working on a small game, using C# and VS2005. You're supposed to move a character (the "hero", its basically just an image drawn on the form) around the form to pick up an item; another image drawn on the form. When the item has been picked up it moves to another location (you move up a level).
When the item's location is hardcoded or moved using
int x = 50;
int y = 50;
...
x += 10; //when item's found, move it
y += 10;
item.Location = new Point(x, y);
everything works fine, when the character image "touches" the item, it is found.
The problem is when I try to set a new location to the item using Random
Random rand = new Random();
int x, y;
...
x = rand.Next(ClientSize.Width); // The Form's ClientSize
y = rand.Next(ClientSize.Height);
item.Location = new Point(x, y);
The character just moves over it, it seems like they never collide.
Does anyone have any idea why it behaves like that? Why does it work hardcoded but not randomized?
Thanks in advance!
Yodine, the rookie
All replies (5)
Thursday, February 8, 2007 4:37 PM ✅Answered
I know this has nothing to do with your current problem but
x = rand.Next(ClientSize.Width);
y = rand.Next(ClientSize.Height);
With this there is the posibility that the item be outside of the screen
x = rand.Next(ClientSize.Width - item.Width);
y = rand.Next(ClientSize.Height - item.Height);
And make sure that the new random location is not on the player sice this will be an easy lvl up
while(Item Is On Player)
{
x = rand.Next(ClientSize.Width - item.Width);
y = rand.Next(ClientSize.Height - item.Height);
}
I don't know what you use for your collision detection but here is an easy way with if statment
if(player.x < item.x + item.width && player.x + player.width > item.x)
if(player.y < item.y + item.height && player.y + player.height > item.y)
There is a hit
Thursday, February 8, 2007 11:13 PM ✅Answered
You can use IntersectsWith and bounds properties of the picturebox control to determine if a collision has occurred
if (pbxBall.Bounds.IntersectsWith(pbxInnerTarget.Bounds))
{// hit on inner target
Console.WriteLine(" Hit on inner target");
}// end hit on inner target
if (pbxBall.Bounds.IntersectsWith(pbxTarget.Bounds))
{// hit on target
Console.WriteLine(" Hit on target");
{//end hit on target
IntersectsWith occures when two object touch
Bounds is the outer edge of the related object
.Top (top of object)
.Bottom(bottom of object)
.Left(left of object)
.Right (right of object)
Thursday, February 8, 2007 3:21 PM
My guess the problem is width the algorithm to determine if the character has touched the item. From your example, when the item's location is hardcoded, it's always on a multiple of 10. This would not be the case when it's moved randomly.
Thursday, February 8, 2007 3:36 PM
Hi,
can you post the code showing how you determine how the two objects are hit?
Mark
Monday, February 12, 2007 8:11 AM
Thank you all for all your kind answers, I really appreciated them :)
I've managed to get it working quite properly thanks to the tips
//Yodine