如何在html游戏中保存进度 [英] How to save progress in an html game

查看:254
本文介绍了如何在html游戏中保存进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何挽救球员在我制作的比赛中所取得的进步。我可以通过cookies做到这一点吗,或者我还可以保存到玩家电脑吗?感谢您的所有帮助!

解决方案

您几乎有两种选择, 和 localStorage



使用Cookies:



<$> document.cookie 属性用于设置,读取,更改和删除浏览器cookie。




  • 要设置一个cookie, document.cookie =cookiename = Foo Bar; ,或者如果你想要一个过期日期: document.cookie = cookiename = Foo Bar; expires = Mon,2016年1月18日12:00:00 UTC;

  • 要读取cookie,只需输入 document.cookie 会在该页面上返回所有网站的Cookie。它不会将它们放入数组中,而是使用 cookiename = foobar格式的单个字符串; nextcookiename = foobar; 等。您可以使用 document.cookie.split(;);

  • 要更改Cookie,只需按上述方法重新设置即可,因为它会覆盖它。

  • 要删除一个cookie,请将其重新设置为过去。这将覆盖它,然后它将过期,删除它。



使用localStorage:



新的HTML5 localStorage Object是本地存储数据的另一种方式:


  • 要在localStorage中设置项目,请使用 localStorage.setItem('itemname','contents');

  • 一个项目,它是 localStorage.getItem('itemname'); 。您可以使用truthy值检查项目是否存在(即 if(localStorage.getItem('itemname'))

  • 如上所述,您可以使用 localStorage.setItem 来修改localStorage项目。

  • 您可以使用 localStorage.removeItem('itemname')。



我应该使用哪个?



几乎所有您可以想到的浏览器都支持Cookie,但它们会过期(它们会在一段时间后被删除),并且可能会被用户禁用。就我个人而言,我也发现 document.cookie 一个笨拙的界面。另一方面,localStorage不能轻易禁用用户,并为程序员提供更易于访问的界面。据我所知,localStorage没有到期。由于localStorage是HTML5的新增功能,因此它可能不适用于某些较旧的浏览器(但它在新浏览器上的覆盖范围很广,请参阅 http://caniuse.com/#feat=namevalue-storage )。请注意,有关限制将数据存储在您的整个网站,而不仅仅是一件商品。



最后,这取决于您。选择一个你认为最适合你的游戏 - 如果你已经使用其他HTML5内容(例如< canvas> ),那么localStorage而且您将使用更可靠的存储方法。但是,如果您对Cookie感到满意,那么它们是成千上万非常受欢迎的网站使用的完美可行选项 - 有些甚至可以同时使用! cookie的一个好处是它们可以被你的web服务器访问,而localStorage不能。



无论哪种方式,你都需要检查 cookie law ,它通过网络应用程序影响用户计算机上的所有类型的存储。


I want to know how I can save the progress a player has made in a game that I am making. Could I do this through cookies or how else can I save to the players computer? Thanks for all of the help!

解决方案

You have pretty much two options for saving localy using Javascript, which are cookies and localStorage.

With Cookies:

The document.cookie property is used for setting, reading, altering and deleting browser cookies.

  • To set a cookie, document.cookie="cookiename=Foo Bar";, alternatively like so if you want an expiry date: document.cookie="cookiename=Foo Bar; expires=Mon, 18 Jan 2016 12:00:00 UTC";
  • To read a cookie, just the code document.cookie will return all of your site's cookies on that page. It doesn't put them in an array, but instead a single string in the format of cookiename=foobar; nextcookiename=foobar;, etc. You can turn this into an array easily using document.cookie.split("; ");
  • To alter a cookie, simply set it again as described above, as that will overwrite it
  • To delete a cookie, set it again with an expiry date in the past. This will overwrite it, and then it will expire, deleting it.

With localStorage:

The new HTML5 localStorage Object is another way to store data locally:

  • To set an item in localStorage, use localStorage.setItem('itemname','contents');
  • To read an item, it's localStorage.getItem('itemname');. You can check if an item exists using "truthy" values (i.e. if(localStorage.getItem('itemname')))
  • You can alter a localStorage item using localStorage.setItem as described above.
  • You can delete a localStorage item using localStorage.removeItem('itemname').

Which should I use?

Cookies are supported in just about any browser that you can think of, however they expire (they get deleted after a set amount of time) and also can be disabled by users. Personally, I also find document.cookie a clunky interface.

localStorage on the other hand cannot be easily disabled by the user, and provides a more accessible interface for the programmer. As far as I'm aware, there is no expiration for localStorage. Since localStorage is new with HTML5, it may not work in some older browsers (however it's got great coverage on new browsers, see http://caniuse.com/#feat=namevalue-storage). Note that there is a limit for storing data on your entire site, not just for one item.

In the end, it's up to you. Pick the one you think is going to work best for your game - if you're already using other HTML5 content (such as <canvas>) then there's no harm in localStorage and you'll be using a more reliable storage method. However, if you're happy with cookies then they are a perfectly viable option used by thousands of extremely popular sites - some even use both! One advantage to cookies is that they can be accessed by your web server, whereas localStorage cannot be.

Either way, you'll need to check out the cookie law, which effects all types of storage on the user's computers by a web app.

这篇关于如何在html游戏中保存进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆