Page 1 of 1

addFrameCallback

Posted: Thu Jul 02, 2020 12:45 pm
by DGill
How would I use addFrameCallback to speed up animation

I have:

this.shipWillLaunchFromStation = function()
{
if (this._radarTimer)
this._radarTimer.start()
else
this._radarTimer = new Timer(this, this._radarCheck, 0.25, 0.25)
this.$radar = 0;
}


this._radarCheck = function()
{
this.$radar += 1;
if(this.$radar == 25) {this.$radar = 0}
this.$Hudchart = ("P" + this.$radar + ".png");
player.ship.setCustomHUDDial("fhud1", this.$Hudchart);
}

but the animation is not smooth; I believe addFrameCallback would be better but cannot get the code to work - any suggestions?

Re: addFrameCallback

Posted: Thu Jul 02, 2020 9:03 pm
by phkb

Code: Select all

this.shipWillLaunchFromStation = function() {
    this._setupRadarCheck();
}

this.shipDied = this.shipWillDockWithStation = function() {
     this._stopFCB();
}

this._stopFCB = function _stopFCB() {
    if (isValidFrameCallback(this.$fcb)) stopFrameCallback(this.$fcb);
}

this._setupRadarCheck = function _setupRadarCheck() {
    this.$fcb = addFrameCallback(function (delta) {
        var w = worldScripts.MyWorldScriptName;
        w.$radar += 1;
        if(w.$radar == 25) {w.$radar = 0}
        w.$Hudchart = ("P" + w.$radar + ".png");
        player.ship.setCustomHUDDial("fhud1", w.$Hudchart);
    });
}
That's a pure conversion of what you have to a frame callback, but I think you'll need some sort of interlacing check inside the frame callback function. What I mean is, you could accumulate the deltas and only do a change to $radar after a given number of milliseconds have passed.

You should also only do the setCustomHUDDial when required with this methodolgy. That is, if you're not going to increment $radar, then don't do the rest of the function.

Re: addFrameCallback

Posted: Thu Jul 02, 2020 10:11 pm
by DGill
Thanks phkb, the code looks interesting - I'll give it a go