03-02-laser-chase.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. Ninjas create two if statements to use the mouse to control the position of an
  3. object in the scene.
  4. New Vocabulary and Concepts
  5. * Comparison Operators
  6. * GDP getMouseX()
  7. * GDP .x() function
  8. * Pseudocode
  9. Reinforced Vocabulary and Concepts
  10. * Boolean Logic
  11. * Functions and Parameters
  12. * if() statements
  13. * Object Names
  14. Sensei Notes
  15. * This is the first introduction to user input.
  16. * This activity uses if statements and Boolean logic.
  17. Solution Steps
  18. 1 Make sure the Ninja opens the correct scene.
  19. 2 Let the Ninja play the game so they can see what functionality needs to be
  20. added.
  21. 3 This step introduces pseudocode. As the code becomes more complex, the manual
  22. places a larger focus on the logic rather than the syntax.
  23. 4 Work with the Ninja to brainstorm and think about the first piece of
  24. pseudocode and the situations it applies to. Lead them to discover we need a
  25. second if statement that covers when the laser is to the right of the mouse.
  26. Help the Ninja write their pseudocode statement. It is okay if it is not perfect.
  27. 5 Compare and contrast the Ninja's pseudocode with the provided pseudocode.
  28. 6 Make sure the Ninja is coding in the scene's Update Every Frame event.
  29. Reference the pseudocode before the Ninja types the if statement. The GDP's
  30. editor will say there is an error. This is because we have an incomplete if
  31. statement. This will be fixed in the next step.
  32. if(){
  33. }
  34. 7 Make sure the Ninja writes the new line of code inside the parentheses. All
  35. GDP objects have an .x() and .y() function that can be used to get or set the
  36. object's position. The error is now gone, but the game hasn't changed. Ask the
  37. Ninja what functionality still needs to be coded.
  38. if(laser.x()) {
  39. }
  40. 8 This is the introduction to operators. If the Ninja does not understand what
  41. the less than operator means, then ask them if a real-world object's age or size
  42. is less than another object's age or size.
  43. if(laser.x() < ) {
  44. }
  45. 9 Make sure the Ninja writes the getMouseX() function in the correct location of
  46. the if statement. This function will return the x value of the current or last
  47. known mouse position.
  48. if(laser.x() < getMouseX()) {
  49. }
  50. 10 Make sure the Ninja writes the new line of code inside the body of the if
  51. statement. Since we want to easily control the direction that the laser moves,
  52. we are using a version of moveX that takes the speed as a second parameter. If
  53. the second parameter is left out, then the laser will move at the rate of speedX
  54. that is set directly in the properties tab. A speed of 400 is not too slow or
  55. too fast, but the Ninja can change it if they want.
  56. if(laser.x() < getMouseX()) {
  57. moveX(laser, 400);
  58. }
  59. 11 Make sure the Ninja types the new if statement after the first. There will be
  60. another error like in step 6.
  61. if() {
  62. }
  63. 12 Help the Ninja write the entire conditional statement using the pseudocode
  64. and the first if statement. Make sure they use the greater than sign.
  65. if(laser.x() > getMouseX()) {
  66. }
  67. 13 Help the Ninja write the body of the second if statement using the pseudocode
  68. and the body of the first if statement. Mention that since we want the laser to
  69. move to the left, we want to use a negative speed.
  70. if(laser.x() > getMouseX()) {
  71. moveX(laser, -400);
  72. }
  73. 14 This step asks the Ninja to change the speed being passed to the moveX
  74. function. Make sure the Ninja changes it at least twice to see how a smaller and
  75. a larger number affects the speed.
  76. 15 Before the Ninja submits the scene, verify that the script is correct.
  77. */
  78. if(laser.x() < getMouseX()) {
  79. moveX(laser, 400);
  80. }
  81. if(laser.x() > getMouseX()) {
  82. moveX(laser, -400);
  83. }
  84. /**
  85. Sensei Stops
  86. 5 Read your pseudocode if statement to a Sensei. Explain what your condition is
  87. and what happens when that condition is met. Why is it helpful to use pseudocode?
  88. If the Ninja's pseudocode is incorrect, discuss what is wrong and how to correct
  89. it. Pseudocode is important because it lets you plan before you start coding.
  90. 10 Play your game and explain your code to a Sensei. Be sure to explain the
  91. three parts of the condition and the code in the body. What do you think you
  92. need to change for the second if statement? Right now, the laser will only move
  93. right towards the mouse. Have the Ninja connect the code to the action in the
  94. scene. Have the Ninja think about what parts of the if statement will need to
  95. change to make the laser move in the opposite direction.
  96. 15 Explain to a Sensei how planning out your code before you program it is
  97. helpful. Was it easier to understand the real code ofter you wrote out the
  98. pseudocode?
  99. Compare planning with pseudocode to brainstorming and outlining before writing
  100. in school.
  101. */