The name input screen in the game is a scene with very little Chinese characteristics.
We know that there are only 26 English letters and only several hundred Kana in Japanese, but there are too many Chinese characters, as a result, only some Chinese characters can be used to fill the Kana name when the name input image is translated into Chinese.
The input name function has no important value, but the implementation method of this function is worth studying.
The game has a default number input window, but it is very difficult to use.
Today, let's write a digital input screen with reference to the name input screen. Used by gamers to input numbers into the game.
There are three classes involved in the name input screen: scene_name, window_nameedit, and window_nameinput.
Scene_name is a scenario class, window_nameedit is a window for displaying the current number, and window_nameinput is an option for inputting numbers.
Therefore, we need at least three classes: scene_number, window_editnumber, and window_inputnumber.
In scene_name, there are only three methods: Prepare, start, and on_input_ OK.
Add these three methods to our scene_number class. However, you need to change the parameters required during initialization.
# Encoding: UTF-8 #===================================================== ========================================================== # ■ scene_number # digit # digital input image #================================ ========================================================== ========= class scene_number <scene_menubase # ---------------------------------------------------------------------------- # ● Preparation # javasdef prepare (var_id, max_char) @ var_id = var_id @ max_char = max_char end # begin # ● start processing # define def start super @ edit_window = window_editnumber.new (@ max_char) @ input_window = window_inputnumber.new (@ edit_window) @ resolve (: OK, method (: on_input_ OK) end # else # ● enter "OK" # define def on_input_ OK $ game_variables [@ var_id] = @ edit_##numbertoint return_scene endend
The name of some variables is modified in the scene_number class.
The initialization method accepts two parameters. One is the number of the global variable to be changed. We store the entered number in the global variable, and the other is the maximum number of characters, that is, the maximum number of digits.
The start method generates two window class instances.
Window_editnumber is a window used to display input numbers.
Most of its code is the same as window_nameedit.
But there are also differences that need to be modified.
In its initialization method, we only accept one max_char parameter.
We use the array @ number to store the input number, initialize it as null, and modify the corresponding operation in other methods.
The array does not directly return the integer we need. Therefore, we need to use the numbertoint method to calculate the numbers of all bits in the array as an integer.
# Numbers # ● return the entered number # ------------------------------------------------------------------------------ def numbertoint return-1 If @ number. Empty? Ans = 0 for I in @ number do ans = ans * 10 + I end return ans end
If-1 is returned, the array is empty.
In the name input screen, the width of a text is the width of a Chinese character, which is slightly larger for numbers.
# Coding # ● get the text width # ------------------------------------------------------------------------------ def char_width text_size ("0"). Width end
Change the text width to the numeric width.
It works like this. During window initialization, set the window size and coordinates, and assign an initial value to the variables related to the input number.
Whenever the content to be displayed changes, call the refresh method to redraw the window.
# Refreshing # ● refresh # ------------------------------------------------------------------------ def refresh contents. clear @ max_char.times {| I | draw_underline (I)} @ number. size. times {| I | draw_char (I)} cursor_rect.set (item_rect (@ index) End
In this method, use draw_underline to draw @ max_char underline to the screen, and then use the draw_char method to draw the elements in the @ number array. Finally, specify the cursor position.
Some auxiliary methods are used to draw underlines and texts. The most important method is item_rect (obtain the rectangle of the project ).
# Region # ● obtain the left-side coordinates drawn by name # required def left number_center = (contents_width)/2 number_width = (@ max_char + 1) * char_width return [number_center-number_width/2, contents_width-number_width]. min end # rectangle # ● obtain the project's rectangle # -------------------------------------------------------------------------- def item_rect (INDEX) rect. new (left + Index * char_width, 0, char_width, line_height) End
The item_rect method calls the left method to obtain the leftmost coordinate, and then uses index and char_width to calculate the offset of the rectangle.
In order to make the window style more beautiful, the parameters used to determine the drawing coordinate in the item_rect method and the parameters used to determine the window size and coordinate in the initialize method must be adjusted and set reasonably.
Finally, it is window_inputnumber. In the number input screen, select the number window.
To enter 10 numbers and confirm the input, the window should have at least 12 options.
# Digit # ● number table # ------------------------------------------------------------------------ table = [, 8, 9, 6, 3, 0, 'zero', 'confirmed']
12 options are displayed in a 4*3 number table.
# Define # ● initialization object # define def initialize (edit_window) Super (edit_000000000000x + edit_00000000width/2-60, edit_000000000000y + edit_00000000height + 8,120, fitting_height (4 )) @ edit_window = edit_window @ page = 0 @ Index = 0 refresh update_cursor activate end
In the initialization method, adjust the window size and coordinates so that 12 options are displayed.
There are too many magic numbers in the window class window_nameinput with the input name. You need to adjust them one by one to adapt them to the option Window 4*3.
The default button processing method has been written in the parent class window_selectable, And the process_cursor_move method is used to process the movement of the cursor without modification.
However, when you press the OK or cancel key, you need to set the operation, So rewrite the process_handling method.
# Handler # ● processing of "OK", "delete character", and "cancel input" # ---------------------------------------------------------------------- def process_handling return unless open? & Active process_jump if input. Trigger? (: A) process_back if input. Repeat? (: B) process_ OK if input. Trigger? (: C) End
The process_ OK method takes three cases into account when processing the OK key:
When the current option is at the zeroth key, set the number to 0.
When the current option is in the number key, add the corresponding number.
When the current option is located at the validation key, it determines whether the input is null. If not, call_ OK _handler can be called to call the confirmation method specified by the scene_number class.
The complete code for window_inputnumber is as follows:
# Encoding: UTF-8 #===================================================== ========================================================== # ■ window_inputnumber # ---------------------------------------------------------------------------- # in the digital input screen, select a number. #===================================================== ==================================================== Class window_inputnumber <window_selectable # EMPLOYEE # ● number table # -------------------------------------------------------------------- table =, 9, 6, 3, 0, 'zero', 'confirmed'] # -------------------------------------------------------------------------- -- # ● Initialization object # effecdef initialize (edit_window) Super (edit_00000000x + edit_00000000width/2-60, edit_000000000000y + edit_00000000height + 8,120, fitting_height (4 )) @ edit_window = edit_window @ page = 0 @ Index = 0 refresh update_cursor activate end # ------------------------------------------------------------------------ # ● obtain a Word Table #------ -------------------------------------------------------------------- Def table return [Table] end # else # ● get text # -------------------------------------------------------------------------- def character @ index <12? Table [@ page] [@ Index]: "" end # ---------------------------------------------------------------------- # ● determines whether the cursor position is on "0" # -------------------------------------------------------------------------- def is_back? @ Index = 10 end # cursor # ● determine whether the cursor position is "OK" # ------------------------------------------------------------------------ def is_ OK? @ Index = 11 end # -------------------------------------------------------------------------- # ● obtain the project's rectangle # define def item_rect (INDEX) rect = rect. new rect. X = index % 3*32 + index % 3/5*16 rect. y = index/3 * line_height rect. width = 32 rect. height = line_height rect end #-------------------------------------------------- ------------------------ # ● Refresh # -------------------------------------------------------------------- def refresh contents. clear change_color (normal_color) 12. times {| I | draw_text (item_rect (I), table [@ page] [I], 1)} end # cursor # ● update cursor # defdef update_cu Rsor cursor_rect.set (item_rect (@ index) end # cursor # ● determines whether the cursor can be moved # javasdef cursor_movable? Active end # cursor # ● move the cursor down # wrap: Allow loop # define def cursor_down (WRAP) If @ index <9 or wrap @ Index = (index + 3) % 12 end # ---------------------------------------------------------------------------- # ● move the cursor up # wrap: Allow loop #-------------------------------- ---------------------------------------- Def cursor_up (WRAP) If @ index> = 3 or wrap @ Index = (index-3 + 12) % 12 end # cursor # move cursor to the right # wrap: allow loop # javasdef cursor_right (WRAP) If @ index % 3 <2 @ index + = 1 elsif wrap @ index-= 2 end #------------- ----------------------------------------------------------- # ● Move the cursor to the left # wrap: Allow loops # javasdef cursor_left (WRAP = false) if @ index % 3> 0 @ index-= 1 elsif wrap @ index + = 2 end # else # ● move to next page #---------------------------------------------------------- ---------------- Def cursor_pagedown refresh end # cursor # ● move to previous page # def cursor_pageup refresh end # cursor # ● process cursor movement # cursor #--------------------------------------------------------------------- ----- Def process_cursor_move last_page = @ page super update_cursor sound. play_cursor if @ page! = Last_page end # handle # ● "OK", "delete character", and "cancel input" # handle def process_handling return unless open? & Active process_jump if input. Trigger? (: A) process_back if input. Repeat? (: B) process_ OK if input. Trigger? (: C) end # ---------------------------------------------------------------------------- # ● jump to "OK" # define def process_jump if @ index! = 11 @ Index = 11 sound. play_cursor end # prop # ● remove a character # ---------------------------------------------------------------------------- def process_back sound. play_cancel if @ edit_window.back end # Processing # ● processing when you press the OK key #-------------------------------------------- ------------------------------ Def process_ OK if is_back? @ Edit_window.restore_default @ edit_window.add (0) elsif! Is_ OK? On_number_add elsif is_ OK? On_number_ OK end # character # ● Add a number character # define def on_number_add if @ edit_character character add (character) sound. play_ OK else sound. play_buzzer end # prop # ● confirm the number # define def on_number_ OK if @ edit_00000000numbertoint =-1 sound. play_buzzer else sound. play_ OK call_ OK _handler end endend
All three classes have been written.
Add the following script to game events:
Scenemanager. Call (scene_number)
Scenemanager. Scene. Prepare (1, 10)
You can enter the number input screen and enter a number to the specified global variable.