#### Archived

This topic is now archived and is closed to further replies.

# What's wrong with this code?!!

This topic is 6193 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This code is suppose to stop me from going through walls when I press the up key. But it doesn''t , can anyone help? Thanks Sub CheckColC() For a = (0) To (1) If Man.x + Dude.Width >= Wallv(a).Left And Man.x <= Wallv(a).Left + Wallv(a).Width And Man.y + Dude.Height >= Wallv(a).Top And Man.y <= Wallv(a).Top + Wallv(a).Height Then Man.y = Man.y + 200 Next End Sub

##### Share on other sites
What does ''a'' represent? Why is the subroutine called CheckColC? Why does it move the Man 200 in the y direction if true, and is it supposed to be able to do that twice? If you explained these points, I would understand what your code does exactly, and perhaps I could help you a little more.

##### Share on other sites
I think I understand. But why are you adding 200 to Man.y? Depending on which direction the player or character is walking, you should just push it back to the edge of the wall that he colided with. Like if the man was walking forward, you should set ''Man.y'' to ''Wallv(a).top + Wallv(a).height'' if the condition is true.

##### Share on other sites
Oh, now I understand, this was only for the up key (I missed that when I first read the post).

Your code looks like it should mostly be working. What exactly happens when you walk forward into a wall? Does it do anything at all?

##### Share on other sites
Well it looks like you''re checking if the man''s position is within the wall boundaries, ie left to right and top to bottom and if so, you''re letting him move. Maybe try:

If Man.x + Dude.Width >= Wallv(a).Left And Man.x <= Wallv(a).Left + Wallv(a).Width And Man.y + Dude.Height >= Wallv(a).Top And Man.y <= Wallv(a).Top + Wallv(a).Height Then
Man.y = Man.y
Else
Man.y = Man.y + 200
Endif

I could be wrong though.

##### Share on other sites
I, Pactuul, vow on this day, that I will never help anyone that writes code like this.

Seriously, I find that people can''t write code neatly. EVERY problem I''ve seen is hardly a syntax error, but a logical error. it''s very hard to debug logic errors like this win you use one or two letter variables and write everything one line. I should be able to understand what your doing in your code with little effort on my part.

1) You assume that we will understand your specific problem and that the answer will be a specific answer.

2) You assume we can understand your variables with ease.

3) You just assumed....

this my sound like critizism (and it is) but to help you and us, you have to be more clear.

##### Share on other sites
I totally agree with Pactuul, however I''ll give it a shot anyways cause I''m just a hell-of-a nice guy:

#1 if this is Visual Basic code where is your Next identifier? I see a Next... but no identifier... What the heck do you need a For loop for anyways?

#2 do not use implicitly declared variables!!!! Ever hear of Option Explicit ? use it!

#3 Where is this subroutine defined and why are all the variables used global? Passing your variables will help in the long run, trust me (even if they are all ByRef).

#4 Wouldn''t hurt to comment your code you know... line seperators might help too when the lines of code get too long (as in your case).

#5 Now onto the actual code, looks as if you have a little mess of AND statements there. I''ll try to muck through it, but without a definition of your Wallv structure I have no real way to determine exactly what is supposed to be going on.

#6 Okay I just spent 30min trying to figure out what the heck you were trying to do... I''m really at a loss without a solid definition of what Wallv is. You definitly have a logic error in that mess of ANDs, but it''s hard to say where... you really should look into a better way to check world object collisions, possible with a boolean return function (rather than a subroutine). Is your game tile based? If so you might want to try the four corner method?

##### Share on other sites
I think it is obvious what that code is supposed to do. WallV is obviously some Type or class that defines a wall and there are obviously only 2 walls. I''m not sure why the code wouldn''t work. It looks perfectly logical. Make sure that it is being called properly because there doesn''t seem to be anything wrong with the code itself.

##### Share on other sites
quote:
Original post by shalom
I think it is obvious what that code is supposed to do. WallV is obviously some Type or class that defines a wall and there are obviously only 2 walls. I'm not sure why the code wouldn't work. It looks perfectly logical. Make sure that it is being called properly because there doesn't seem to be anything wrong with the code itself.

No offense but it is not "obvious" what the code is supposed to be doing. The best we can do is assume what it is doing, which is no better how Rad76 assumed we'd all be able to fix his code without the necessary information. He could very well have set up his Wallv structure inccorrectly (there might not even be anything in it for all we know!), or it could be his player's X,Y coordinates are offset to that structure making his check invalid (If he is using FastGraph as his graphics library then the X,Y coordinate system does not use the upper left corner it uses the lower left!), or it just might be the fact he is logic is flawed. Here is as much as I could infer about his code in plain english:

IF

the players rightmost X coordinate (i.e. his right side) is greater than or equal to the leftmost edge of the Wall

AND

the players leftmost X coordinate (i.e. his left side) is less than or equal to the rightmost edge of the Wall

AND

the players bottommost Y coordinate (i.e. his bottom side) is greater than or equal to the topmost edge of the Wall

AND

the players topmost Y coordinate (i.e. his top side) is less than or equal to the bottommost edge of the Wall

THEN

add 200 units to the player's current Y coordinate value.

Now... considering all the boolean operators are AND, every one of the above statements must be true in order for the player to be moved back. If even one is false the entire if statement will be false (maybe all those ANDs should be ORs?).

Let's stick with the ANDs though, and assume the X,Y coordinate system is utilizing the upper left corner. Take a look at the third statement. If the players bottom side is greater than or equal to the wall's top side? does that make sense? no... shouldn't it be if the player's top side is less than or equal to the wall's bottom side? For that matter add the next statment into the mix, if the player's bottom side is greater than the wall's top side AND the player's top side less than the wall's bottom side then you'd effectively be inside the wall. Depending on how high the wall tiles are compared to the player sprite this case may never happen, or if is does the player will continue to move upwards until it is completely within the tile. (velocity will then be the determining factor for whether the sprite appears to stop at the walls edge, or travels into it before being pushed back). Now add in the X coordinate checks and you'll notice you must be exactly within a tile for the entire set of statements to comeout true. What happens if you've spanned a couple tiles? Maybe that's what the for loop checks... but we'll really never know without a proper description of the structures involving in the process.

I would highly suggest to Rad76, to not only re-write, but to re-think his code. There are far better ways to do simple tile collision. First and foremost, thoroughly think out and solve the problem before implementing it in the code. It seems to me this may have been an on the fly attempt, which can only lead to more problematic situations.

{Programming is not the act of "coding", it's the act of logical problem solving... there's a difference}

Edited by - Xorcist on August 6, 2001 3:57:05 PM

##### Share on other sites
What''s wrong with this code?

Um ... it''s not commented, and most of those if conditionals could probably be recoded as functions. It''ll make the code clearer, and the additional function calls aren''t going ot break you ...

As for the question of why doesn''t it work, which I think is what you are trying to ascertain, refactor it and you will probably find the error

--

Games, Anime and more at GKWorld

Meet Bunny luv'' and the girls, all ready to perform on your desktop just for you (warning: adult content).

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631785
• Total Posts
3002348
×