【问题标题】:javascript logic returning strange resultsjavascript逻辑返回奇怪的结果
【发布时间】: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


【解决方案1】:

您需要在要测试的表达式周围加上括号 > origin.movement 以便在那里使用该表达式的结果;没有它们,表达式会更早地被分解:

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;
}

但是,无论如何我都不会在一条长线上使用赋值给自己,为了清晰和易于调试,我会将其分解,并使用 @987654324 @:

所以:

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 变量而成为The Horror of Implicit Globals 的牺牲品(后来也有一个错字)。我通过在上面添加var 修正了错字并修正了隐式全局。

【讨论】:

  • 仍然遭受同样的错误结果..我不明白..逻辑有什么问题?
  • 我认为变量输入一定有什么奇怪的地方.. idk,谢谢你的帮助!
【解决方案2】:

我想通了,我没有正确计算操作,我需要将操作添加到我正在移动的轴上,所以声明

    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] );

【讨论】:

    猜你喜欢
    • 2012-08-08
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多