Ich war gerade ein wenig dabei mit JavaScript und dem Canvas-Element zu spielen, als ich folgende Fehlermeldung bekam:
"Maximum call stack size exceeded"
Nach einer kurzen Suchanfrage weiß ich nun, dass dies daher rührt, dass eine rekursive Funktion zu oft aufgerufen wird.
Ich dachte aber genau für viele Animation/Positionsänderungen sei window.requestAnimatonFrame geeignet
Würde mich freuen , wenn jemand einen Tipp kennt um dies zu umgehen
Hier noch der Code dazu:
JavaScript
function Spielfeld() {
//Welt
this.test = document.getElementById('test');
this.ctx = this.test.getContext("2d");
//Objekte
this.ball = new Ball("white", 5);
//Fenstergröße erkennen
this.getSize = function() {
var width = window.innerWidth - 50;
var height = window.innerHeight - 50;
return [width, height];
};
//Spielgeschehen behandeln
this.uptade = function() {
this.size = this.getSize();
this.test.width = this.size[0];
this.test.height = this.size[1];
this.ctx.fillStyle = "black";
this.ctx.fillRect(0, 0, this.size[0], this.size[1]);
this.ball.uptade();
requestAnimationFrame(this.uptade());
};
requestAnimationFrame(this.uptade());
}
function Ball(color, diameter) {
//Unveränderlich
this.color = color;
this.r = diameter / 2;
//Veränderbar
this.world = document.getElementById('test');
this.ctx = this.world.getContext("2d");
//Position:
this.x = 50;
this.y = 50;
//Geschwindigkeit:
this.vx = 3;
this.vy = 6;
//Methoden
this.draw = function() {
//Ball zeichnen
this.ctx.beginPath();
this.ctx.fillStyle = this.color;
this.ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
this.ctx.fill();
};
this.uptade = function() {
//Änderungen in der Umgebung behandeln
this.world = document.getElementById('test');
this.ctx = this.world.getContext("2d");
//Neue Spielgröße speichern
var size = [this.world.height, this.world.width];
//Kollision mit dem Rand?
if (this.y >= size[1]) {
this.y = size[1];
this.vy = -this.vy;
} else if (this.y <= 0) {
this.y = 0;
this.vy = -this.vy;
}
if (this.x >= size[0]) {
this.x = size[0];
this.vx = -this.vx;
} else if (this.x <= 0) {
this.x = 0;
this.vx = -this.vx;
}
//Position ändern
this.x += this.vx;
this.y += this.vy;
//Ball neu zeichen
this.draw();
};
}
function init() {
var Spiel = new Spielfeld();
Spiel.uptade();
}
Alles anzeigen
mfg Nita