如何在单击时正确选择和取消选择矩形/纹理 [英] How to select and deselect a Rectangle/texture properly on click
问题描述
有关问题本身的视频: https://www.youtube.com/watch? v = cJfFIB3baAg
A video of the problem itself : https://www.youtube.com/watch?v=cJfFIB3baAg
我知道有一个简单的方法可以用scene2d做到这一点,但是我选择使用Rectangle和Texture变量来创建这个项目,这意味着我有20个Textures(1到9带有空白背景,1到9带有绿色黑底,如图所示) matrix [0] [0])和20个Rectangles,每个矩形代表创建的Texture变量之一.如您所见,在此刻,正方形[0] [0]被选中.
I know there's a simple way to do this with scene2d but i chose to make this project with Rectangle and Texture variables, that means I have 20 Textures(1 to 9 with blank background and 1 to 9 with green blackground as shown in matrix[0][0]) and 20 Rectangles , which each one represents one of the Texture variables created. Like you see, the square[0][0] is selected at this very momment.
我想要做的是,通过选择另一个正方形,将其作为结果:
What I want to do Is, by selecting another square , to be this the result:
这正在发生!问题是,当我单击一个正方形时,要对其进行选择,我需要单击,例如... 2或3次,而不是仅仅一次!而且我不知道为什么会这样!这是我的一些代码,如果您不了解某些内容,请问我.希望你能帮助我.
and that is happening! The problem is that when i click a square, for it to be selected i need to click like... 2 or 3 times instead of only one time! And i can't figure it out why's that happening! Here's some of my code, if you don't understand something please ask me. Hope you can help me.
@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多个此类. x和y变量用于associateImage(); ...
I'm only giving you one example here because there are 80 more of this kind in checkTexture() method. The x and y variables are used in batch.draw in associateImage();...
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;
}
然后在render方法中,我仅向您提供我做的9个示例之一(绘制未选择的80个示例)
And then in the render method I'll give you only one of the 9 examples of what I do (drawing the 80 that are not selected)
@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;
...
}
推荐答案
我认为您应该使用1个Rectangle
/cell,而不是每个数字和背景色使用1.
因此,您应该有9 * 9 = 81 Rectangle
s,其中每个Rectangle
代表数独字段的1个单元格.
在touchDown(touchDown(int screenX, int screenY, int pointer, int button)
中,您可以使用Rectangle.contains(Vector2 point)
来简单地检查已触摸了哪个Rectangle
.
要知道选择了哪个Rectangle
,可以存储一个int selRect
,如果没有选择Rectangle
,则可以存储所选Rectangle
或-1的索引.
左下角Rectangle
的索引为0,右下角的索引为8,右上角的索引为80.
整个数独字段都可以存储在一个2D int数组中,该数组包含0-9之间的数字,其中0是一个空字段.
在渲染中,您可以简单地循环浏览2D数组并检查selRect
并渲染正确的Texture
In my opinion you should use 1 Rectangle
/cell, not 1 per number and background color.
So you should have 9*9=81 Rectangle
s, where every Rectangle
represent 1 cell of the sudoku field.
In the touchDown(touchDown(int screenX, int screenY, int pointer, int button)
you can simply check, which Rectangle
has been touched, by using Rectangle.contains(Vector2 point)
.
To know, which Rectangle
is selected you can store an int selRect
, which stores the index of the selected Rectangle
or -1, if no Rectangle
is selected.
The lower left Rectangle
would have the index 0, the lower right the index 8 and the top right would have the index 80.
The whole sudoku-field can be stored in a 2D int array, containing the numbers from 0-9, where 0 is an empty field.
In the render you can simply cycle through the 2D array and check the selRect
and render the right Texture
我希望我足够清楚.
这篇关于如何在单击时正确选择和取消选择矩形/纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!