Table of Contents | Previous Page | Next PageCopyright Don Sleeth, 1998 ©
![]()
Page 10 - Debugging Jump
Load in your work
- From the file menu, open your project named Page9. Then from the file menu select "Save Project As..." and save your project as Page10
In the procedures page, your program should look like this:
to Jump Height local "OriginalHeading make "OriginalHeading heading seth 0 forward thing "Height wait 1 back thing "Height seth thing "OriginalHeading end ==================== to Main cc talkto "t1 setsh [dog1 dog2] seth 270 repeat 200 [Walk 10 10 Jump 10] end ==================== to Square Length repeat 4 [forward thing "Length right 90 wait 1] end to Start Main end to Walk StepSize QSteps repeat thing "QSteps [forward thing "StepSize wait 1] endAdd a button
- We now have three movement procedures, but we are not really using the Square procedure at this point. Our Jump procedure seems to work very well when we call it from Main. However I want an event in the program to trigger the character to Jump, so to simulate that, let's add a Button and program it so that a click on the button makes the turtle/dog Jump.
Go to the program page. In the Tool Box click on the Button and then drag on the page to make the button.
A dialog box pops up giving us the opportunity to type in the instruction that we want to run when the button is clicked. This is a problem for us because I want to pass arguments to Jump but I sure don't want to hide those arguments out here in the dialog box. Also, the name of the instruction becomes the label on the button. Well, I want the label to be Jump, so type in Jump as the name of the instruction.
If you need to resize the button, drag an area on the program page that includes all of the button, then release the mouse button. The button should then have four handles on it which allow you to resize it.
Drag the Jump button down beside the start button. Try clicking on the Jump button. Here is what my screen looks like:
![]()
As you can see, we get an error message, because our Jump procedure requires an input. I really don't want to have to go to the dialog box of the Jump button to change the arguments for Jump everytime I want to try something different so we have no choice but to change the name of our procedure. Also we want to change Main so that it no longer calls Jump. Make your procedures page look like this:
to DoJump Height local "OriginalHeading make "OriginalHeading heading seth 0 forward thing "Height wait 1 back thing "Height seth thing "OriginalHeading end to Main cc talkto "t1 setshape [dog1 dog2] seth 270 repeat 200 [Walk 10 10] end to Start Main end to Walk StepSize QSteps repeat thing "QSteps [forward thing "StepSize wait 1] endYou can see that I changed the name of our Jump procedure to DoJump and I also removed the call to Jump from Main.
Try your program by clicking on the Start button. The Dog walks (actually runs but we'll call it walk) across the screen. Click on the Jump button. You get an "I don't know how to Jump" error message, because there is no longer a Jump procedure, we renamed it to DoJump.
Processes
- There is one other thing to notice. Even though you get an error message, the little dog keeps on walking! We get the error message because when we click on the button we call the procedure Jump which does not exist. Doesn't that strike you as a bit strange that the dog keeps walking? We have an error, but the program keeps running! Let's try a little experiment. I'll call this Version 1 of the program.
Let's put the same error in Main and see what happens. Stop your dog and then edit Main as follows:
to Main cc talkto "t1 setshape [dog1 dog2] seth 270 repeat 200 [Walk 10 10 Jump 30] endWe will call that Version 2 of the program. Now, after the dog Walks 10 steps, the program will call Jump, which does not exist. On your program page, Click on Start. Sure enough, you get the error message but this time the Dog stops. What is going on here?The explanation is that the Logo language has "Processes". Each process is independent of another process. A process is almost like its own little program. When we click on a button we start a Process. If we had more than one turtle moving along on the screen, each one would be running in its own process. In Version 1 of the program clicking on the Start button started a process which had no errors and ran fine. Clicking on the Jump button started another process which did have an error and would not run but did not affect the first process.
In Version 2, we put an error in the process that is started by the Start button and sure enough, the error stopped the process!
This was just to give you a brief idea about processes. Let's go back to Version 1, take the call to Jump out of Main.
Jump has a bug!
- We need a procedure Jump which will be called when the button is clicked. Add this procedure to your procedure page:
to Jump DoJump 20 endNow try your program. Well, it is fun, you can make the Dog Walk along and then by clicking on Jump, make him take a little jump and then carry on. This way we get to have the label we want on the Button and we get to keep the program out where we can see it in the Procedures Page.Did you notice something odd? Did you notice that the Dog kept getting higher and higher? It didn't seem to come all the way back down after a Jump.
Well, folks, we have a bug in our program. Something is not right and we need to debug our program, that is, we need to find what the error is. To debug a program, the first step is to find out exactly what is causing the problem.
Let's try to narrow it down. When the turtle just Walks, it does not move higher.
Try stopping the Walk and just clicking on the Jump button. Jump seems to work just fine and Walk seems to work just fine when they are run separately but when both processes are running at the same time, we seem to have a bug in our program!
So, it only occurs when both processes are running at the same time. I would like to see the movement without the shape of the Dog obscurring the turtle. In Main, put a semicolon in front of the setshape line, like this:
to Main cc talkto "t1 ; setshape [dog1 dog2] seth 270 repeat 200 [Walk 10 10 ] endA line with a semicolon on it is a Comment and is not part of the program. It is like all the words to the right of the semi-colon are invisible to Logo and just visible to humans. On your program page go to the shape center and click on the little black turtle and then on the Dog on the Program page. This turns the dog back into a turtle.Now run the program. Seeing it this way, with the naked Turtle, makes me a little suspicious of what is happening with forward.
Let's take a hard look at our two procedures and try to figure out what happens that is different, when both procedures are running at the same time.
You can see that both Walk and DoJump tell the turtle to move forward. forward just means "go in the direction your head is pointing". So, if the DoJump process is running, then the head is pointed up (seth 0). So during the time the DoJump process is running, the Walk process actually calls forward and increases the height of the jump! That is why the turtle edges a little higher each time, I THINK! We need to test this theory.
Looking at DoJump, we see that the turtles head is pointing up right from the line seth 0 until the line seth thing "OriginalHeading . If the turtle spent less time pointing up, would the amount the turtle kept climbing decrease? In other words if the turtle spent less time with its head pointing up, and the climbing problem decreased, then that would confirm our suspicion that the problem is Walk calling forward, while DoJump has the turtles head pointing up.
Let's decrease the amount of time by getting rid of the wait 1 line. Comment out that line as shown below.
to DoJump Height local "OriginalHeading make "OriginalHeading heading seth 0 forward thing "Height ; wait 1 back thing "Height seth thing "OriginalHeading endGet the turtle walking and then click Jump several times. On my system, that eliminates the "climbing up" problem! Unfortunately you can barely see the turtle Jump, but we know that it is. Another way to test is to change wait 1 to wait 3 and see if it makes the problem worse. Try it.Now we understand that the problem is that while the turtle is pointing up in DoJump, Walk makes it go higher. But how are we going to fix it?
It would be nice if we could eliminate the use of forward in the procedure DoJump. It would be nice if we could make it jump without turning its head at all, then Walk could make it go forward at the same time without any bad effect. As hackers, oops, I mean computer programers, our next step is to check the Vocabulary to see if there are any other words that we could try.
Using ycor
- Way down at the bottom of the Index is xcor and ycor. ycor looks like a very likely candidate, so read the help screen, which is reproduced below:
![]()
This tells us that there is a built-in procedure ycor that has as an output the y coordinate (vertical coordinate) of the turtle and another procedure sety which takes a number as an input and presumably sets the y coordinate.
Let's try replacing the use of forward with ycor in our procedure DoJump. Because we are developing procedures, it is quite common that you would want to re-write a procedure on a trial basis, but you don't want to lose the old one, at least not yet. Here is what I do. I rename the existing procedure to, say, DoJumpOld and write the new procedure as DoJump. That way you do not have to search through your program to change all the calls to DoJump. They stay as they are, only now they are calling your new version of DoJump.
Here is my old and my new DoJump. You really should try to write your own before looking at mine.
to DoJumpOld Height local "OriginalHeading make "OriginalHeading heading seth 0 forward thing "Height ; wait 1 back thing "Height seth thing "OriginalHeading end to DoJump Height local "OriginalY make "OriginalY ycor sety sum ycor thing "Height wait 1 sety thing "OriginalY endI like it! I like our new version of DoJump and it has fixed the "Climbing Up" problem. Time to tidy up our program and save it. Here is my version of Page10:to DoJump Height local "OriginalY make "OriginalY ycor sety sum ycor thing "Height wait 1 sety thing "OriginalY end to Jump DoJump 30 end ========================== to Main cc talkto "t1 setshape [dog1 dog2] seth 270 repeat 200 [Walk 10 10] end ========================= to Start Main end to Walk StepSize QSteps repeat thing "QSteps [forward thing "StepSize wait 1] end