首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android:检测屏幕左侧边沿减去100 of

Android:检测屏幕左侧边沿减去100 of
EN

Stack Overflow用户
提问于 2015-10-13 02:43:55
回答 1查看 349关注 0票数 1

我有3张图片,从300 of、400 of和500 of的translationX开始。因此,当活动开始时,只有一个是可见的,而另一个则是屏幕外(右边)。它们在一个处理程序中运行,每50 in将它们向左移动30 in。我需要做的是检测到任何图像已经到达屏幕的左边边缘减去100 do,所以100 do离开屏幕,然后再将它们设置到右边的边缘,再加上100 do。

我已经研究了几个小时了,我似乎找不到任何方法来区分屏幕边缘所在的代码。我发现了类似的问题,但它们似乎都已经将屏幕边缘存储在变量中,并试图对其进行操作(但没有说明它们是如何得到该变量的)。

这是我一直在使用的,它工作了一段时间,但经过几次的ki爆炸消失了(突然只有两个将显示,然后1,最后一个).

代码语言:javascript
运行
复制
public void kiBlastHandler() {
    kiHandler = new Handler();
    kiHandler.postDelayed(kiRunnable, 50);
}

    private Runnable kiRunnable = new Runnable() {
        @Override
        public void run() {

            if (gameRunning) {
                // Ki Blasts
                ImageView kiBlast1 = (ImageView) findViewById(R.id.kiBlast1);
                ImageView kiBlast2 = (ImageView) findViewById(R.id.kiBlast2);
                ImageView kiBlast3 = (ImageView) findViewById(R.id.kiBlast3);

                // Ki blast movement
                kiBlast1.setX(kiBlast1.getX() - currentSpeed);
                kiBlast2.setX(kiBlast2.getX() - currentSpeed);
                kiBlast3.setX(kiBlast3.getX() - currentSpeed);

                // Images for lives
                ImageView imgLives = (ImageView) findViewById(R.id.imgLives);
                if (currentLives == 3) {
                    imgLives.setImageResource(R.drawable.lives_03);
                } else if (currentLives == 2) {
                    imgLives.setImageResource(R.drawable.lives_02);
                } else if (currentLives == 1) {
                    imgLives.setImageResource(R.drawable.lives_01);
                }

                // Ready the vibration for losing life
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

                // Handle losing lives
                if (kiBlast1.getTranslationX() == -1100) {
                    currentLives = (currentLives - 1);
                    v.vibrate(vibrateLives);
                    kiBlast1.setTranslationX(600);
                }

                if (kiBlast2.getTranslationX() == -1100) {
                    currentLives = (currentLives - 1);
                    v.vibrate(vibrateLives);
                    kiBlast2.setTranslationX(600);
                }

                if (kiBlast3.getTranslationX() == -1100) {
                    currentLives = (currentLives - 1);
                    v.vibrate(vibrateLives);
                    kiBlast3.setTranslationX(600);
                }
            }

            // No lives left
            if (currentLives == 0) {
                if (gameRunning) {
                    gameRunning = false;
                    gameOver();
                }
            }

            kiHandler.postDelayed(kiRunnable, 50);
        }
    };

有人能帮忙吗?我会很感激的!

EN

回答 1

Stack Overflow用户

发布于 2015-10-13 06:28:01

多亏了@MuhammadBabar的帮助,我才解决了这个问题。我用的是:

代码语言:javascript
运行
复制
if (kiBlast1.getTranslationX() == -1100) {// etc }

当我应该使用getX()函数时:

代码语言:javascript
运行
复制
if (kiBlast1.getX() <= (viewMain.getX() - 100)) {// etc }

而且它现在运行得很完美!我希望这能帮助其他人理解物体的坐标是如何工作的,因为它让我非常困惑!

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

https://stackoverflow.com/questions/33093265

复制
相关文章

相似问题

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