任何玩家都可以通过浏览器控制台更改纸杯蛋糕的数量来轻松作弊。我想知道是否有办法阻止用户这样做。这也发布在 github 上.
var numberOfCupcakesDisplay = document.getElementById("numberOfCupcakes");
var cupcake = document.getElementById("cupcake");
function updateValues() {
numberOfCupcakesDisplay.innerHTML = amountOfCupcakes.toString();
//displays number of cupcakes to screen
document.getElementById("amountOfToasters").innerHTML = amountOfToasters.toString();
//displays number of toasters to screen
document.getElementById("toasterButton").innerHTML = "        " + Math.round(costOfToasters).toString() + " cc";
//changes cost of toaster every time it is bought by 110%
}
cupcake.addEventListener('webkitAnimationEnd', function(e){
e.preventDefault();
this.style.webkitAnimationName = '';
}, false);
function clickCupcake() {
++amountOfCupcakes;
cupcake.style.animation = "shiftCupcake";
cupcake.style.webkitAnimationName = "shiftCupcake";
}
updateValues();
最佳答案
是啊!!正如其他答案提到的那样,阻止用户从浏览器控制台更改值是IMPOSSIBLE。换句话说,您只是在问如何编写 JS 代码以便用户无法调试。你总是可以使用一些方法让那些想在游戏中作弊的人过不去。
1.Obfuscate the code.
查看这些链接以获取有关混淆的更多信息。
2.Do not store the game's control values in global variable.
不要将控制值存储在全局变量中。而是定义一个类并将这些变量作为私有(private)变量以便用户必须深入挖掘才能找出断点的位置。
3.Minify/Compress your javascripts.
混淆或多或少也包括缩小。
关于javascript - 防止用户在浏览器控制台中更改游戏值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590690/