Ja, auf den ersten Blick scheint es komplizierter zu sein, aber wenn man sich an die Denkweise von jCanvas gewöhnt hat, ist es einfacher als mit Timern oder animationFrame.
Ich habe mal das Skript levels.js umgearbeitet und jetzt läuft der Bloon einwandfrei und geschmeidig. Auch wenn ich die Konfiguration auf zwei Bloons erweitere, funktioniert es:
function levels() {
level++;
allLevels();
console.log($('canvas').getLayers());
console.log('klappt');
(function () {
var intv = 500;
var h = 1;
var speed = 0.1;
function getDuration(v, x1, y1, x2, y2) {
var t = Math.hypot(x2 - x1, y2 - y1) / v;
return t;
}
for (let idx1 = 0; idx1 < whichBloons.length; idx1++) {
function animateIt(layer) {
console.log("ani")
idxAni++;
$('canvas').animateLayer(layer, {
x: mapValues[idxAni].x,
y: mapValues[idxAni].y
}, getDuration(speed,
mapValues[idxAni - 1].x, mapValues[idxAni - 1].y,
mapValues[idxAni].x, mapValues[idxAni].y),
'linear',
function (layer) {
animateIt(layer)
}
);
}
let idx2 = 0, idxAni = 0;
setTimeout(function () {
var bloonName = whichBloons[idx1] + h;
$('canvas').addLayer({
type: 'image',
name: bloonName,
data: {
bloonType: 0
},
groups: ['bloons', 'redBloons'],
source: 'redBloon.png',
x: mapValues[0].x,
y: mapValues[0].y
});
animateIt($('canvas').getLayer(bloonName));
console.log("create bloon " + whichBloons[idx1] + " " + bloonName);
h++;
idx2++;
}, timeLapse[idx1] * 1000);
}
})();
function startButton() {
}
return;
}
Alles anzeigen
Ich bin der Meinung, dass es relativ kompakt ist, man muss sich nur an den rekursiven Callback gewöhnen, aber der wird häufig verwendet, auch bei Animationen mit jQuery,