- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果方法为真,有什么方法可以暂停计时器。我创建了一个简单的拼图游戏,并添加了一个计时器来显示时间流逝,我试图在拼图解决时停止计时器。运行时,应用程序运行平稳,计时器滴答作响,但在游戏开始时不会停止解决了。任何形式的帮助将不胜感激。这是代码;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
chronometer = findViewById(R.id.chronometer);
stopChronometer();
startChronometer();
isSolved();
}
public static boolean isSolved() {
boolean solved = false;
for (int i = 0; i < tileList.length; i++) {
if (tileList[i].equals(String.valueOf(i))) {
solved = true;
} else {
solved = false;
break;
}
}
return solved;
}
//to start the chronometer
public void startChronometer(){
if(!running){
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
running = true;
}
}
//to stop the chronometer
public void stopChronometer(){
if(running && isSolved()){
chronometer.stop();
running =false;
}
}
如果对整个代码感兴趣;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import java.lang.*;
import java.util.ArrayList;
import java.util.Random;
import android.widget.Chronometer;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//GestureDetectView* is the class where the puzzle grid is setup
private static GestureDetectGridView mGridView;
private static final int COLUMNS =3;
private static final int DIMENSIONS = COLUMNS * COLUMNS;
private static int mColumnWidth, mColumnHeight;
//up, down, left, right are tile movements
public static final String up = "up";
public static final String down = "down";
public static final String left = "left";
public static final String right = "right";
private static String[] tileList;
private Chronometer chronometer;
private boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer = findViewById(R.id.chronometer);
init();
scramble();
setDimensions();
stopChronometer();
startChronometer();
}
//to start the chronometer
public void startChronometer(){
if(!running){
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
running = true;
}
}
//to stop the chronometer * PROBLEM AREA
public void stopChronometer(){
if(running && isSolved()){
chronometer.stop();
running =false;
}
}
//Grid view from GestureDetectView class
private void init() {
mGridView = (GestureDetectGridView) findViewById(R.id.grid);
mGridView.setNumColumns(COLUMNS);
tileList = new String[DIMENSIONS];
for (int i = 0; i < DIMENSIONS; i++) {
tileList[i] = String.valueOf(i);
}
}
//To shuffle the grid pieces
private void scramble() {
int index;
String temp;
Random random = new Random();
for (int i = tileList.length -1; i > 0; i--) {
index = random.nextInt(i + 1);
temp = tileList[index];
tileList[index] = tileList[i];
tileList[i] = temp;
}
}
private void setDimensions() {
ViewTreeObserver vto = mGridView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int displayWidth = mGridView.getMeasuredWidth();
int displayHeight = mGridView.getMeasuredHeight();
int statusbarHeight = getStatusBarHeight(getApplicationContext());
int requiredHeight = displayHeight - statusbarHeight;
mColumnWidth = displayWidth / COLUMNS;
mColumnHeight = requiredHeight / COLUMNS;
display(getApplicationContext());
}
});
}
private int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
"android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
//To determine if puzzle is solved
public static boolean isSolved() {
boolean solved = false;
for (int i = 0; i < tileList.length; i++)
{
if (tileList[i].equals(String.valueOf(i))) {
solved = true;
} else {
solved = false;
break;
}
}
return solved;
}
private static void swap(Context context, int Position, int swap) {
String newPosition = tileList[Position + swap];
tileList[Position + swap] = tileList[Position];
tileList[Position] = newPosition;
display(context);
if (isSolved()) {
Toast.makeText(context, "CONGRATULATIONS, YOU WIN!", Toast.LENGTH_SHORT).show();
}
}
//To source the image pieces and add them to the puzzle grid
private static void display(Context context) {
ArrayList<Button> buttons = new ArrayList<>();
Button button;
for (int i = 0; i < tileList.length; i++) {
button = new Button(context);
if (tileList[i].equals("0"))
button.setBackgroundResource(R.drawable.piece1);
else if (tileList[i].equals("1"))
button.setBackgroundResource(R.drawable.piece2);
else if (tileList[i].equals("2"))
button.setBackgroundResource(R.drawable.piece3);
else if (tileList[i].equals("3"))
button.setBackgroundResource(R.drawable.piece4);
else if (tileList[i].equals("4"))
button.setBackgroundResource(R.drawable.piece5);
else if (tileList[i].equals("5"))
button.setBackgroundResource(R.drawable.piece6);
else if (tileList[i].equals("6"))
button.setBackgroundResource(R.drawable.piece7);
else if (tileList[i].equals("7"))
button.setBackgroundResource(R.drawable.piece8);
else if (tileList[i].equals("8"))
button.setBackgroundResource(R.drawable.piece9);
buttons.add(button);
}
mGridView.setAdapter(new CustomAdapter(buttons, mColumnWidth, mColumnHeight));
}
public static void moveTiles(Context context, String direction, int position) {
// Upper-left-corner tile
if (position == 0) {
if (direction.equals(right)) swap(context, position, 1);
else if (direction.equals(down)) swap(context, position, COLUMNS);
else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
// Upper-center tiles
} else if (position > 0 && position < COLUMNS - 1) {
if (direction.equals(left)) swap(context, position, -1);
else if (direction.equals(down)) swap(context, position, COLUMNS);
else if (direction.equals(right)) swap(context, position, 1);
else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
// Upper-right-corner tile
} else if (position == COLUMNS - 1) {
if (direction.equals(left)) swap(context, position, -1);
else if (direction.equals(down)) swap(context, position, COLUMNS);
else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
// Left-side tiles
} else if (position > COLUMNS - 1 && position < DIMENSIONS - COLUMNS &&
position % COLUMNS == 0) {
if (direction.equals(up)) swap(context, position, -COLUMNS);
else if (direction.equals(right)) swap(context, position, 1);
else if (direction.equals(down)) swap(context, position, COLUMNS);
else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
// Right-side AND bottom-right-corner tiles
} else if (position == COLUMNS * 2 - 1 || position == COLUMNS * 3 -1)
{
if (direction.equals(up)) swap(context, position, -COLUMNS);
else if (direction.equals(left)) swap(context, position, -1);
else if (direction.equals(down)) {
// Tolerates only the right-side tiles to swap downwards as opposed to the bottom-
// right-corner tile.
if (position <= DIMENSIONS - COLUMNS - 1) swap(context, position,
COLUMNS);
else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
} else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
// Bottom-left corner tile
} else if (position == DIMENSIONS - COLUMNS) {
if (direction.equals(up)) swap(context, position, -COLUMNS);
else if (direction.equals(right)) swap(context, position, 1);
else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
// Bottom-center tiles
} else if (position < DIMENSIONS - 1 && position > DIMENSIONS - COLUMNS) {
if (direction.equals(up)) swap(context, position, -COLUMNS);
else if (direction.equals(left)) swap(context, position, -1);
else if (direction.equals(right)) swap(context, position, 1);
else Toast.makeText(context, "Invalid move", Toast.LENGTH_SHORT).show();
// Center tiles
} else {
if (direction.equals(up)) swap(context, position, -COLUMNS);
else if (direction.equals(left)) swap(context, position, -1);
else if (direction.equals(right)) swap(context, position, 1);
else swap(context, position, COLUMNS);
}
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
}
最佳答案
public void stopChronometer() {
if(running && isSolved()){
chronometer.stop();
running =false;
}
}
更改为
public void stopChronometer(){
if(running || isSolved()){
chronometer.stop();
running =false;
}
}
或者您可以从 isSolved() 方法调用 stopChronometer() (不过您必须从 stopChronometer 中删除 isSolved() )。在没有看到完整代码的情况下,很难回答。
关于java - 如果方法返回 true,则停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391193/
这个问题在这里已经有了答案: Why in Python does "0, 0 == (0, 0)" equal "(0, False)"? (7 个回答) 去年关闭。 代码片段 1: a = Tru
Integer i = 127; Integer j = 127; System.out.println(i == j); System.out.println(i.equals(j)); Integ
我试图用 Python 进行类似下面的代码的比较,但对产生的输出感到困惑。 谁能解释为什么输出是这样的? >>> True, True == True, True (True, True, True)
我们的下拉值是动态的 010100。 你能帮我将这些值转换为 true、false 吗? Offer的值是10100,Reject的值是10111。所以这些需要转换成 10100 = true,fal
我正在测试,如果用户在页面顶部显示一种货币“EUR”和另一种货币“GBP”,那么我期望包含文本“EUR”和页面下方还存在另一个包含文本“GBP”的链接。它包含在一个名为 "nav-tabs au-ta
如何检查数组的所有元素是真值还是假值。 因为以下内容似乎没有做到这一点:_.all([true, true, true], true); 它返回:false? 最佳答案 您应该重新阅读_.every(
C#:我有一个如下所示的字符串变量: string a = "(true and true) or (true or false)"; 这可以是任何东西,它可以变得更复杂,比如: string b
ruby : true == true == true syntax error, unexpected tEQ 对比JavaScript: true == true == true // => tr
这个问题已经有答案了: Equality of truthy and falsy values (JavaScript) (3 个回答) Which equals operator (== vs ==
为什么 R 中的 TRUE == "TRUE" 是 TRUE? R 中是否有与 === 等效的内容? 更新: 这些都返回FALSE: TRUE == "True" TRUE == "true" TRU
简单的查询,可能不可能,但我知道那里有一些聪明的人:) 给定一个 bool 参数,我希望定义我的 where 子句来限制特定列的输出 - 或不执行任何操作。 因此,给定参数@bit = 1,结果将是:
编写 Excel 公式时,将值设置为 true、“true”还是 true() 是否有区别? 换句话来说,以下哪一个是最好的?还是要看具体情况? if (A1 = 1, true, false) if
如果我们评估这个:TRUE AND TRUE,为什么会这样? 'yes' : 'no' 等于 TRUE 但不等于 yes 何时评估:(TRUE AND TRUE) ? 'yes' : 'no' 等于
这个问题在这里已经有了答案: Behaviour of and operator in javascript [duplicate] (1 个回答) 关闭 7 年前。 如题所说,我不太明白为什么(t
我有一个包含 FromDate 、 ToDate 、 VendorName 和 GoodsName 的表单,一旦一切为真,我需要显示结果 示例: FromDate="11/20/2019"、ToDat
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
我有一个模型,我有: ipv4_address = models.IPAddressField(verbose_name=_('ipv4 address'), blank=True, null=Tru
False in [True,True] False in pd.Series([True,True]) 第一行代码返回False第二行代码返回 True! 我想我一定是做错了什么或者遗漏了什么。当我
我可以在 Coq 中证明以下内容吗? Lemma bool_uip (H1 : true = true): H1 = eq_refl true. 即true = true 的所有证明都相同吗? 例如
如果我的理解是正确的,他们做的事情完全一样。为什么有人会使用“for”变体?仅仅是味道吗? 编辑:我想我也在考虑 for (;;)。 最佳答案 for (;;) 通常用于防止编译器警告: while(
我是一名优秀的程序员,十分优秀!