Hey all. Because Crapple is both obscure and extremely limited, i've decided to write a tutorial on how to figure out the frame a button is pressed and released, as it's a pretty fundamental thing to know. In this tutorial you will learn:
Not everyone is used to Ye Olde Console, so I want to give a quick intro into the Crapple OS console and how to get to the code editor. If you have used a console before, you can likely skip this part.
After booting up your Crapple for the first time, you'll be taken to a blank screen where you can type onto. This is the console. To load a file to be run, or to create a new blank file you need to use the load command, followed by the name of the file you want to load and then hit enter.
To save a file, you do the same thing but with the save command.
To run the loaded file, you can simply use the run command. Keep in mind that you cannot run a file that isnt loaded by typing its name after run.
The same thing can be said for the edit command, which is used to take you to the code editor.
When writing in JANK, there are a few things you need to be aware of.
Firstly, memory is stored in the start of the program, so that it doesn't change place as you write more code. To begin with, we are going to fill lines 1 - 4 with a '0'.
0 | 1 | 0 2 | 0 3 | 0 4 | 0 5 |
ln1 will be the value of if the button was pressed last frame, ln2 will be the value of if the button is being pressed this frame, ln3 will be if the button is starting to be pressed this frame, and ln4 will be for if this frame the button has been released.
As this language is a language of JANK, its best practice to leave the first and last line of the program free of code or memory.
Next were going to start writing out actual code.
0 | 1 | 0 2 | 0 3 | 0 4 | 0 5 | 6 | btn X 2 7 |
With the btn function, you can get the input of a key being pressed and write the value to a line.
This code checks to see if the X key is being pressed, and then set the value to ln2, 1 for if the key is being pressed, and 0 if its not being pressed.
At the end of the code we want to set the old key press value to be that of the current key press value, so that we can compare them against eachother.
5 | 6 | btn X 2 7 | 8 | set 1 ln2 9 |
This is the set function. You can use it to move and set lines to different lines. The first line specifies which line to write to. If you were to use a ln code before the number, you can tell the program to get the value of the line that that line points to, rather than the value of the line specified.
The second line specifies what value to set to the destination line. You can either set a value, or you can tell it the value of another line by using the ln code, like we do here.
5 | 6 | btn X 2 7 | 8 | drw txt 1 key %2% pressed %3% released %4% 9 | 10 | set 1 ln2 11 |
To draw our key presses to the screen so we can test that our code is working, we want to use the drw function.
We need to tell the drw function three things: 1) That we want to draw text to the screen 2) What line of the screen to draw the text to 3) what we want to draw to the screen.
Anything that you write after the line of the screen you wan to write to will be written to the screen. You can get inject data held at lines of the program into the middle of your message using the % symbols on either side of the line number you wish to reference. You can also use ln to tell the program to goto where the line references, rather than the value of the line designated.
Comparing two values agaisnt eachother is the same as other programing languages is very similar to other languages. For the first value, you can specify either a line or the line it points to with ln, and with the second value you can specify either a value or the value at a line with ln.
5 | 6 | btn X 2 7 | 8 | if ln2 = 1 9 | // Pressed 10 | if ln1 = 0 11 | set 3 1 12 | end 13 | end 14 | 15 | drw txt 1 key %2% pressed %3% released %4% 16 | 17 | set 1 ln2 18 |
Here we're using two if statements to check if a button has started to be pressed on this frame. We do this by checking if the button is being pressed this frame at ln8 and if the button wasnt pressed the last frame, at ln10. Then at ln11 we set ln3 to true.
5 | 6 | btn X 2 7 | 8 | if ln2 = 1 9 | // Pressed 10 | if ln1 = 0 11 | set 3 1 12 | end 13 | // Un Pressed 14 | if ln1 = 1 15 | set 3 0 16 | end 17 | end 18 | 19 | drw txt 1 key %2% pressed %3% released %4% 20 | 21 | set 1 ln2 22 |
Next we want to add an if for setting it all back if the key was pressed both this frame and last frame.
The code to check if the button has been released is very similar to the code to check if it has been pressed. Instead you want to check if the button was being pressed last frame, but is not being pressed this frame. We also want to write the result to ln4 instead. This code looks like this:
5 | 6 | btn X 2 7 | 8 | if ln2 = 1 9 | // Pressed 10 | if ln1 = 0 11 | set 3 1 12 | end 13 | // Un Pressed 14 | if ln1 = 1 15 | set 3 0 16 | end 17 | end 18 | 19 | if ln2 = 0 20 | // Released 21 | if ln1 = 1 22 | set 4 1 23 | end 24 | // Un Released 25 | if ln1 = 0 26 | set 4 0 27 | end 28 | end 29 | 30 | drw txt 1 key %2% pressed %3% released %4% 31 | 32 | set 1 ln2 33 |
And that's it!
I hope that this helps. There are some improvements that can be made to this code to make it shorted, but I chose not to include them for simplicity's sake. If you wanted something extra to do to see if you fully understand how the code works, why dont you see if you reduce the amount of if statements needed to four?
As always, if something isn't entirely clear, and you need clarification, you can contact me here.
25 04 21