2016-10-19 2 views
0

Ich kann einfach nicht herausfinden, warum meine Zeichenfunktion nicht definiert ist. Es hat etwas mit dem Prototyp zu tun. Grundsätzlich möchte ich, dass jedes neu erstellte Objekt sein eigenes x, y, dx und dy hat, so dass ich mehrere springende Bälle mit Objekten erstellen kann, und ich dachte mir, dass die Verwendung von Prototypen Methoden für jedes Objekt zu erstellen wäre.JavaScript - Prototyp-Methode ist undefined

document.addEventListener("DOMContentLoaded", function(event){ 
var canvas, 
    context, 
    altezza, 
    larghezza, 
    x = 100, 
    y = 100, 
    raggio = 25, 
    dx = 5, 
    dy = 5, 
    immagine, 
    ballTest; 

    window.onload = init; 

    //Assegniamo a canvas l'elemento HTML canvas 
    canvas = document.getElementById("campo"); 
    //Assegniamo a context un oggetto che contiene tutti i metodi per disegnare 
    context = canvas.getContext("2d"); 
    //Assegniamo a "dy" una velocità iniziale di 0.1 che verrà incrementato nel tempo 
    dy = 0.1; 
    //Assegniamo a immagine l'immagine della sfera 
    immagine = new Image; 
    immagine.src = "tBall.png"; 
    //Assegniamo alle variabli altezza e larghezza le dimensioni del campo 
    larghezza = window.screen.availWidth - 40; 
    altezza = window.screen.availHeight - 120; 

    function Entity(x, y, raggio){ 
     this.x = x; 
     this.y = y; 
     this.raggio = raggio; 
     draw(); 
    } 

    Entity.prototype.draw = function(){ 
     context.clearRect(0, 0, context.canvas.width, context.canvas,length); 
     context.drawImage(immagine, x, y, raggio, raggio); 
    }; 

    //MIGHT NEED TO PUT THIS.X AND THIS.Y 
    Entity.prototype.move = function(dx, dy){ 
     if(this.x < -10 || this.x > larghezza - 20){ 
      dx *= -1; 
     } 
     if(this.y < 20 || this.y > altezza - 40){ 
      dy *= -1; 
     } 

     this.x += dx; 
     this.y += dy; 
    }; 

    function init(){ 
     //Assegniamo a canvas l'elemento HTML canvas 
     canvas = document.getElementById("campo"); 
     //Assegniamo a context un oggetto che contiene tutti i metodi per disegnare 
     context = canvas.getContext("2d"); 
     //Settiamo le dimensioni del campo di canvas 
     context.canvas.width = larghezza; 
     context.canvas.height = altezza; 
     //Inizializziamo l'entità 
     ballTest = new Entity(100, 100, 25); 
     setInterval(ballTest.move.bind(dx, dy), 10); 
    } 
}); 

ERROR: Uncaught Reference: wird Unentschieden nicht

+1

@mplungjan 'bind' gibt eine Funktion zurück, also würde es in setInterval übergeben, aber das erste Argument scheint falsch zu sein, da es' this' Kontext sein sollte. – pawel

+0

@mplungjan Wie kommt es nicht zu einer Bindung? –

+0

bitte posten Sie ein komplettes Protokoll des Fehlers – Karim

Antwort

0

Die draw Funktion ist im Prototyp von Entity so innerhalb der Entity Klasse definiert man es als this.draw() zugreifen müssen.

function Entity(x, y, raggio){ 
     this.x = x; 
     this.y = y; 
     this.raggio = raggio; 
     this.draw(); 
    } 
+0

Vielen Dank! :) –