English 中文(简体)
以动动 JS 重置为默认位置
原标题:Reset to Default position with KineticJS

我的游戏中有以下圆圈。 玩家会把他们拖到游戏的不同位置。 当玩家选择时, 再玩一次, 我希望这些形状返回到 Init 函数中显示的原有位置 。 我该怎么处理这个? 谢谢 。

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)
最佳回答

在一个单独的变量中存储默认值, 然后使用 < a href=" http://kinticjs.com/api-docs.php#docsNode" rel= "nofollow"\\ code>. setpotion( 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);
问题回答

您也可以使用这样的超级便捷设置Attrs 方法 :

circle1.setAttrs(settings);

干杯! 干杯! 干杯! 干杯!

我知道这个问题已经很老了 但我刚解决了这样的问题

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

舞台. reset () 不再有效, 但是这应该做你想做的事 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签