Step 1

Displaying lives in a text field

We're already keeping track of the player's lives in a global variable, so to display this number we need to create a text field. Inside the player character's script, grab the "create new textfield" block (from Draw) and drag it underneath the other blocks in the "When created" stack.

We want the number of lives to be displayed in this text field, so we need to grab the variable block for "number lives" from the Variables tab.

We need to grab an extra block to convert the number to text. The block we need is named "create text with" and it's under Operators. Drop this block into the space where the empty block is. (The "create text with" block has two slots next to it, but it's okay to leave one of them empty since it will ignore the empty space.) Now this block will allow a number block to be attached to it, so grab the "number lives" block and snap it onto the "create text with" block.

Lastly, we need to store this text field as a new variable so that we can retrieve it and update it as the number of lives changes. This is because the "Create new textfield" block automatically creates a local variable (some other blocks like "When I am touched get toucher" also do this). Local variables only hold their values temporarily, which is why we need to store the text field as a property variable if we want to be able to retrieve and update its value.

To store this text field as a new property variable, grab the block "Set true/false i to" from the Properties section in the Variables category, and drag this block under the "create new textfield" block. Click on true/false to change the variable type to instance, and then rename this variable to "lives text".

Then grab the "instance textfield" block from the Local section of the Variables category, and drag it inside the blank space in our newly created "instance lives text" block.

Now if you play the game, you'll briefly see your number of lives displayed in the top-left corner of the game, but the number stays there. We need to get it to follow the game camera.

results matching ""

    No results matching ""