1

我正在开发一个类似单词搜索的游戏。为此,我使用了 AndEngine,并使用了 TiledMap 来显示单词网格。在这个游戏中,我们需要连接字母来组成一个单词,当我们拖动手指时,它会在瓷砖上画线并在字母之间建立联系。我能够准确地垂直和水平地画线,但是当我尝试对角线画线时,它会到达附近的瓷砖并从最后触摸的瓷砖画线到附近的瓷砖。但是当我小心地移动到对角线瓷砖时,它会准确地画线。

谁能帮我解决这个问题?

e.g.
----------------
|1   2   3   4 |
|5   6   7   8 |
|9  10  11  12 |
|13 14  15  16 |
----------------

如上图所示

我能够为 画线"1234" & "159",这意味着所有水平和垂直位置都可以正常工作。但因为"963"它不如我画的水平和垂直准确。

case TouchEvent.ACTION_MOVE:

    xMove = pSceneTouchEvent.getX();
    yMove = pSceneTouchEvent.getY();
    TMXLayer tmxLayer1 =  mTMXTiledMap.getTMXLayers().get(0);
    TMXTile tmxTile1 = tmxLayer1.getTMXTileAt(xMove, yMove); 
    tmxTile1.setTextureRegion(mPlayerTextureRegion);

    if (tmxTile1.getTileRow() == 1 && tmxTile1.getTileColumn() == 1) {
    //letter M
        xMove = pSceneTouchEvent.getX();
        yMove = pSceneTouchEvent.getY();
        //yMove = pSceneTouchEvent.getX();
        if (xMove >= (TILESQUARE * 1)+20 && yMove >= (TILESQUARE * 1)+20) {//added
            yMove = (TILESQUARE * 1) + 30;//gets x-cordinate of center
            xMove = (TILESQUARE * 1) + 30;//gets y-cordinate of center
            Log.i("On Action Move X11", ""+xMove);
            Log.i("On Action Move Y11", ""+yMove);
                          //tmxTile1.setTextureRegion(mPlayerTextureRegion);
            if (!addedAmazeM) {
                addedAmazeM = true;
                System.out.println("You are in (1,1) tile");
                wordArray.add("M");
                HashMap<Float, Float> moveXY = new HashMap<Float, Float>();
                moveXY.put(xMove, yMove);
                //linePath.add(moveXY);
                Point tile11Pt = new Point();
                tile11Pt.x = (int) xMove;
                tile11Pt.y = (int) yMove;
                linePoints.add(tile11Pt);
            }
        }
    }
4

3 回答 3

1

另一种方法是使用圆形碰撞箱。这将使您的手指能够通过角落,但永远不会跳过瓷砖并像国际象棋中的马一样跳跃。

您可以发布一些用于手指检查的代码吗?

编辑*: 我将保存一个包含我最新的有效瓷砖的 previousTile,然后拥有

tmxLayer1.getTMXTileAt(xMove, yMove);

如果我在圆形命中框之间,则返回 null。示例 getTMXTileAt:

  1. 像现在函数一样查找 tile。
  2. 找到瓷砖的中心。
  3. 检查我的按压点是否在该中心点的某个“灵敏度半径”内
  4. 仅当该图块在其内时才将其计为有效。如果不是:我的手指在瓷砖之间。

如果您仍想使用盒子方法,您还需要为 x 和 y 添加下限。我仍然建议切换到循环。这也容易得多。

声明一个静态 int 用于敏感度 ex:20

然后做

int tX,tY;
tX = moveX - tileSquareCenterX;
tY = moveY - tileSquareCenterY;
tX *= tX;
tY *= tY;
if(tX + tY < sensitivity*sensitivity)

代替

(xMove >= (TILESQUARE * 1)+20 && yMove >= (TILESQUARE * 1)+20) {//added
于 2013-07-09T10:54:14.593 回答
0

在我看来,你必须为手指设置大的命中框,没有空间可以让手指沿对角线移动而不会在水平或垂直框上记录命中。

如果是这种情况,应该通过将“hitbox”的大小减小几个像素来轻松解决。这将使您能够通过角落拖动手指。

于 2013-07-09T08:04:12.083 回答
0

Like Agnfolie said, reduce the size of the hitbox, just not only a few pixels, but try to reduce them to like 50%.

Basicly, only if you draw through the middle of the field it accepts it, and not if you just touch the border.

于 2013-07-09T09:32:23.240 回答