首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:睡眠()方法正在停止我的整个代码

Java:睡眠()方法正在停止我的整个代码
EN

Stack Overflow用户
提问于 2022-02-07 22:23:16
回答 1查看 83关注 0票数 -1

我正在使用android演播室玩一个21点游戏,在运行睡眠方法时遇到了一个问题。我希望当玩家按下看台按钮时,它将执行2项任务:

  1. 翻开经销商牌,播放经销商的手点数
  2. ,一直到他有17张牌为止。在每一张卡片的创作和每一张卡片的创作之间都有一笔交易。我希望在这两个任务之间有一个延迟。但是当我使用睡眠方法时,它会在持续时间内停止,然后立即显示这两个任务。

代码:

代码语言:javascript
复制
stand.setOnClickListener(new View.OnClickListener() { 
                    
            @Override
            public void onClick(View view) {
          //task 1
                ImageView iv;                                         // declaritaion          
                int i = 0;

                iv = new ImageView(MainActivity.this);                // make the imageview for the card i want to flip
                 
                iv.setTranslationX(400);                              // set all attributes
                iv.setTranslationY(1000);
                iv.setLayoutParams(layoutParams);

                String s = r.getdHand().getQ().get(0).getImg();       // get the image name of the card I want to flip (I already have the card stored in dealer hand)
                int imagekey = getResources().getIdentifier(s, "drawable", getPackageName());
                iv.setImageResource(imagekey);
                dealerPoints.setText(Integer.toString(r.getdHand().sum)); // change the text view that displays the dealer points

                main.addView(iv);                                        // add card
                                // here i want a delay of 1 second
                 // task 2
                while (r.getdHand().sum < 17) {
                    ImageView add;                                                       // declariation
                    int index = 2;

                    cards c = r.d.dealNextCard();                                        // add card to dealer hand
                    r.getdHand().add(c);

                    add = new ImageView(MainActivity.this);                         // make a new image view
                    add.setTranslationX(400 + 190 * index);
                    add.setTranslationY(1000);
                    add.setLayoutParams(layoutParams);

                    s = r.getdHand().getQ().get(index).getImg();                   // get the image name
                    imagekey = getResources().getIdentifier(s, "drawable", getPackageName());
                    add.setImageResource(imagekey);
                    main.addView(add);
                    dealerPoints.setText(Integer.toString(r.getdHand().sum));      // change the dealer points in the text view as before

                    index++;
                              // here i want another delay between every card creation
                }
            }
        });
EN

回答 1

Stack Overflow用户

发布于 2022-02-07 22:34:35

你可以用它来睡上一段时间

代码语言:javascript
复制
TimeUnit.SECONDS.sleep(1)

把它放在你想要延迟的地方

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71026186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档