Reputation: 1
in my code right now i've got a lot of strings and dots which are getting pulled to the position of my cursor but what im trying to do right now is that when i do a mouseclick that the variable changes to a negative value so the strings and dots are getting pushed away. Im new to JS and i simply dont know how to Code that.
var c = 1000;
canvas.addEventListener('mousedown', back);
physics.makeAttraction(mouseParticle, myCurrentParticle, c, 100);
function back(){}
i dont know if thats enough code for you guys if theres more needed pls tell me.
Upvotes: 0
Views: 95
Reputation: 12891
You yould utilize the Math.sign() method to check if the value of c is positive or negative and act accordingly.
function back() {
if (Math.sign(c) > 0) {
c = -1000;
} else {
c = 1000;
}
}
Upvotes: 0
Reputation: 92450
You can multiply your value by -1 in the function to swap the sign.
var c = 1000;
let d = document.getElementById('num')
num.innerText = c
function back() {
c *= -1
num.innerText = c
}
<span id="num"></span>
<button onclick="back()">reverse</button>
Upvotes: 1