addFrameCallback

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

Post Reply
DGill
---- E L I T E ----
---- E L I T E ----
Posts: 292
Joined: Thu Jan 01, 2009 9:45 am

addFrameCallback

Post 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?
User avatar
phkb
Impressively Grand Sub-Admiral
Impressively Grand Sub-Admiral
Posts: 4830
Joined: Tue Jan 21, 2014 10:37 pm
Location: Writing more OXPs, because the world needs more OXPs.

Re: addFrameCallback

Post 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.
DGill
---- E L I T E ----
---- E L I T E ----
Posts: 292
Joined: Thu Jan 01, 2009 9:45 am

Re: addFrameCallback

Post by DGill »

Thanks phkb, the code looks interesting - I'll give it a go
Post Reply