kineticjs - 在组S中移位形状

时间:2013-04-09 06:52:21

标签: kineticjs

我有一点问题。我的动力学阶段是这样的:

Stage -> layer0 -> group0 -> shapes
          layer1 -> group1 -> shapes
          layer2 -> group2 -> shapes

我需要在调用group0事件(dragstart,dragmove等)时移动group1和group2。我试着做这样的事情:

group0.draggable = true;
group0.on('dragstart', function(){
  var a = #save first mouse position point
})
group0.on('dragmove', function(){
  #ref to group1 and group2 is store in group0 and as i debugged in chrome, this object is properly recognize 
  group1.setPosition(my new positions x, y)
  group2.setPosition(...)
})
换句话说,

我需要来自不同层的连接组,并将它们视为与嵌套的3个其他组在一个组中。我的代码不起作用,是bug还是我忘记了什么?怎么做到这一点?控制台没有错误,它只是不起作用,我可以移动group0但group1和group2 setPosition函数不会改变任何东西,尽管它们似乎被调用正常。谢谢

1 个答案:

答案 0 :(得分:2)

我只能推测,因为我没有在你面前的代码。

但要检查的第一件事是你要重新绘制图层。

group0.on('dragmove', function(){
    #ref to group1 and group2 is store in group0 and as i debugged in chrome, this object is properly recognize 
    group1.setPosition(my new positions x, y)
    group2.setPosition(...)
    group1.getLayer().draw(); //redraw group1 layer
    group2.getLayer().draw(); //redraw group2 layer
    // stage.draw(); // also a possibility
})

//you can also do transitions, which do redrawing for you.
group1.transitionTo({
    duration: 1, //how long the animation takes in seconds
    x: new position x coord
    y: new position y coord
}); 
//repeat for other groups you want moved

另外,重要的是要注意设置位置不会改变其中的项目的位置,所以如果你的shape1在100,100,那么在移动组后它仍会报告为100,100,因为位置是相对于容器而言。