0

我会尽量准确地解释我的问题。

我的项目有 2 个组([Group1]、[Group2])我想要 [Button],它可以切换 [Group1] 的可见性。(setOpacity 为 1 或 0)我希望 [Group1],[Group2] 可以拖动并相互锚定。

I've tried this way:
I've created 2 layers [Layer1] and [Layer2].
I've added [Group1] to [Layer1]
I've added [Group2] to [Layer2]
When i click [Button], i toggle Opacity of [Layer1]
I've created [MainGroup] and set it to draggable.
I've added [Group1,2] to [MainGroup]. To make them anchored to each other, and i could drag them.

但是如果我使用 [Group1].getParent() 我得到 [Layer1],而不是 MainGroup,这是为什么呢?我应该使用不同的逻辑吗?我需要 [Group1].getParent() 来返回 [MainGroup],所以我可以在其中找到另一个组,即 [Group2] 并为其添加一些形状。

我的 KineticJS 版本是:4.7.2

4

1 回答 1

0

您可以像这样嵌套组:http: //jsfiddle.net/m1erickson/YmAdJ/

  • 在层上创建一个 containerGroup。
  • 创建 Group1 并将其添加到 containerGroup。
  • 创建 Group2 并将其添加到 containerGroup。
  • 使 containerGroup 可拖动。

嵌套组看起来像这样:

// make a container group to hold group1 and group2

var containerGroup=new Kinetic.Group({
    x:20,
    y:20,
    width:225,
    height:100,
    draggable:true
});
layer.add(containerGroup);

// create group1 and group2 inside containerGroup

var group1=new Kinetic.Group({
    id:"g1",
    x:0,
    y:0,
    width:125,
    height:100,
});
containerGroup.add(group1);

var group2=new Kinetic.Group({
    id:"g2",
    x:126,
    y:0,
    width:100,
    height:100,
});
containerGroup.add(group2);

对于 Group1 和 Group2,您可以像这样获得对 containerGroup 的引用:

group1.on(“click”,function(){
      var myContainerGroup=this.getParent();
});

group2.on(“click”,function(){
      var myContainerGroup=this.getParent();
});

您可以使用对 containerGroup 的引用来获取对子组的引用,如下所示:

var myGroup1 = myContainerGroup.get("#g1")[0];

var myGroup2 = myContainerGroup.get("#g2")[0];
于 2013-10-25T17:49:17.183 回答