【发布时间】:2015-01-16 14:42:46
【问题描述】:
问题本身的视频:https://www.youtube.com/watch?v=cJfFIB3baAg
我知道有一种简单的方法可以使用 scene2d 来实现,但我选择使用 Rectangle 和 Texture 变量来制作这个项目,这意味着我有 20 个纹理(1 到 9 个带有空白背景,1 到 9 个带有绿色黑色背景,如图所示matrix[0][0]) 和 20 个 Rectangles ,每个 Rectangles 代表创建的纹理变量之一。如您所见,此时选择了 square[0][0]。
我想要做的是,通过选择另一个方块,得到这样的结果:
这正在发生!问题是,当我单击一个正方形时,要选择它,我需要单击... 2 或 3 次而不是仅一次!而且我无法弄清楚为什么会这样!这是我的一些代码,如果你不明白,请问我。希望你能帮助我。
@Override
public void render() {
batch.begin();
...(drawing of the matrix)...
if(Gdx.input.isTouched()){
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
System.out.println(touchPos.x+ " "+touchPos.y);
checkTexture();
}
batch.end();
}
我在这里只给你一个例子,因为在 checkTexture() 方法中还有 80 多个此类。在 associateImage() 中的 batch.draw 中使用 x 和 y 变量;...
public Texture checkTexture() {
int number=0;int x=0,y=0;
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
int lastX=0,lastY=0;
if((touchPos.x>=125 && touchPos.x<=164) && (touchPos.y>=400 && touchPos.y<=440)){
number=matrix[0][0];x=125;y=400;lastX=currentX;lastY=currentY;squares[lastX][lastY]=false;currentX=0;currentY=0;squares[currentX][currentY]=true;
}
return associateImage(number,x,y);
}
Here are the selected textures
public Texture associateImage(int n,int x,int y) {
Texture t=null;
switch(n) {
case 1: t=numberOneSEL;break;
case 2: t= numberTwoSEL;break;
case 3: t= numberThreeSEL;break;
case 4: t= numberFourSEL;break;
case 5: t= numberFiveSEL;break;
case 6: t= numberSixSEL;break;
case 7: t= numberSevenSEL;break;
case 8: t= numberEightSEL;break;
case 9: t= numberNineSEL;break;
case 10: t= emptySquareSEL;break;
}
batch.draw(t, x, y);
return t;
}
然后在渲染方法中,我将只给你我所做的 9 个示例中的一个(绘制未选中的 80 个)
@Override
public void render() {
...batch.begin()...
for(x=0;x<matrix.length;x++){
for(y=0;y<matrix.length;y++){
switch(matrix[x][y]){
case 1:
if(squareCounter==9) {squareCounter=0;startingX=125;startingY-=40;numberOneR.x=startingX;numberOneR.y=startingY; }
numberOneR.setX(startingX);numberOneR.setY(startingY);
if(squares[x][y]==false)
batch.draw(numberOne, numberOneR.x, numberOneR.y);
squareCounter++;
startingX+=40;
numberOneR.x=startingX;
break;
...
}
【问题讨论】: