1

我正在使用 for() 语句创建矩形节点,现在我需要获取每个节点的引用。似乎有一些黑魔法来创建这些画布对象,因此很难访问没有 id 可引用的项目。有人可以解决这个问题或指出我正确的方向吗?

for(x=1;x<=8;x++)
  {

  var rect = new Kinetic.Rect({
    x: 300,
    y: 80+offset,
    width: 60,
    height: 20,
    fill: 'white',
    stroke: 'black',
    strokeWidth: 1,
    draggable: false
  });
rect.on('mouseover', function() {
    writeMessage(messageLayer, this.getY());
  });
  // add the shape to the layer
  layer.add(rect);

offset += 120;

谢谢

4

1 回答 1

1

您应该可以使用购买来访问它们中的每一个layer.getChildren();,这将返回所有这些,或者如果您知道需要layer.getChildren()[0];哪个,则将获得第一个。虽然我会给他们每个人一个名字,以使其更容易一些。

for(x=1;x<=8;x++)
{
  var rect = new Kinetic.Rect({
  name: 'rct'+x,
  x: 300,
  y: 80+offset,
  width: 60,
  height: 20,
  fill: 'white',
  stroke: 'black',
  strokeWidth: 1,
  draggable: false
  });
}

然后你可以使用layer.get('.rct3');or layer.get('.rct3')[0];

于 2012-12-29T16:31:45.867 回答