2013-04-09 48 views
0

我有一个小问题。我动能阶段是这样的:kineticjs - 移位组中的形状

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

我需要移动1与组2时,都称为GROUP0事件(的dragstart,dragmove等)。我试图做这样的事情:

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(...) 
}) 

换句话说。我需要来自不同层次的连接组,并将它们对待,就像它们将嵌套在其他组中一样。我的代码不起作用,是错误还是我忘了什么?如何实现这一目标?在控制台中没有错误,它只是不起作用,我可以移动group0,但group1和group2 setPosition函数不会改变任何东西,尽管它们似乎是正确调用。谢谢

+0

你可以把你的代码放在jsfiddle中吗?这样我们可以看到它不起作用。 – SoluableNonagon 2013-04-09 13:22:02

回答

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 

另外,需要注意的是设定的位置不会改变它里面的物品的位置,所以如果你在100,100有shape1那么它仍然报告处于100,100组移动后,因为这个位置是相对于它的容器的。

+0

谢谢你,我在代码中发现了一个bug :) – Puchacz 2013-04-24 23:10:47