我有一个网格,我正在尝试将光标编程到,网格中有一个中心点,我试图让光标只能从该中心点移动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;
}
“operation”为1或-1,用于方向性
“origin.movement”是您可以移动光标的原点的方格数。
我希望/期望的行为是从我的网格上的一个正方形,你只能在“origin.movement”变量中指定多个正方形。但是当我打印出结果时它返回奇怪的数字,并且它没有正确计算位置,即原点应该为零,而是取而代之前的一个或两个,取决于先前的运动,我还没有其他许多异常能够理解。任何有关这个问题的帮助将不胜感激,谢谢!
答案 0 :(得分:0)
你需要在正在测试> origin.movement
的表达式周围放置一些parens,以便在那里使用该表达式的结果;如果没有它们,表达式就会被打破:
var makecursor = function (axis, operation) {
var oppositeAxis = axis === 'y' ? 'x' : 'y';
app.cursor[axis] = (
// calculate 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[oppositeAxis] - 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 )
) > operation ? app.cursor[axis] : app.cursor[axis] + operation;
}
但是,无论如何我都不会在分配到自我的长线上这样做,为了清晰和易于调试,我会将其分解,并且只使用{{ 1}}:
所以:
if
旁注:你的代码也是The Horror of Implicit Globals的牺牲品,因为你没有声明你的var makecursor = function (axis, operation) {
var oppositeAxis = axis === 'y' ? 'x' : 'y';
var movement =
// calculate 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[oppositeAxis] - 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 less than or equal
// to the allowed movement, add the operation to `app.cursor[axis]`
if (movement <= origin.movement) {
app.cursor[axis] += operation;
}
}
变量(以后也有使用它的拼写错误)。我修正了拼写错误并通过在上面添加oppositeAxis
来修复隐含的全局。
答案 1 :(得分:0)
我想通了,我没有正确地考虑操作,我需要将操作添加到我正在移动的轴上,所以语句
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[oppositeAxis] - origin[oppositeAxis] ) +
// add the operation to be performed ( movement of cursor, 1 or -1 )
operation;
应该是这样的:
// add operation to curser location to account for movement
Math.abs(( app.curser[axis] + operation ) - origin[axis] ) +
// instead of the adding operation at the end
Math.abs( (app.curser[oppositeAxis]) - origin[oppositeAxis] );