【发布时间】: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吗?