看博客学学Android(二十二)

原文地址:

Day 20: Weekly challenge. Persistent player data. Java date woes.

Weekly challenge will be to collect some amount of stars during a week and get some nice reward, like 8 atomic bombs, 5 shields or similar. I made a nice gold star in Gimp. I tried different particle effects on it and also some diffused star-light, but it did not look really good. So I went back to the particle effect used for powerups and tweaked that until I got something distinct for the star. Stars show up on their own pacing, so you can have both a star and a powerup on the screen at the same time.

Weekly Challenge

I also worked on loading and saving player data. It was much easier than I expected. I expected to have to learn some Android data storage API, but for simple key-value storage, libGDX provides the Preferences class. Just init with:

Preferences prefs = Gdx.app.getPreferences("DroneInvaders");

and then use get("key", defaultValute) and set(key, value) to read and write the values.

The only thing I had problems with are the dates. To keep track of daily and weekly challenges, the game stores the date of last play. When player launches the game, it compares that and resets some counters. Theoretically, I could prevent players from changing the system's calendar to past date, but I don't want to. What I am doing, is setting new set of daily and weekly challenges when day rolls over and resetting the counters for number of stars and letters picked up.

To make that work, I had to get the day difference between the previous play and current date. It matters whether it's the same day, exactly one day apart or more than one day. Googling got me to various websites and StackOverflow questions. Answers are funny. Many programmers simply calculate difference in seconds and then divide by 60 * 60 * 24 to get the days, completely ignorant about issues with daylight savings and leap seconds. One could argue that it does not matter that much for a game, but still getting many bug reports twice a year doesn't sound like fun to me. Some other guys simply count the days by adding one-by-one from start until they reach the end. While those loops might look correct, they still miss sometimes as they do not take the time out of the calculation. If one object stored 01.Jan at 5am, and you calculate it vs 02.Jan at 23pm, adding one day to first object is still less than the second, so they add two and get 2 day difference.

One of the tricks I use in this situation is to always set the time of the earlier Date to be something like 10am, and set the time for the later Date object to 5pm. Since daylight changes always happen at night, this is safe. And we also have 7 hours in between, just in case someone in future decides to do daylight saving changes in the middle of the day.

 

第20天:周挑战。可持续的用户数据。Java日期问题

每周的挑战将是收集这一周中的一些星星并且获得不错的奖赏,像8个原子弹,5个盾牌或者类似的东西。我使用Gimp制作了一个很好看的星星。我尝试在它上面是用不同的微粒效果并且也进行一些星光的扩散效果,但是效果真的不好.所以我重新换回使用微粒效果并且一直调整到星星很明显。星星以一个固定的速度显示出来,所以在屏幕上你可以同时看到一个星星和一个能量条。

Weekly Challenge

同时我也在进行游戏数据的加载和保存的工作。它要比我想的要容易很多。我还以为要去学习一些安卓的数据存储API,但是libGDX提供了一个接口类来存储简单的的键值对.仅仅需要如此初始化:

Preferences prefs = Gdx.app.getPreferences("DroneInvaders");

然后使用get("key", defaultValute) and set(key, value)的方式来读取和写入数据。

对我来说唯一的问题是日期。为了保持对日常挑战和周挑战的追踪,游戏存储了上一次玩的日期。当用户运行游戏,它会进行比较并且重置一些计数器。理论上,我能够阻止玩家将系统日历改为之前的日期,但是我不想这么做。我能做的是设,当日期回滚后,设置一个新的日常挑战周挑战并且重置星星和捕获的星星的数量。

为了如此,我需要获得之前游戏的日期和当前日期的不同。重要的是无论是否是同一天,都能精确的分辨出是不是超过了一天。通过谷歌查找了各种网站和一些论坛提问。答案是很有有趣的。许多的程序员只是简单的来计算相差的秒数,然后通过除以60*60*24来获得天数,完全忽略了夏时制和闰秒。有人会说这对游戏来说无关紧要,但是一年两次的获得很多bug报告对我来说是不好的事情。另外的一些童鞋只是简单通过从开始一个一个的加到结束来统计天数。尽管他们的循环看起来很正确,但是他们依旧遗忘了一些东西,比如他们不能控制时间溢出。如果一个一个对象存储的是1月1日上午5点,然后和1月2日晚上23点进行比较,给第一个对象增加一天依旧会小于第二个对象,所以他们会增加2天,最后获得2天的差距。

我在这里使用的一个策略是一直设置早一点的时间在10点左右,然后设置晚一点的时间在下午五点左右,因为夏时制一个发生在晚上,所以这样是安全的。并且在这之间我们依旧有7个小时,仅仅是为了预防万一在将来夏时制发生在中午.

涨姿势:

夏时制(Daylight Saving Time:DST):又称“日光节约时制”和“夏令时间”,是一种为节约能源而人为规定地方时间的制度,在这一制度实行期间所采用的统一时间称为“夏令时间”。一般在天亮早的夏季人为将时间提前一小时,可以使人早起早睡,减少照明量,以充分利用光照资源,从而节约照明用电。各个采纳夏时制的国家具体规定不同。目前全世界有近110个国家每年要实行夏令时。 自2011年3月27日开始俄罗斯永久使用夏令时,把时间拨快一小时,不再调回。

闰秒:是指为保持协调世界时接近于世界时时刻,由国际计量局统一规定在年底或年中(也可能在季末)对协调世界时增加或减少1秒的调整。由于地球自转的不均匀性和长期变慢性(主要由潮汐摩擦引起的),会使世界时(民用时)和原子时之间相差超过到0.9秒时,就把世界时向前拨1秒(负闰秒,最后一分钟为59秒)或向后拨1秒(正闰秒,最后一分钟为61秒); 闰秒一般加在公历年末或公历六月末。2012年3月,中科院国家授时中心宣布我国7月1日进行闰秒调整,届时将现7:59:60。

 

 

转载于:https://www.cnblogs.com/NorthDrift/p/3440520.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值