Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Khi lớp học bắt đầu, nó tạo ra một bản sao của cụm từ bằng cách chạy nó thông qua chức năng thay thế bằng một biểu thức chính quy. Các biểu thức / [A-Za-z] / g phù hợp bất kỳ ký tự chữ cái (A đến Z và một z, trên toàn cầu). Nó thay thế những trận đấu bằng một gạch dưới: | Hangman Í331 package import flash.display. import flash.text. import flash.events. public class Hangman extends Sprite private var textDisplay TextField private var phrase String Imagination is more important than knowledge. - Albert Einstein private var shown String private var numWrong int When the class starts it creates a copy of the phrase by running it through the replace function with a regular expression. The expression A-Za-z g matches any letter character A to Z and a to z globally . It replaces these matches with an underscore public function Hangman create a copy of text with _ for each letter shown phrase.replace A-Za-z g _ numWrong 0 The text field we ll set up will use a simple text format for Courier font 30 point. It will set the width and height so that the text will not interfere with the hangman graphic to the right. NOTE The reason I chose Courier is that it is a monospaced font. This means that each letter has the same width. Other fonts have different widths for different letters for example l and w . By using a monospaced font the text characters will not change positions as we substitute letters for the underscores. set up the visible text field textDisplay new TextField textDisplay.defaultTextFormat new TextFormat Courier 30 textDisplay.width 400 textDisplay.height 200 textDisplay.wordWrap true textDisplay.selectable false textDisplay.text shown addChild textDisplay The pressKey function will be assigned to the KEY_UP event for the stage listen for key presses stage.addEventListener KeyboardEvent.KEY_UP pressKey Wow eBook WoweBook.Com 332 Chapter 9 Word Games Hangman and Word Search When the player presses a key we ll use the event.charCode returned to get the letter pressed public function pressKey event KeyboardEvent get letter pressed var charPressed String String.fromCharCode event.charCode After the letter is known the phrase is searched for any matches. We re careful to use toLowerCase so that the key pressed will match both upper- .