Game Development Reference
In-Depth Information
In plain English, this statement is asking this question: is 210 less than or equal to 220? The answer is
yes, of course! The directives execute and move the cat an additional 15 pixels to its new y position of
225. The code is working perfectly!
“It's not a bug; it's a feature!” This phrase has been uttered by more than a few programmers in the
face of disgruntled clients who think a piece of software is buggy, even though code is working per-
fectly as written. Depending on the charm of the programmer, he or she might be able to convince
the client that the supposed bug is actually a good thing in some way and thus save a lot of time
In most of your game projects, at least your first few, you'll be your own client. So you need to ask
yourself whether you can accept the way the code is behaving, or whether you need to roll up your
sleeves and get down to the hard work of figuring out what's wrong, why it's wrong, and what you
have to do to fix it.
Let's think about the current problem a little more. Is it okay for the cat to move as far as it does?
After all, pixel-perfect precision isn't really important in this project, and if you click the button again,
the code continues to do its job and prevents the cat from moving any farther. You could shrug your
shoulders and move on.
But what if that weren't okay? It's an interesting problem to consider. Let's see whether you can
fine-tune the code a bit to prevent the cat from moving beyond the bounds you've set for it. There
are few ways to solve this, but I'll discuss the most interesting solution because it will really help your
future understanding of later chapters.
First, is it possible for the cat to actually reach a y position of 220? No, it isn't. Each button click moves
the cat 15 pixels at a time, so if it starts out at a y position of 120, it will “jump over” 220 on its way
from 210 to 225. The only way you can get it to stop at 220 is to actually force it back to that position
if it happens to go over (similar to a car that accidentally drives through a red light, screeches to a halt
in the middle of an intersection, and then sheepishly reverses back to the white line). That might work!
Maybe you just need a traffic cop to blow a whistle at the wayward code. You can actually program
a traffic cop with additional eb statements pretty easily. Let's try it:
1. Add and change the following code in bold to the kjQl>qppkj?he_g and kj@ksj>qppkj?he_g
event handlers. It's considerably different from the first version, so make sure to update it
carefully. Replace -., and .., with the values you've been using in your own program.
Search Nedrilad ::

Custom Search