使本地存储计数缓慢上升
Posted
技术标签:
【中文标题】使本地存储计数缓慢上升【英文标题】:Make Local storage count slowly go up 【发布时间】:2021-05-06 05:41:33 【问题描述】:我正在尝试制作一款类似于流行的在线游戏 Cookie Clicker 的游戏,或者说实话只是抄袭。我的游戏实际上是 Cursor Clicker,我对编码有点陌生,我只是为学校做点东西,因为我们的 chromebook 上几乎所有东西都被屏蔽了。我真的需要帮助进行升级,它会带走 10 个游标,并且游标计数每增加一次(秒数)。提前致谢!
//Tells user/player that page is still in beta.
alert("Cursor Clicker is still in beta so if there are any bugs,glitches, or suggestions please let me know.\n Here: https://php-Contact-Form.michaelmatherne.repl.co")
//Sets the cursor image so when clicked, Cursors are added.
function clickCounter()
if (typeof(Storage) !== "undefined")
if (localStorage.clickcount)
localStorage.clickcount = Number(localStorage.clickcount)+1;
else
localStorage.clickcount = 1;
document.getElementById("result").innerhtml = "Cursors: " + localStorage.clickcount;
else
document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
//Disables CTRL+U
document.onkeydown = function(e)
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117))
alert('Sorry but i have disabled CTRL+U. \nLook at the link displayed below for help getting started. \n\nhttps://www.w3schools.com/html/tryit.asp?filename=tryhtml5_webstorage_session');
return false;
else
return true;
;
//Disables right click content-menu
document.addEventListener("contextmenu", function(e)
e.preventDefault();
, false);
//Clear all Cookies
function clear()
localStorage.clear();
//Alert for Cursor Reset
function reset()
alert("Cursors have been reset!")
//trash
/*function RightClick()
if(RightClick(Sell))
function Sell()
var x = -1;
x.tostring();
(-1).tostring();
(100-1).tostring();
*/
html
background-color: #181818;
body
color: lightgray;
text-align: center;
font-family: monospace;
font-size: 30px;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Cookie Clicker Remake But With Cursors :)">
<meta name="keywords" content="game, fun, clicker">
<title>Cursor Clicker</title>
<!-- favicon -->
<link rel="shortcut icon" href="https://cdn.glitch.com/e991135e-1ef8-4944-b9c8-e4bf9682315f%2Fb2f27baa-8b5f-4aca-9c22-73cfe6ae0ae7.image.png?v=1612226466065">
<!-- import the webpage's stylesheet -->
<link rel="stylesheet" href="/style.css">
<!-- import the webpage's javascript file -->
<script src="/script.js" defer></script>
</head>
<body>
<h4>
Click the Cursor to earn Cursors...Buy upgrades with Cursors to earn more Cursors
</h4>
<img src="https://cdn.glitch.com/e991135e-1ef8-4944-b9c8-e4bf9682315f%2Fec4700a9-10fc-474a-a6c3-9fdfc278dbfe.image.png?v=1612201917876" onclick="clickCounter()">
<div id="result"></div>
<br>
<button onclick="localStorage.clear(), reset()">
Reset All Cookies
</button>
</body>
</html>
【问题讨论】:
请发帖minimal reproducible example。您可以使用Stack Snippet 使其可执行。 代码应该发布在这里,而不是在未来可能会改变的外部网站上。 你是这个意思吗? 【参考方案1】:这个基本示例应该说明如何做到这一点。你想用setInterval()
每秒连续加分。
var button = document.getElementsByTagName("button")[0];
var counter = document.getElementById("cursors");
function update()
counter.innerHTML=parseInt(counter.innerHTML)+1;
function achievement(points, reduction, interval)
counter.innerHTML=parseInt(counter.innerHTML)-reduction;
setInterval(function()counter.innerHTML=parseInt(counter.innerHTML)+points;, interval);
<button onclick="update()">Click</button><br>
<button onclick="achievement(10, 10, 1000)">Achievement #1 (10 ps)</button><br>
Cursors: <p id="cursors">0</p>
注意:我没有在示例中使用 LocalStorage
,因为 Stack Snippets 不支持它。
【讨论】:
是的...我不知道如何使它进入本地存储...以上是关于使本地存储计数缓慢上升的主要内容,如果未能解决你的问题,请参考以下文章