开发者

How to create a function like 'build building' like in Ikariam (MMORPGs), that keeps counting when the user isn't on the website

I've been developing games and websites for 开发者_运维百科the past two years, and really love it.

Some time ago I started playing Ikariam (and OGame). These are MMORPGs. You can create your own city by building for example Barracks. BUT, when you click the button to build such thing, it starts counting from for example '10 minutes'.

Then, when you go offline/leave the website, it keeps counting down!

And I was wondering: How/using which coding language do you achieve such thing? (How do you create a function that keeps getting called via intervals on the server, so the user doesn't need to stay online?)

Thanks in advance, Dalionzo

P.S. I have a good understanding of JavaScript, PHP, HTML and CSS. I first thought of PHP to do the job, but couldn't figure out how. I also already asked the same question to Ikariam and Gameforge itself, but they said they couldn't provide me that info :(


Rather than constantly running something, just check when the player is online. Like if the current time is after completion time, the building is constructed.

The countdown is pure javascript, no server contact at all. When javascript thinks the countdown has finished, it just refreshes the page which triggersthe server side script that checks construction time.


It's quite simple really, you save the time the countdown started, and then you know that in that time + 10 minutes (600 seconds) the job will be complete. No need to keep pinging a server or anything like this.

IE in PHP:

// On start
$start = time();
$finish = $start + 600;

Save this to a database and then later on you know if time() is between these values it's still building, and it's trivial to work out how much time is left.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜