Game Development Reference
In-Depth Information
There are a few places in your game where you could add this code, but it might make the most sense
to add it in the _da_gC]iaKran method, outside of the eb statement.
Entering numbers with the Enter key
At the moment, you can enter numbers by clicking the Guess button. Wouldn't it be nice if you could
also enter them by hitting the Enter key? Of course it would!
Capturing key presses will be discussed in detail in the next chapter, but here's a sneak peek for the
adventurous among you.
It isn't too difficult, but there's one major modification you have to make to the structure of your
code. If you click the Guess button or press the Enter key, you'll want the same directives to run, so
the Enter key and Guess button both need to call the same method. Create a new method called
c]iaArajpo and have it contain all the directives that are currently in the kjCqaoo>qppkj?he_g event
handler. Then call the c]iaArajpo method from both kjCqaoo>qppkj?he_g and the new event handler
you create for your Guess button.
Up for a challenge? Try this modification and see whether you can make it work. (You can find the
complete final source code for this modification in the chapter's source files in a folder called Jqi^an
CqaooejcC]iaƍGau^k]n`.)
1. To capture key presses with your program, you need to import the Gau^k]n`Arajp and Gau^k]n`
classes into your program. Add the following two eilknp directives in bold to the top of your
program:
eilknpbh]od*`eolh]u*Ikrea?hel7
eilknpbh]od*arajpo*IkqoaArajp7
eilknpbh]od*arajpo*Gau^k]n`Arajp7
eilknpbh]od*qe*Gau^k]n`7
2. Add an event listener to the op]ca object that listens for key presses. Add the following code
in bold text to the ejep method:
bqj_pekjejep$%6rke`
w
++Ejepe]hevar]ne]^hao
op]npIaoo]ca9E]ipdejgejckb]jqi^an^apsaaj-]j`-,,7
iuopanuJqi^an9I]pd*_aeh$I]pd*n]j`ki$%&-,,%7
cqaooaoNai]ejejc9-,7
cqaooaoI]`a9,7
c]iaOp]pqo97
c]iaSkj9b]hoa7
++Pn]_apdaiuopanujqi^an
pn]_a$Pdaiuopanujqi^an6'iuopanuJqi^an%7
++Ejepe]hevapatpbeah`o
kqplqp*patp9op]npIaoo]ca7
ejlqp*patp97
ejlqp*^]_gcnkqj`?khkn9,tBB??????7
 
Search Nedrilad ::




Custom Search