2012-05-24 122 views
1

我的遊戲中有以下圈子。玩家將他們拖到遊戲中的不同位置。當玩家選擇,再次玩時,我希望形狀回到init函數中陳述的原始位置。我應該怎麼做呢?謝謝。用KineticJS重置爲默認位置

stage = new Kinetic.Stage({ 
    container: "container", 
    width: 900, 
    height: 550 
}); 

shapes = new Kinetic.Layer(); 

function init() { 
    circle1 = new Kinetic.Circle({ 
    x: stage.getWidth()/3.2, 
    y: stage.getHeight()/3.2, 
    radius: radius, 
    fill: "blue", 
    stroke: "black", 
    strokeWidth: 4, 
    name: "circle", 
    draggable: true 
}); 

shapes.add(circle1) 
stage.add(shapes) 

回答

2

存儲默認設置在一個獨立的變量,然後使用.setPosition(x, y)重置密碼:

//store settings 
var settings = { 
    x: stage.getWidth()/3.2, 
    y: stage.getHeight()/3.2, 
    radius: radius, 
    fill: "blue", 
    stroke: "black", 
    strokeWidth: 4, 
    name: "circle", 
    draggable: true 
}; 

//use settings 
circle1 = new Kinetic.Circle(settings); 

//after move, reset using: 
circle1.setPosition(settings.x,settings.y); 
2

,您還可以使用超級方便setAttrs方法是這樣的:

circle1.setAttrs(settings); 

乾杯!

0

我知道這個問題是相當古老的,但我只是解決了這樣的事情。

stage.removeChildren(); 
stage.setAttrs(stage.defaultNodeAttrs); 

stage.reset()不再工作,但這應該做你想做的。