KineticJS - moving sprites causing unknown error

I'm trying to write code in KineticJS to move unit represented by a group which contains its sprite (it's a group since I might want to add more things to the graphical representation).

The code for the movement function is:

this.moveTo = function(x,y) {
distance = Math.sqrt((this.shape.getX() - x)*(this.shape.getX() - x)+(this.shape.getY() - y)*(this.shape.getY() - y));
time = distance / MOVEMENT_SPEED;
var sprite = this.sprite;
this.sprite.setAnimation('walkDown'); //TODO: change{
        x: x,
        y: y,
        duration: time,
        easing: "ease-in-out",
        callback: function(){

Sin la linea sprite.setAnimation('idle'); everything works well. With this line everything still works as expected most of the time (i.e. the animation stops after the movement has ended) but sometimes the sprite disappears altogether and I get the following cryptic error message:

Error: d is undefined" in line 29 in kinetic-v3.9.8.min.js

For the purpose of testing, right now moveTo is called by clicking on the mouse on the screen. I thought it might have something to do with me clicking the mouse before the previous movement is over, but sometimes the error occurs even when I'm careful not to do it.

EDIT: The error occurs most of the time when I click near the edge of the stage and the movement order orders the unit "out" of it. Still, nothing bad happens unless I change the sprite animation and I don't understand the connection. Also, I managed to reproduce the error without clicking anywhere near the border, it's only more rare.

preguntado el 12 de junio de 12 a las 21:06

1 Respuestas

Solved. This had nothing to do with the movement, and like most of my bugs, it's very embarrassing. I simply forgot to use sprite.setIndex() when changing the animation to 'idle' which only contains one frame, as opposed to 'walkDown'.

Bottom line: sprite.setIndex() is important.

Respondido el 13 de junio de 12 a las 06:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.