Conditional Blocks and Randomness

An event handler is an event and a sequence of responses. It is a "when" block in app inventor and all the blocks within. When something happens, e.g., a button click, the app responds.

   In HelloPurr, when the user clicks the button, the meow sound is played.

But what if your app needs to make a decision, e.g.,

    Every time you click a button a different picture appears on the button.

This requires a conditional (IF) block and a way to generate a random number.

Conditionals

    if, and if-else both in the math drawer
    the test can be any boolean condition, e.g., is a number 1?

Demo: build a game where clicking on an image button changes the image back and forth.

Random
    randomInteger in the math drawer. You can get a random number for any range of possibilities.

Demo: build a game where the image changes randomly between two things.

Silly Animal Game


Student Assignment
Create a silly animal game that randomly shows 1 of 2 animal pictures and plays a corresponding animal sound.
Extra credit:
Modify your silly animal game so that it randomly shows 1 of 3 pictures and plays a corresponding animal sound. Note that you'll need to use a variable and a nested if-else block.For help, see the chapters on variables and decision-making


Comments