我在PaperScript中创建了两类对象:Node
s和Line
s。每行都有一个this.nodes
属性,该属性是引用两个节点对象的数组,每行一个。节点具有this.dragging
属性,当创建节点对象时,该属性被初始化为false
,但在onMouseDown
事件上被设置为true
,并且被设置回false
在onMouseUp
。这样我就可以跟踪节点是否意图在某个时间点移动。尝试从另一个对象访问对象属性始终返回属性的初始值
现在我希望能够从Line
对象中检查其关联节点是否在任何时间点被拖动。为此我有一个if语句来检查是否(this.nodes[0].dragging == true || this.nodes[1].dragging == true)
。如果这返回true,则可能会发生进一步的操作。
问题是无论我是否拖动节点,两个this.nodes[i].dragging
都始终返回false。为什么会发生?特别是当我从Node
对象中检查this.dragging
的值时,它确实会在拖动时返回正确的true值,否则返回false。
编辑包括一些代码:
以下是我的代码的精简版。重要的是this.dragging
变量。
function Node(pos) {
///// Stuff
this.dragging = false;
this.node.onMouseDown = function(event) {
this.dragging = true;
// Do other stuff
}
this.node.onMouseDrag = function(event) {
if (this.dragging == true) {
// Do stuff
}
}
this.node.onMouseUp = function(event) {
// Do stuff
this.dragging = false;
}
}
以下是(的一部分)的构造函数Line对象:
function Line(p1, p2) {
this.nodes = [p1, p2];
///// Stuff
this.update = function() {
if (this.nodes[0].dragging === true || this.nodes[1].dragging === true) {
// True
} else {
// False
}
}
}
2日编辑评论者要求我如何实例化这些对象:
我实例化它们如下:
var nodeNum = 7;
var lineConnections = [{
from: 0,to: 1}
,{from: 0,to: 2}
,{from: 0,to: 3}
,{from: 0,to: 4}
,{from: 1,to: 2}
,{from: 1,to: 5}
,{from: 2,to: 3}
,{from: 2,to: 4}
,{from: 3,to: 5}
,{from: 4,to: 5}
,{from: 2,to: 5}
]
function init() {
for (var i = 0; i < nodeNum; i++) {
Nodes[i] = new Node();
}
for (var i = 0; i < lineConnections.length; i++) {
Lines[i] = new Line(Nodes[lineConnections[i].from], Nodes[lineConnections[i].to]);
}
}
init();
现场演示可在http://aronadler.com/experiment/
首先,体面的探索,但我们不能开始猜测没有一些代码。请发布您的类构造函数等。如果它更复杂,JFiddle会很好的工作。 – Wobbles
最初的想法,我可能会使用===运算符进行测试,因为== true有时可能表现错误,具体取决于属性的解释方式。 – Wobbles
我无法弄清楚如何让我的PaperScript文件的内容在JSFiddle中工作,所以如果我将它链接到托管的域名就足够了。该项目目前在http://aronadler.com/experiment/ – Aron