Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello! I made a question an hour ago because I was getting problems to move a bitmap in C#. Now it moves, but it keeps getting off of the screen. Its position changes, but the limit should be the border of the window, but it isn't.

Here's my code

private void timerGameLoop_Tick(object sender, EventArgs e)
      {
          UpdateVar();
          this.Refresh();
      }
      private void UpdateVar()

      {

          cVarandas.Update(
         rnd.Next(this.Width - Resources.vhappy.Width, Resources.vhappy.Width ),
           rnd.Next(this.Height - Resources.vhappy.Height , this.Height)
                );


What I have tried:

I tried to change to (this.width - Resources.vhappy.Width /2) for example, but didn't work.

Sorry if I spelled something wrong, english isn't my first language.
Would appreciate some help, thank you!
Posted
Updated 28-Mar-20 9:46am
Comments
gggustafson 28-Mar-20 16:13pm    
I am not a "gamer" however I have experience moving items around on a monitor. In my situations, not necessarily yours, I take action on the main timer tick. When the tick occurs, I move whatever objects need to be moved. I then determine if any of the objects have "hit" the screen border. If so, I revise the position (by changing the x- and y-coordinates). After all border collisions have been fixed, I update the monitor display (through the Paint event handler).
Lousada SCP 28-Mar-20 16:58pm    
Hello! Thank you for your answer! And how do I change de x and y coordinates?
gggustafson 28-Mar-20 23:51pm    
I assume that you repaint your moving item directly. Thus, as you move an item, just before you make the move test if the rightmost part of the object is overlapping the right side of the monitor. If that's the case, change the sign of the x velocity. Likewise for the other three sides.

1 solution

Use the debugger and find out what numbers you are dealing with: what values are in this.Width, Resources.vhappy.Width, this.Height, and Resources.vhappy.Height while your code is running.
When you know that, you can tell how large the values being passed to your Update method, and then look at that to find out what it is doing with them.
We can't do that for you: we can't run your code - so the debugger is (as it always should be) your friend and your first port of call. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
gggustafson 28-Mar-20 16:19pm    
Please don't use the solution area for this kind of comment; rather use the comment section. That way the unanswered question is not removed from the category "Unanswered Questions". (Usually, I only look at unanswered questions.)
OriginalGriff 28-Mar-20 17:25pm    
Teaching him to use the debugger is the solution to this problem, and probably to the next dozen of his as well...
gggustafson 28-Mar-20 23:46pm    
I'm sorry, you misunderstood me. I am in 100% agreement with your position. The problem is where you post it. If you post your response in "Solutions", then you remove the question from the category "View Unanswered Questions". If however you post your response in the "Have a question or comment" category, the OP's question remains in the "View Unanswered Questions". This makes it easier for responders to reply to only unanswered questions. Regards
Lousada SCP 29-Mar-20 9:32am    
it actually helped me, solved my problem! Thank you
OriginalGriff 29-Mar-20 9:59am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900