Wie können wir eine Variable als Funktion verwenden. Im Anschluss an mein CodeMember-Funktion in Javascript
$(document).mouseup(function (evt) {
balls.push(new ball(mousePos["downX"],
mousePos["downY"],
5 + (Math.random() * 10), 0.9, randomColor()));
});
function ball(positionX, positionY, radius, color) {
this.px = positionX;
this.py = positionY;
this.rad = radius;
this.clr = color;
this.draw = drawFun(this.px, this.py, this.rad, this.clr);
}
function drawFun(x, y, r, c) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = c;
ctx.fill();
//stroke
ctx.lineWidth = r * 0.1;
ctx.strokeStyle = "#000000";
ctx.stroke();
}
for (var i = 0; i < balls.length; i++) {
//TODO: DRAW ALL BALLS
balls[i].draw;
}
Jetzt möchte ich ball[i].draw;
verwenden, aber in der Konsole sagt es, dass Draw nicht definiert ist. Wie kann ich drawFun Zugriff von ball[i]
Bevor MouseUp passiert, wie erhalten Sie Zugriff auf diese Kugeln Array? –
Ja, ich spreche nach dem MouseUp. Balls array bekommt Bälle in mouseUp und drawFun wird einmal ausgeführt, aber ich will es auch in for loop ausführen –
'this.draw = drawFun (this.px, this.py, this.rad, this.clr) ; 'erstellt keine Funktion. Sie brauchen etwas wie 'this.draw = function() {....}' oder lassen Sie drawFun eine Funktion zurückgeben oder die Funktion sein: this.draw = drawFun; – mplungjan