Unit 6 Lesson 6 Puzzle 16
OK, I don’t know if this is a bug or something I just don’t understand.
I’ve copied my code for the program below.(and you can see my remix at: https://studio.code.org/projects/applab/xb-XonRg-8lspcN-W4C-dJs7hxMlwSLWxJYQfkahT4A
The problem I have is with line 16:
var speed = getProperty(“slider1”, “value”);
The program will not work as is. I have to remove the var to get it to work. Now I understand that it is not good practice to re-declare a variable, but shouldn’t it still work? Wouldn’t it just (non-tech talk) make a new speed variable in place of the old one with the value of the slider? If not, how do the onboard events work when they redeclare a variable each time a button is pressed? I also don’t remember having these troubles when my students did this lesson in the past which is why I’m wondering if this is a bug
This is a copy of the code
// Set the speed of the motorcycle
var speed = 10;
// Move the motorcycle up when the left button is clicked
onBoardEvent( buttonL, “press”, function(event) {
var y = getProperty(“motorcycle”, “y”);
setProperty(“motorcycle”, “y”, y - speed);
});
// Move the motorcycle down when right button is clicked
onBoardEvent( buttonR, “press”, function(event) {
var y = getProperty(“motorcycle”, “y”);
setProperty(“motorcycle”, “y”, y + speed);
});
onEvent(“slider1”, “change”, function(event) {
var speed = getProperty(“slider1”, “value”);
});