Extend Tumult Hype with JavaScript: hypeDocument.extensions

↑ extension index


hypeDocument.startCustomBehaviourTicker

Hype Pro Extension: Starts a ticker based on a time interval that broadcasts a custom behaviour. You can specify the interval in seconds or pass a object containing the FPS and the extension calculates it for you. Beware these tickers live across scenes so start any given ticker once and stop him if not needed. But we make sure that no behaviour ticks twice if you repeat the command call.


/**
* hypeDocument.startCustomBehaviourTicker 1.1
* @param {String} behaviour name to fire
* @param {Number} time in seconds (can be fractional)
* @param {Object} Some optional settings like pattern
*/
hypeDocument.startCustomBehaviourTicker = function(behaviour, time, opt){
    var fnc;
    if (behaviour==null || time==null) return;
    if (!this.hasOwnProperty('_ticker')) { this._ticker={} }
    if (!this._ticker.hasOwnProperty(behaviour)) {
        var interval = (time.hasOwnProperty('FPS')) ? 1000/time.FPS : time*1000;
        opt = opt ? opt : {};
        if (opt.hasOwnProperty('pattern')) {
            opt._buf = opt.pattern.slice(0);
            if (opt.countdown==null) opt.countdown = Infinity;
            fnc=function(){
                if (opt._buf.length==0) opt._buf = opt.pattern.slice(0);
                if (opt._buf.shift()) hypeDocument.triggerCustomBehaviorNamed(behaviour);
            }
        } else {
            fnc=function(){
                hypeDocument.triggerCustomBehaviorNamed(behaviour);
            }
        }
        this._ticker[behaviour] = setInterval(fnc,interval);
        if ( !opt.omitFirst) fnc();
    }
}

Usage:
Let’s broadcast a behaviour called enterFrame at 60 FPS

hypeDocument.startCustomBehaviourTicker('enterFrame', {FPS:60});


Let’s broadcast a behaviour called redSquareToggle every second

hypeDocument.startCustomBehaviourTicker('redSquareToggle', 1);

The function supports tick pattern since version 1.1

hypeDocument.startCustomBehaviourTicker('bounceBall', 2, {pattern:[1,1,0]});

The function supports omitFirst since version 1.1 to wait one interval before start

hypeDocument.startCustomBehaviourTicker('turnClock', 1, {omitFirst:true});


startCustomBehaviourTicker.hype.zip (82,5 KB)

Updates:
1.0 initial release
1.1 code cleanup, added pattern and omitFirst options

1 Like