【发布时间】:2018-12-14 01:19:05
【问题描述】:
我正在尝试创建一个类似于 cookie clicker 的游戏。我现在正在尝试使用bota 和atob 来创建和解码保存代码。我的保存代码是一个字符串,所有变量都用句点分隔。我可以使用bota 对字符串进行编码而不会发生任何意外。当我将编码的字符串放入base64 decode 时,我得到 9999.1.0.0 女巫是想要的结果。当我去我的网站(本地主机)并输入保存代码时,我得到了
Uncaught DOMException: Failed to execute 'atob' on 'Window': 要解码的字符串编码不正确。
我只使用数字和句点,因此存在非 ascii 字符应该不是问题。有人可以帮忙吗?
index.js:
//on page load function
function onLoad() {
var name = prompt("Please name your Universe");
}
//Clicking
var atoms = 9999;
var clickValue = 1;
function atomClick() {
atoms = atoms + clickValue;
document.getElementById("atoms").innerHTML = abbrNum(atoms , true , 2);
};
//upgrades
function upgradeClickValue () {
clickValue = clickValue * 2;
};
//Auto click modifiers
//create veriables
var elmts = 0;
var molecules = 0;
function buyElement() {
var elementCost = Math.floor(10 * Math.pow(1.1,elmts));
if(atoms >= elementCost) {
elmts++;
atoms = atoms - elementCost;
document.getElementById('elmts').innerHTML = abbrNum(elmts , true , 2);
document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
};
var nextECost = Math.floor(10 * Math.pow(1.1,elmts));
document.getElementById('elementCost').innerHTML = abbrNum(nextECost , true , 2);
};
function buyMolecule() {
var moleculeCost = Math.floor(100 * Math.pow(1.2,molecules));
if(atoms >= moleculeCost) {
molecules++;
atoms = atoms - moleculeCost;
document.getElementById('molecules').innerHTML = abbrNum(molecules , true , 2);
document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
};
var nextMCost = Math.floor(100 * Math.pow(1.2,molecules));
document.getElementById('moleculeCost').innerHTML = abbrNum(nextMCost , true , 2);
};
window.setInterval(function() {
data = atoms + "." + clickValue + "." + elmts + "." + molecules;
atoms = atoms + (elmts * 1) + (molecules * 2);
document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
document.title = "Atoms: " + abbrNum(atoms , true , 2);
}, 1000);
//round numbers
const COUNT_ABBRS = [ '', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'Se', 'Sp', 'Ot', 'No', 'De'];
function abbrNum(count, withAbbr = false, decimals = 2) {
const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000));
let result = parseFloat((count / Math.pow(1000, i)).toFixed(decimals));
if(withAbbr) {
result += `${COUNT_ABBRS[i]}`;
}
return result;
}
function reset() {
atoms = 0;
clickValue = 1;
elmts = 0;
elementCost = 10;
molecules = 0;
moleculeCost = 100;
}
//initalising save var
var data = atoms + "." + clickValue + "." + elmts + "." + molecules;
console.log(data);
var encodedString = btoa(data);
function save() {
// Encode the String
encodedString = btoa(data);
console.log(encodedString);
alert("Save Code: " + encodedString);
}
function load() {
var pastedValue = prompt("Past Your Save Code Here.");
// Decode the String
if (pastedValue === "") {
alert("You left the text box blank.");
} else {
if (pastedValue = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/) {
var decodedString = atob(pastedValue);
console.log(decodedString);
var splitRslt = decodedString.split(".");
console.log(splitRslt)
atoms = splitRslt[0];
clickValue = splitRslt[1];
elmts = splitRslt[2];
molecules = splitRst[3];
} else {
alert("Please enter a valid save code.");
}
}
}
【问题讨论】:
标签: javascript base64