0

I'm working on a board game with kineticjs and I'm facing a problem with mouse handling.

I have an image with a mousemove event that draw a preview selection.

The problem is with the click event on the same image that is firing only 1 on 9 times..

If I remove the on_mousemove the on_click is working perfectly..

Anyone has an explanation and/or workaround ?

Thanks


Edit

Fiddle link: http://fiddle.jshell.net/gTuCE/12/


Davidet
  • 3
  • 3
  • So do you have to click on the image to start drawing the preview selection? Or you just mousemove over the image and it will start drawing? Need more details and it would be much easier to help if you showed us some code or a jsfiddle! – projeqht Aug 02 '13 at 14:20
  • 1
    I added a fiddle example – Davidet Aug 02 '13 at 14:48

1 Answers1

0

Your code was a bit messy so I cleaned it up and it's working now: jsfiddle

  1. In your function createSquareItem(), squareLayer was undefined.

  2. You only have to add Kinetic.Layers once. In both your createSquareItem createTemporarySquareItems functions, you were adding mainLayer to the stage every time you call the function. This is wrong since you already added mainLayer to the stage earlier.

  3. In your createTemporarySquareItems function, I had to add mainLayer.drawScene() and in createSquareItem I had to add mainLayer.draw()

    function createSquareItem(point, color) {
        var boardPoint = point;
    
        var rect = new Kinetic.Rect({
            x: boardPoint.x,
            y: boardPoint.y,
            width: 18,
            height: 18,
            fill: color,
            stroke: 'black',
            strokeWidth: 0
        });
    
        mainLayer.add(rect);
        //squareLayer.drawScene(); //squareLayer is undefined.
        mainLayer.draw();
    }
    
    function createTemporarySquareItems(point, cleanLayer) {
        if (cleanLayer == true) {
            do {
                var r = tempSquareArray.pop();
                if (r != null) {
                    //tempSquareLayer.remove(r);
                    r.destroy();
                }
            } while (r != null);
        }
    
        var boardPoint = point;
    
        var rect = new Kinetic.Rect({
            x: boardPoint.x,
            y: boardPoint.y,
            width: 18,
            height: 18,
            fill: 'orange',
            stroke: null,
            strokeWidth: 0
        });
    
        tempSquareArray.push(rect);
    
        mainLayer.add(rect);
        mainLayer.drawScene();
    }
    

For more information on KineticJS' draw methods, see here: What is the difference between KineticJS draw methods?

Community
  • 1
  • 1
projeqht
  • 3,160
  • 3
  • 18
  • 32