我已经在图像网格中设置了数字图像,现在的问题是如何用一个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中),以对雪碧进行定位。
发布于 2011-10-25 03:38:40
假设您使用2D数组Cell[4][4] cells
跟踪单元格,则可以初始化单元格并拥有emptyCellLocation = (emptyCell.row, emptyCell.col)
。然后,您可以确定要拖动哪个单元格,并且只允许它移动到emptyCellLocation
。确保更新emptyCellLocation
。
https://stackoverflow.com/questions/7869550
复制相似问题