The purpose of this mini-lab is to practice working with arrays (Chapter 10) and random access.
Start out by adding two characters to a scene. Decide on what jokes you want to use. You should have at least four of them.
Create a tellRandomJoke method. (Should this be a
world-level method or a class-level method? Think about what its
purpose is.) Inside this method, create two array variables, one to
hold the names of who's there in the knock-knock jokes, and the other
to hold the punchlines. You will need to add the name and punchline
items to the appropriate arrays
when the array variables are created. (Why?)
The first two lines in the method should alternate characters saying the first two lines of the joke. (Character 1: "Knock-knock". Character 2: "Who's there?") You may want to change the font, and/or set the duration of the sayings to make them visible longer.
Now we want to use a random number, call it i, to get which joke we want to say.
Create a new class-level variable to hold an integer. (The initial
value does not matter.) In the program, set the value of i
to a placeholder value (i.e., just some value, like 0 or 1), by
dragging the variable into the code and choosing to set value.
Next, look at the world-level functions. Under the random functions,
choose the random number function, and drag it onto the
value for i. Then click on the "more" option to set its
minimum, maximum, and integerOnly attributes to appropriate values.
This value represents an index in the array, so its minimum should be 0,
and its maximum should be the number of jokes you have. Set the
integerOnly attribute to be true, otherwise the function may produce
values with decimal places, like 1.25, 0.05, 3.647, etc..
Now we can use this value of i to select the appropriate
array values from the array of names and the array of punchlines to
finish the joke. Your characters should continue to alternate lines of
the joke using this information.
To test that your joke-telling scenario is working correctly, put an
infinite loop in the world.my first method that calls the
tellRandomJoke method.
Modify the tellRandomJoke method to take the characters as
parameters. You will need two parameters, and will need to modify the
joke-telling sequence to use the parameters as the characters, instead
of the actual characters. Update the call to
tellRandomJoke in the world.my first method to
include your characters as the parameters.