【发布时间】:2015-04-25 06:44:33
【问题描述】:
我有一个网格,我正在尝试将光标编程到该网格上,网格中有一个中心点,我试图使光标只能从该中心点移动 x 个正方形,这里是我的代码
var makecursor = function (axis, operation) {
oppositeAxis = axis === 'y' ? 'x' : 'y';
// app.cursor contains the x and y coordinates of the cursor
app.cursor[axis] =
// calcualte the difference between the current cursor location and the origin
Math.abs( app.cursor[axis] - origin[axis] ) +
// calculate the difference between the opposite axis and
// the origin and add it to the previous calculation
Math.abs( app.cursor[oppositAxis] - origin[oppositeAxis] )
// add the operation to be performed ( movement of cursor, 1 or -1 )
+ operation
// if the sum of the x, y and current operation are greater then the allowed
// movement, then make "app.cursor[axis]" equal to itself (dont move) ,
// otherwise make "app.cursor[axis]" equal to itself plus the operation ( move )
> origin.movement ? app.cursor[axis] : app.cursor[axis] + operation;
}
“操作”是 1 或 -1,用于定向
“origin.movement”是从原点可以移动光标的方格数。
我希望/预期的行为是,从我的网格上的一个正方形,您只能移动“origin.movement”变量中指定的多个正方形。但是当我打印出结果时它返回奇怪的数字,并且它没有正确计算位置,即原点应该为零,而是一个或两个,这取决于以前的动作,我没有的许多其他异常能够理解。任何有关此问题的帮助将不胜感激,谢谢!
【问题讨论】:
-
sry 有点晚了,我很累.. 我没有复制它,现在修复.. 我想,
app.cursor[axis] =和结尾之间的所有内容都在一行上
标签: javascript math logic