Step 2: Ask for the Numerator and Denominator

In this step we'll program Isaac to set up a fraction by asking the player what the numerator and denominator are. We'll pop up a text input box for the user to type the numerator and denominator into.

Right-click on Isaac and select "Add Script".

The first thing we'll do is tell the player that they can press Spacebar to show a fraction with apples.

Grab the "When the level starts" block (from Events), and also grab "print" (from Control Flow).

Then grab the empty text block from Operators, and connect it to the "print" block, and type into it "Press spacebar to show a fraction with apples!"

Let's test the game so far to make sure it does what we want. Click Play (in the top-right corner) and preview the game. Make sure to save the script as "Apple controller".

The message works! Now we'll go back to the script, so click Edit.

We'll make it so that when the player presses spacebar, two message boxes will pop up asking them to enter the numerator and denominator.

Grab the "When the player presses key" block (from Events), and drag it out under the first blocks. Click on "backspace" and change it to "spacebar".

From the Properties section of Variables, grab two "Set true/false i" blocks and drag them into the previous event block.

Click on the "i" in the first variable block and rename it to "numerator". Then rename the second variable to "denominator". Click on true/false for both variables and change them to number variables.

Now we'll grab the blocks to pop up an input box. Grab two "prompt for number with message" blocks (from Operators), and drag them both into the empty gaps in the variable blocks.

In the numerator prompt block, type the message "What is the numerator?".

Then in the denominator prompt block, type the message "What is the denominator?".

Lastly, we'll create a textfield to display the fraction in the corner of the screen. Grab the "create new textfield" block (from Draw), and drag it under the variable blocks.

Drag out the text block, and replace it with a "create text with" block (from Operators). Click on the cog icon in the "create text with" block, and drag another item block into the right side. Click on the cog icon again to close this small window.

Now grab "number numerator" from Variables and drag it into the top slot of the "create text with" block. Then grab an empty text block (from Operators), drag it into the second slot down, and type "/" into it. Finally, grab "number denominator" (from Operators) and drag it into the third slot down.

Now play the game. You should be asked what the numerator and denominator will be, and then you should see the fraction appear in the textfield.

results matching ""

    No results matching ""