Side Scroller 5

Planning
Step 1
Temp1539974468
Step 2

Define a new function called showOption that we are going to use to show the message when the player is on top of the sign.

Use cut and paste to move the code there.

Temp1539892086
Step 3
Call the "showOption" function when the player is on top of the sign
Temp1539892119
Step 4

Right now, the only thing our showOption function will do is tell you that you are on top of the sign. If we want to change that, we need to be able to change the variable called message.

Do do this, we are going to give the function an input, usually called a parameter

Step 5

We're going to remove the variable from the first line of the function, and move it up to become a parameter.

Temp1539892179

How will the program know what the message is?

Step 6

Now that our showOption function has a parameter, we now have to supply that information whenever we call the function.

Add this string to make it display an option to check the sign.

Temp1539892229
Check Your Work
Step 7
Temp1539892274
Step 8
Temp1539892290
Challenge
Step 9

Change your think function so that it takes a parameter called thought. This should work just like the showOption function, so that we can supply what thought we want to think.

Step 10

Now that think has a parameter, we should be able to add this code to see what the sign says, when we press enter while overlapping it.

Temp1539892340
Check Your Work
Step 11

Press Save and Run and then press the "enter" key while on top of the sign. You should see the message we put in () when we called the think function.

Temp1539892365
Step 12
Temp1539892397
Step 13
Temp1539892411
Information
Step 14

Just like functions can have inputs called parameters, we can also give them outputs, which are called returns.

Example:

var doubleThisNumber = function(aNumber){
    var doubledNumber = aNumber*2;
    return doubledNumber;
}

In our example, aNumber is the parameter and doubledNumber is the return.

Step 15
Temp1539892459
Step 16
Temp1539892469
Step 17
Temp1539892481
Step 18
Temp1539892491
Step 19
Temp1539975412
Challenge
Step 20
Give the player someone to talk to.

We're going to make a person object for the player to interact with. Here are the steps:

  • Declare a new variable called villager
  • In setup assign villager to an object with the same properties as player.
  • Define a new function called drawVillager that draws them just like in drawPlayer.
  • Create a new scene 4 in drawScene, and call your drawVillager function in it.
Check Your Work
Step 21
Temp1539975468

Your scene doesn't have to have a sweet castle like this one. Just make something new!

Challenge
Step 22
Talk to the villager

For our final challenge, we are going to make an interaction with the villager.

Just like you did with the sign, you should:

  • In the movePlayer function, use another if statement to check if scene === 4
  • Inside that if, create another if that checks if the player is overlapping the villager
  • Show the option to "Talk" to the villager
  • If you press the key to interact, have the player think "How are you today?"
Check Your Work
Step 23
Temp1539974459
Side Scroller 5 Info

Account

MVCode Clubs

Created By

Mandala2 slinkous

Course:

Side Scroll Adventure

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach