开发者

C# or AS3 Custom world time

I'm wondering how can I create开发者_如何学JAVA custom world time for a game.

like one hour = 30 minutes, 1 year = 6 months

6 custom months

I'm stucked, and I have no clue how to do it.

I known I can use timer event.


Say your main game loop is something like:

this.addEventListener(Event.ENTER_FRAME, gameLoop, false, 0, true);

var _timer = 0; // starts as zero
const WORLD_TIME_FACTOR:Number = 2;

function gameLoop(e:Event):void {
    // all your game logic happens here

    _timer++;
}


function getWorldTime():Number {
    return (_timer/stage.frameRate) * WORLD_TIME_FACTOR;
}

// later (you can adjust WORLD_TIME_FACTOR to be anything, 2 would indicate that the 
// game world happens twice as quick as in real life (like your examples)
// 

trace(getWorldTime()); // At 3 seconds real world, would trace 6

trace(getWorldTime()); // At 33 seconds real world, would trace 66

You would then format this number into minutes with something like:

const div60:Number = 1/60;
const div120:Number = 1/60/60;     
function format_time(value:Number):String {
    // x >> 0 is the same as int(x) but quicker
    var minutes:String = String((value* div60 >> 0) % 60);
    minutes = (minutes == "60") ? "00" : minutes;  
    minutes = (minutes.length == 1) ? "0" + minutes : minutes;

    var seconds:String = String(value% 60);
    seconds = (seconds.length == 1) ? "0" + seconds : seconds;

    return (value* div120 >> 0) + ":" + minutes + ":" + seconds;
}

So finally you would have

 trace(format_time(getWorldTime())); // At 72 (1 minute, 12 secs) seconds real world, would trace 00:02:24


The following code will give you the milliseconds that passed since the epoch Jan,1 1970
Any call to this function afterwords would give you a time in milliseconds.
Subtract that from the original time and you have how many milliseconds that have passed.
Knowing that you can now factor in your conversion. EX 6 seconds = 1 minute

var startingTime:Number = Date.parse(new Date( ));
trace(startingTime);


you'll have to remember the timestamp and in-game date of the moment when your in-game time started, and then to calculate in-game date for given moment:
milliseconds_of_double_time = moment.timestamp - start_timestamp
ingame_date = date_from_timestamp(start_date.timestamp + milliseconds_of_double_time * 2)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜