【问题标题】:Andengine: game 15 and number image movementAndengine:游戏15和数字图像移动
【发布时间】:2011-10-23 22:00:25
【问题描述】:

我已经在图像网格中设置了数字图像,现在的问题是如何将具有 50 像素“模块”(单个单元格网格的尺寸)的数字图像移动到有空闲单元格的地方? 我正在使用 Andengine,每个数字图像都是一个 Sprite,网格图像也是一个。

编辑:我就是这样工作的:

我用这两个函数设置图块位置:

private void getGrid()
{
    boolean end     =   false;
    Random random   =   new Random();

    int x = 0;
    while( ALDef.size() < 15 && x < 100 )
    {
        // Genera un numero a caso, da 1 a 15
        int numRandom       =   random.nextInt( 15 );

        System.out.println( x++ + ") Scelgo un numero RANDOM:" + numRandom );

        // il numero e' gia' presente in array?
        if ( !memory.contains( numRandom ) )
        {
            System.out.println( "ADD " + numRandom );
            // Non e' presente...
            ALDef.add( numRandom );
            memory.add( numRandom );
        }
        else
        {
            System.out.println( "ESISTE " + numRandom );
            memory.add( numRandom );
        }
    }

    System.out.println( "------------------------------" );
}

// ===========================================================
// Restituisce le coordinate
// ===========================================================
public HashMap getCoordinates( int numInArray )
{
    // Ottiene l'integer corrispondente a numInArray, ovvero il tasto
    Integer value       =   ALDef.get( numInArray );

    int row         =   0; // riga della tabella
    int position    =   0; // posizione
    // Definisce la riga
    if ( numInArray <= 3 )
    {
        row         =   0;
        position    =   3 - ( 3 - numInArray );
    }
    else if ( numInArray >= 4 && numInArray <= 7 )
    {
        row     =   1;
        position    =   numInArray - 4;
    }
    else if ( numInArray >= 8 && numInArray <= 11 )
    {
        row     =   2;
        position    =   numInArray - 8;
    }
    else if ( numInArray >= 12 && numInArray <= 15 )
    {
        row     =   3;
        position    =   numInArray - 12;
    }

    HashMap HM      =   new HashMap();
    HM.put( "btn" , value );
    HM.put( "x" , width * position );
    HM.put( "y" , height * row );

    System.out.println( "Colloco il numero " + ( value ) + " nella casella " + numInArray + "(ROW=" + row + ",POS=" + position + ")" );

    return HM;
}

这两个函数的逻辑是getGrid放入一个ArrayList中的随机数(从0到15)对应于瓦片编号图像。 使用 getCoordinates 函数返回图块的坐标(行 - 0, 1, 2, 3 - 和位置 - 0, 1, 2, 3)以实例化精灵。

【问题讨论】:

  • 你说的是this吗?

标签: android andengine


【解决方案1】:

假设您使用 2D 数组 Cell[4][4] cells 跟踪单元格,您可以初始化单元格并拥有 emptyCellLocation = (emptyCell.row, emptyCell.col)。然后您可以确定要拖动哪个单元格,只允许它移动到emptyCellLocation。确保更新emptyCellLocation。

【讨论】:

  • 我通过我在第一篇文章中编写的脚本设置了磁贴位置,请检查它
  • 对不起,我不能按照你的代码。我会考虑引入像Tile 和Grid 这样的对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多