![]() Why? The answer can be found in the Step Event. Hurray, it works! We can move the cursor with the up and down key, but the movement is way too fast. Ok, let’s check our game again! Press the play button and look at the result: If the value of our cursor equals 1 (Load button), we get a new y value of 163 and the cursor will change the position. If the value of our cursor equals 0 (Start button), the y-value remain 148. Why? The distance between the menu buttons is 18 pixel, so we must move our cursor by a value of 18 for every button change. The change the y-value of our cursor from 145 to 145 + 18 x cursor. Next, we modify some code lines in the Draw Event, look at the red frame: Feel free to try it out for yourself! This is your first exercise to test your new knowledge! If this is the case, the value of the cursor variable will be increased by 1 ( cursor++ )! Here, we will face our first problem: using the down key frequently, we can reach infinitely high values! Therefore, we include another if-statement, which change the value back to 0, if we reach higher values of cursor than the value of max_cursor. In our case, we check if the down arrow key is pressed. GMS2 is evaluating the expression and if this expression is true, the statement will be executed. You see, our script is already useful! After that, we include a if-statement. So, what happens here? First, we called scr_input(), giving us access to the variables up_key and down_key. Insert the following code lines in this event: The code lines in the Step Event will be called every frame, in our case 30 times in one second. The value of this variable is the maximum number of buttons in our title screen (in this case: 2 is the value of the End button). Don’t forget, GMS2 starts to count arguments and variables at 0! We use the variable cursor to store our current position in the title menu, the initial value of 0 is therefore the Start button. Our title screen has three buttons, Start, Load and End, corresponding to the values 0, 1, 2. We initiate two variables, cursor and max_cursor. The code lines in a Create Event will be only called one time, during the instance creation of the corresponding object. Double-click on this object, we want to add some code lines! First step: add a Create Event using the Add Event button. Well, let’s return to our only object, obj_titlemenu. We write this code for the other three directions and we have some very useful code lines, which may be called everytime using scr_input() Translated: if the right arrow key is pressed ( vk_right), the value true will be stored in the variable right_key. We want to store this value in a variable, in case of our script it’s the variable **right_key for the true/false check of our right arrow key. keyboard_check(argument0) will return a true or a false* as value (boolean data type). argument0 is the key to check the down state of. Keyboard_check(argument0) is an already implemented script in GMS2. Name the script scr_input and include the following code: ![]() Go to the ressources tree and right-click on Scripts, than left-click on Create. Nice!Įnough theory, let’s write our own script. Therefore, we just need to write for example addition(3, 5) and we value 8 will be returned. The sum of both variables is stored in the variable c and variable c is returned by the script. Let’s look at the code:Īrgument0 and argument1 are saved in the variables a and b. The script adds both numbers and returns the sum. I will explain this possibility using a script named addition(argument0, argument1) Both arguments are numbers, which are passed to the script. We want to put our keyboard checks into one script and call it every time we need it using script() Another possibility to use scripts is with the involvement of arguments. We will use this method to create an input script during this part. We don’t want to insert the whole code segment each time when we need it, so the transfer of these segments into scripts is very useful. Scripts contain code segments, which are used frequently in our projects. We will return to scripts and how to use them. ![]() Today, we will continue with the implementation of some keyboard inputs and the corresponding cursor movement!įeel free to join this course and make your first steps as a game developer! Welcome to the second part of our Game Maker Studio 2 (GMS2) tutorial! In the last part, we finished the title screen draw and create a script for some font shadow. Your first game with Game Maker Studio 2 Part 2:Keyboard input and cursor movement! Schotti December 18, 2016
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |