如何判断拼图是否完成?
我正在准备一款像拼图这样的小游戏,为此我在布局中使用了 9 个图像视图和 9 个不同的图像。在启动时将图像设置为 imageview 这些是实际图像,随机播放后用户将滑动图像以完成拼图,我想检查修改后的图像与实际图像的,天气是否相等,如果它们相等弹出一条消息,就像游戏结束一样。
我尝试像这样
1.通过在图像(Drawables)之间使用 AND 运算符,但不幸的是。
2.对图像使用 setLevel() ,将这些 setLevel 值与滑动后图像的 getLevel 值进行比较仍然不走运..这里的问题是,如果我单击 imageview 一次 getLevel() 给出正确的值,如果我单击多次给出零值。为什么会发生这样的情况..
如果您在我的代码中发现任何错误,请帮助我,否则请用好的技术指导我..
Xml code like this
<RelativeLayout
//9 imageview's
在像这样的jave代码中 如果用户单击 imageview,它将交换图像的
img_View11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(img_View21.getDrawable() == default_img ||
img_View12.getDrawable() == default_img) {
System.out.println("yes");
++click;
Noof_moves.setText("Moves: " +click);
ImageView iv = (ImageView)v;
d11 = iv.getDrawable();
prev_img = prev_imgView.getDrawable();
la11 = d11.getLevel();
System.out.println("d11 value is " +la11);
prev_imgView.setImageDrawable(d11);
img_View11.setImageDrawable(prev_img);
prev_imgView = img_View11;
check();
}
else { System.out.println("no"); }
}
});
void check(){
System.out.println("in checking condition");
if((lb11==la11) && (lb12==la12) && (lb13==la13) && (lb21==la21) && (lb22==la22)
&& (lb23==la23) && (lb31==la31) && (lb32==la32) && (lb33==la33)) {
Context c = getBaseContext();
System.out.println("gameover");
Toast.makeText(c, " GameOver ", Toast.LENGTH_SHORT).show();
}else{ System.out.println(" codition not checked "); }
}
//here lb=level before sliding
// la=level after sliding..
i am preparing one small game like jigsaw , for that i am using 9 imageview's with 9 different images in the layout. set the images to imageview at the time of starting those are actual images, after shuffle user will do sliding the images to complete puzzle, i want to check the modified image with actual image's, weather it's equal or not if those are equal popup a message, like gameover.
i tried like this
1.by using AND operator between the images(Drawables) but unlucky.
2.Using setLevel() for the images, compare those setLevel values with getLevel values for images after sliding still Unlucky.. here the problem is if i click on imageview one time getLevel() gives correct value, if i click more than once it gives zero value. Why it happens like this..
Please help me if you find any mistake in my code, otherwise guide me with good technique..
Xml code like this
<RelativeLayout
//9 imageview's
in jave code like this
if user will click on imageview it will swap the image's
img_View11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(img_View21.getDrawable() == default_img ||
img_View12.getDrawable() == default_img) {
System.out.println("yes");
++click;
Noof_moves.setText("Moves: " +click);
ImageView iv = (ImageView)v;
d11 = iv.getDrawable();
prev_img = prev_imgView.getDrawable();
la11 = d11.getLevel();
System.out.println("d11 value is " +la11);
prev_imgView.setImageDrawable(d11);
img_View11.setImageDrawable(prev_img);
prev_imgView = img_View11;
check();
}
else { System.out.println("no"); }
}
});
void check(){
System.out.println("in checking condition");
if((lb11==la11) && (lb12==la12) && (lb13==la13) && (lb21==la21) && (lb22==la22)
&& (lb23==la23) && (lb31==la31) && (lb32==la32) && (lb33==la33)) {
Context c = getBaseContext();
System.out.println("gameover");
Toast.makeText(c, " GameOver ", Toast.LENGTH_SHORT).show();
}else{ System.out.println(" codition not checked "); }
}
//here lb=level before sliding
// la=level after sliding..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,这就是您为解决方案所做的事情。
可能就像
drawable og11 = imageView11.getDrawable();
在洗牌之前完成这部分。现在你有了原始的可绘制对象,以可绘制对象的形式存储。
HTH。
Ok, here is what you do for the solution.
may be like
drawable og11 = imageView11.getDrawable();
do this part before shuffling. now you have original drawables, stored in the form of drawables.
HTH.
我有一个简单的解决方案。在混乱之前(准备拼图之前)使用 View.setTag(index) 将序列号
Integer
标签设置为 ImageView。然后每次用户移动时,循环遍历所有 imageview 并检查它们是否按顺序排列。如果顺序不对,则拼图尚未完成。每次移动时:
每次移动后检查是否完成:
I have a simple solution. Set serial number
Integer
tags to ImageViews using View.setTag(index) before jumbling(before preparing the puzzle.) Then everytime the user makes a move, loop through all the imageviews and check if they are in order. If out of order then puzzle is not completed yet.On every move:
After every move check for completion:
为什么不使用 gridView 呢?每次更改后,只需检查项目以查看图像是否按所需顺序排列
why don't you use a gridView? and after each change, just check the items to see that images are in desired order
当您使用 == 运算符时,它们会检查两个对象是否引用同一个对象。但就你而言,它们是两个不同的对象。
您应该为实际图像和其他图像设置标签。并检查它们是否相同。这应该适用于你的 if 条件。
对于可绘制类,我确实注意到了 setLevel 和 getLevel 方法。您也许可以使用它来满足您的要求。
When you use == operator they are checking if both the objects are referencing the same object. But in your case they are 2 different objects.
You should set a tag to both the actual image and the other image. And check if they are both the same. That should work in your if condition.
For the drawable class i did notice a method setLevel and getLevel. You might be able to use this for your requirement.