0

I am trying to get the speed variable to change the speed of the ball, but when I set the dx and dy variables to the speed, they don't update after you lose a life. When I set dx and dy to the value of Xspeed and Yspeed, the speed doesn't change when you change difficulty.

I've tried setting the value of dx and dy to Xspeed and Yspeed because I thought it might keep the value instead of refreshing it, but it just doesn't change it when I want it to.

Full link to the code, https://codepen.io/Jacob-Bruce/pen/mQgbXa

// ball movement - change variables to change speed
var Xspeed = 2
var Yspeed = -2
var dx = Xspeed;
var dy = Yspeed;

function hardMode() {
   lives = 1;
   Xspeed = 7;
   Yspeed = -7;
   redraw();
}

I want the speed to be able to be changed with the difficulty buttons, and it to stay the same speed until you either die or change difficulty. Right now, it either changes speed once and doesn't at all.

Jacob Bruce
  • 55
  • 2
  • 10
  • 1
    If you got two variables that should contain the same value, why do you need two variables at all? – Jonas Wilms Feb 03 '19 at 18:11
  • What you are trying to achieve is binding the values of `dx` and `dy` to `Xspeed` and `Yspeed`, right? I don't quite understand why, but you should have a look here: [link](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Musti Feb 03 '19 at 18:14
  • I know it doesn't make sense to have two variables, it was just my attempt at making something happen. But when I use the difficulty buttons to change the speed, when you lose a life, it reverts back to what it is set in the original declaration. – Jacob Bruce Feb 03 '19 at 18:20

0 Answers0