0
k=0;
var sf = new Array();
function draw_sf (type,sf_text,schriftgroesse,sf_width,sf_height,x0,y0,ports_top,ports_right,ports_bottom,ports_left) {
    sf[k] = new Kinetic.Group({
        draggable: true
    });

    sf[k]['x0']=x0;
    sf[k]['y0']=y0;

    m=0;
    sf[k][m] = new Kinetic.Rect({
        x: x0,
        y: y0,
        width: sf_width,
        height: sf_height,
        fill: '#EEE',
        stroke: '#000',
        strokeWidth: randbreite_sf
    });
    sf[k].add(sf[k][m]);
    m++;


    sf[k].on('dragend', function() {
        var dx=parseFloat(document.getElementsByName("dx")[0].value);
        var dy=parseFloat(document.getElementsByName("dy")[0].value);
        sf[k]['x']=sf[k]['x0']+dx;
        sf[k]['y']=sf[k]['y0']+dy;
    });
    boxLayer.add(sf[k]);
    k++;
}

My function draw_sf() draws rectangles, which can be moved my drag&drop. After being droped (event handler 'dragend') i want to save the new position of the element in an array. But my counter variable 'k' doesnt count in the dragend-function. k is always the number of times i called the draw_sf(). So how can i save the actual positions of my rectangles?

1
  • At a quick glance, try adding var before you declare k. I'm not sure what browser youre using, but I recall javascript treating it like a new variable if you dont declare it in an older version of FF, not sure these days as I always declare.
    – half-fast
    May 5, 2013 at 16:28

1 Answer 1

0

Consider changing the approach a bit into something like:

var sf = new Array();
function draw_sf(type, sf_text, schriftgroesse, sf_width, sf_height, x0, y0, ports_top, ports_right, ports_bottom, ports_left) {
  var kineticGroup = new Kinetic.Group({
    draggable:true
  });

  kineticGroup['x0'] = x0;
  kineticGroup['y0'] = y0;

  m = 0;
  kineticGroup[m] = new Kinetic.Rect({
    x:x0,
    y:y0,
    width:sf_width,
    height:sf_height,
    fill:'#EEE',
    stroke:'#000',
    strokeWidth:randbreite_sf
  });
  kineticGroup.add(kineticGroup[m]);
  m++;


  kineticGroup.on('dragend', function () {
    var dx = parseFloat(document.getElementsByName("dx")[0].value);
    var dy = parseFloat(document.getElementsByName("dy")[0].value);
    kineticGroup['x'] = kineticGroup['x0'] + dx;
    kineticGroup['y'] = kineticGroup['y0'] + dy;
  });
  boxLayer.add(kineticGroup);
  sf.push(kineticGroup);
}

The truth is that you actually don't need any counter to store the history of the rect positions - you can just push it to the array using Array.prototype.push: http://www.w3schools.com/jsref/jsref_push.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.