Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像视图的增长速度_Java_Android_Debugging_Imageview - Fatal编程技术网

Java 图像视图的增长速度

Java 图像视图的增长速度,java,android,debugging,imageview,Java,Android,Debugging,Imageview,我正在创建一个游戏,其中障碍物从屏幕顶部以游戏首次加载时随机确定的速度落下 public final int ranObNum1 = randomWithRange(1,30); 我的游戏在gameMenu()、startGame()和endGameMenu()的循环中运行。我的问题是,每次循环后,速度都会因为某种原因而增加。例如,速度可能从每25ml 15px开始,但只要再次运行startGame(),速度就会增加 每25英里运行一次的循环是: ob1.set

我正在创建一个游戏,其中障碍物从屏幕顶部以游戏首次加载时随机确定的速度落下

public final int ranObNum1 = randomWithRange(1,30);
我的游戏在
gameMenu()
startGame()
endGameMenu()
的循环中运行。我的问题是,每次循环后,速度都会因为某种原因而增加。例如,速度可能从每25ml 15px开始,但只要再次运行
startGame()
,速度就会增加

每25英里运行一次的循环是:

                ob1.setY((ob1.getY()) + ranObNum1);
ob1
是屏幕上运行的16个图像视图之一

我的想法是,
ranObNum1
必须以某种方式增加,因为它们的移动方式是采用当前的
Y
位置并添加
ranObNum
,但我不知道会发生在哪里

任何帮助都将不胜感激,如果您需要更多的澄清,我很乐意提供

package com.jayrow.dodge.app;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Handler;
import android.widget.Toast;

import java.util.TimerTask;



public class MainActivity extends Activity {

//Checks if the game is started.
public boolean start;

public final int[] score = {0};

public static final int ranObNum1 = randomWithRange(1,30);
public static final int ranObNum2 = randomWithRange(1,30);
public static final int ranObNum3 = randomWithRange(1,30);
public static final int ranObNum4 = randomWithRange(1,30);
public static final int ranObNum5 = randomWithRange(1,30);
public static final int ranObNum6 = randomWithRange(1,30);
public static final int ranObNum7 = randomWithRange(1,30);
public static final int ranObNum8 = randomWithRange(1,30);
public static final int ranObNum9 = randomWithRange(1,30);
public static final int ranObNum10 = randomWithRange(1,30);
public static final int ranObNum11 = randomWithRange(1,30);
public static final int ranObNum12 = randomWithRange(1,30);
public static final int ranObNum13 = randomWithRange(1,30);
public static final int ranObNum14 = randomWithRange(1,30);
public static final int ranObNum15 = randomWithRange(1,30);
public static final int ranObNum16 = randomWithRange(1,30);



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainMenu();

}

public void mainMenu(){

    start = false;

    final TextView lose = (TextView) findViewById(R.id.youLose);
    final TextView highScoreT = (TextView) findViewById(R.id.highScoreText);
    final TextView playAgainT = (TextView) findViewById(R.id.playAgain);
    lose.setVisibility(View.INVISIBLE);
    highScoreT.setVisibility(View.INVISIBLE);
    playAgainT.setVisibility(View.INVISIBLE);

    //Retrieving views and putting them into variables.
    final ImageView ob1 = (ImageView) findViewById(R.id.obstacle1);
    ImageView ob2 = (ImageView) findViewById(R.id.obstacle2);
    ImageView ob3 = (ImageView) findViewById(R.id.obstacle3);
    ImageView ob4 = (ImageView) findViewById(R.id.obstacle4);
    ImageView ob5 = (ImageView) findViewById(R.id.obstacle5);
    ImageView ob6 = (ImageView) findViewById(R.id.obstacle6);
    ImageView ob7 = (ImageView) findViewById(R.id.obstacle7);
    ImageView ob8 = (ImageView) findViewById(R.id.obstacle8);
    ImageView ob9 = (ImageView) findViewById(R.id.obstacle9);
    ImageView ob10 = (ImageView) findViewById(R.id.obstacle10);
    ImageView ob11 = (ImageView) findViewById(R.id.obstacle11);
    ImageView ob12 = (ImageView) findViewById(R.id.obstacle12);
    ImageView ob13 = (ImageView) findViewById(R.id.obstacle13);
    ImageView ob14 = (ImageView) findViewById(R.id.obstacle14);
    ImageView ob15 = (ImageView) findViewById(R.id.obstacle15);
    ImageView ob16 = (ImageView) findViewById(R.id.obstacle16);

    //Start off with all of the obstacles invisible
    ob1.setVisibility(View.INVISIBLE);
    ob2.setVisibility(View.INVISIBLE);
    ob3.setVisibility(View.INVISIBLE);
    ob4.setVisibility(View.INVISIBLE);
    ob5.setVisibility(View.INVISIBLE);
    ob6.setVisibility(View.INVISIBLE);
    ob7.setVisibility(View.INVISIBLE);
    ob8.setVisibility(View.INVISIBLE);
    ob9.setVisibility(View.INVISIBLE);
    ob10.setVisibility(View.INVISIBLE);
    ob11.setVisibility(View.INVISIBLE);
    ob12.setVisibility(View.INVISIBLE);
    ob13.setVisibility(View.INVISIBLE);
    ob14.setVisibility(View.INVISIBLE);
    ob15.setVisibility(View.INVISIBLE);
    ob16.setVisibility(View.INVISIBLE);

    //Putting the 3 titles into variables
    TextView title1 = (TextView) findViewById(R.id.title);
    TextView title2 = (TextView) findViewById(R.id.byLine);
    TextView title3 = (TextView) findViewById(R.id.tapToPlay);

    //We want these to start off visible when the game first loads.
    title1.setVisibility(View.VISIBLE);
    title2.setVisibility(View.VISIBLE);
    title3.setVisibility(View.VISIBLE);

    title3.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int eventAction = event.getAction();

            switch (eventAction) {
                case MotionEvent.ACTION_DOWN:
                    //On button down
                    startGame();
                    break;

                case MotionEvent.ACTION_UP:
                    // finger leaves the button
                    endGame();
                    break;
            }

            return false;
        }
    });

    score[0] = 0;
    final TextView scoreT = (TextView) findViewById(R.id.scoreView);
    scoreT.setText("" + score[0]);

}

public void endGame(){

    start = false;

    final ImageView ob1 = (ImageView) findViewById(R.id.obstacle1);
    final ImageView ob2 = (ImageView) findViewById(R.id.obstacle2);
    final ImageView ob3 = (ImageView) findViewById(R.id.obstacle3);
    final ImageView ob4 = (ImageView) findViewById(R.id.obstacle4);
    final ImageView ob5 = (ImageView) findViewById(R.id.obstacle5);
    final ImageView ob6 = (ImageView) findViewById(R.id.obstacle6);
    final ImageView ob7 = (ImageView) findViewById(R.id.obstacle7);
    final ImageView ob8 = (ImageView) findViewById(R.id.obstacle8);
    final ImageView ob9 = (ImageView) findViewById(R.id.obstacle9);
    final ImageView ob10 = (ImageView) findViewById(R.id.obstacle10);
    final ImageView ob11 = (ImageView) findViewById(R.id.obstacle11);
    final ImageView ob12 = (ImageView) findViewById(R.id.obstacle12);
    final ImageView ob13 = (ImageView) findViewById(R.id.obstacle13);
    final ImageView ob14 = (ImageView) findViewById(R.id.obstacle14);
    final ImageView ob15 = (ImageView) findViewById(R.id.obstacle15);
    final ImageView ob16 = (ImageView) findViewById(R.id.obstacle16);

    ob1.setVisibility(View.INVISIBLE);
    ob2.setVisibility(View.INVISIBLE);
    ob3.setVisibility(View.INVISIBLE);
    ob4.setVisibility(View.INVISIBLE);
    ob5.setVisibility(View.INVISIBLE);
    ob6.setVisibility(View.INVISIBLE);
    ob7.setVisibility(View.INVISIBLE);
    ob8.setVisibility(View.INVISIBLE);
    ob9.setVisibility(View.INVISIBLE);
    ob10.setVisibility(View.INVISIBLE);
    ob11.setVisibility(View.INVISIBLE);
    ob12.setVisibility(View.INVISIBLE);
    ob13.setVisibility(View.INVISIBLE);
    ob14.setVisibility(View.INVISIBLE);
    ob15.setVisibility(View.INVISIBLE);
    ob16.setVisibility(View.INVISIBLE);

    final TextView scoreT = (TextView) findViewById(R.id.scoreView);
    scoreT.setVisibility(View.INVISIBLE);

    final TextView lose = (TextView) findViewById(R.id.youLose);
    final TextView highScoreT = (TextView) findViewById(R.id.highScoreText);
    final TextView playAgainT = (TextView) findViewById(R.id.playAgain);
    lose.setText("You Lost With a Score of " + score[0]);
    lose.setVisibility(View.VISIBLE);
    highScoreT.setVisibility(View.VISIBLE);
    playAgainT.setVisibility(View.VISIBLE);

    playAgainT.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int eventAction = event.getAction();

            switch (eventAction) {
                case MotionEvent.ACTION_DOWN:
                    //On button down
                    mainMenu();
                    break;

                case MotionEvent.ACTION_UP:
                    // finger leaves the button
                    break;
            }

            return false;
        }
    });

}

//A random number generator that will make a random number within the specified range.
public static int randomWithRange(int mi, int ma)
{
    final int range = (ma - mi) + 1;
    return (int)(Math.random() * range) + mi;
}

//This is called when the user selects, "Tap to play"
public void startGame (){

    final TextView lose = (TextView) findViewById(R.id.youLose);
    final TextView highScoreT = (TextView) findViewById(R.id.highScoreText);
    final TextView playAgainT = (TextView) findViewById(R.id.playAgain);
    lose.setVisibility(View.INVISIBLE);
    highScoreT.setVisibility(View.INVISIBLE);
    playAgainT.setVisibility(View.INVISIBLE);

    //Now the game has started.
    start = true;

    //We need to find the devices pixel width and height for use later in the game, since there are many different screen sizes.
    final int width = 720;
    final int height = 1280;

    //Storing and hiding the textviews.
    final TextView title1 = (TextView) findViewById(R.id.title);
    final TextView title2 = (TextView) findViewById(R.id.byLine);
    final TextView title3 = (TextView) findViewById(R.id.tapToPlay);
    title1.setVisibility(View.INVISIBLE);
    title2.setVisibility(View.INVISIBLE);
    title3.setVisibility(View.INVISIBLE);

    //Initialize and make the score visible
    final TextView scoreT = (TextView) findViewById(R.id.scoreView);
    scoreT.setVisibility(View.VISIBLE);

    //Naming the obstacles into variables
    final ImageView ob1 = (ImageView) findViewById(R.id.obstacle1);
    final ImageView ob2 = (ImageView) findViewById(R.id.obstacle2);
    final ImageView ob3 = (ImageView) findViewById(R.id.obstacle3);
    final ImageView ob4 = (ImageView) findViewById(R.id.obstacle4);
    final ImageView ob5 = (ImageView) findViewById(R.id.obstacle5);
    final ImageView ob6 = (ImageView) findViewById(R.id.obstacle6);
    final ImageView ob7 = (ImageView) findViewById(R.id.obstacle7);
    final ImageView ob8 = (ImageView) findViewById(R.id.obstacle8);
    final ImageView ob9 = (ImageView) findViewById(R.id.obstacle9);
    final ImageView ob10 = (ImageView) findViewById(R.id.obstacle10);
    final ImageView ob11 = (ImageView) findViewById(R.id.obstacle11);
    final ImageView ob12 = (ImageView) findViewById(R.id.obstacle12);
    final ImageView ob13 = (ImageView) findViewById(R.id.obstacle13);
    final ImageView ob14 = (ImageView) findViewById(R.id.obstacle14);
    final ImageView ob15 = (ImageView) findViewById(R.id.obstacle15);
    final ImageView ob16 = (ImageView) findViewById(R.id.obstacle16);



        //Here, all of the obstacles are set to be visible and given a random X coordinate.
        ob1.setVisibility(View.VISIBLE);
        ob1.setX(randomWithRange(0, width));
        ob1.setY(0);
        ob2.setVisibility(View.VISIBLE);
        ob2.setX(randomWithRange(0, width));
        ob2.setY(0);
        ob3.setVisibility(View.VISIBLE);
        ob3.setX(randomWithRange(0, width));
        ob3.setY(0);
        ob4.setVisibility(View.VISIBLE);
        ob4.setX(randomWithRange(0, width));
        ob4.setY(0);
        ob5.setVisibility(View.VISIBLE);
        ob5.setX(randomWithRange(0, width));
        ob5.setY(0);
        ob6.setVisibility(View.VISIBLE);
        ob6.setX(randomWithRange(0, width));
        ob6.setY(0);
        ob7.setVisibility(View.VISIBLE);
        ob7.setX(randomWithRange(0, width));
        ob7.setY(0);
        ob8.setVisibility(View.VISIBLE);
        ob8.setX(randomWithRange(0, width));
        ob8.setY(0);
        ob9.setVisibility(View.VISIBLE);
        ob9.setX(randomWithRange(0, width));
        ob9.setY(0);
        ob10.setVisibility(View.VISIBLE);
        ob10.setX(randomWithRange(0, width));
        ob10.setY(0);
        ob11.setVisibility(View.VISIBLE);
        ob11.setX(randomWithRange(0, width));
        ob11.setY(0);
        ob12.setVisibility(View.VISIBLE);
        ob12.setX(randomWithRange(0, width));
        ob12.setY(0);
        ob13.setVisibility(View.VISIBLE);
        ob13.setX(randomWithRange(0, width));
        ob13.setY(0);
        ob14.setVisibility(View.VISIBLE);
        ob14.setX(randomWithRange(0, width));
        ob14.setY(0);
        ob15.setVisibility(View.VISIBLE);
        ob15.setX(randomWithRange(0, width));
        ob15.setY(0);
        ob16.setVisibility(View.VISIBLE);
        ob16.setX(randomWithRange(0, width));
        ob16.setY(0);

        //The speeds of the asteroids are randomly defined.
        final Handler h = new Handler();
        final int delay = 25; //milliseconds
        final int[] scoreDelay = {1000}; //milliseconds


        scoreDelay[0] = 1000;

        //A loop that will run ever 25 miliseconds is started.
        h.postDelayed(new Runnable() {
            public void run() {

                //Now the obstacles fall at the speed specified earlier.
                ob1.setY((ob1.getY()) + ranObNum1);
                ob2.setY((ob2.getY()) + ranObNum2);
                ob3.setY((ob3.getY()) + ranObNum3);
                ob4.setY((ob4.getY()) + ranObNum4);
                ob5.setY((ob5.getY()) + ranObNum5);
                ob6.setY((ob6.getY()) + ranObNum6);
                ob7.setY((ob7.getY()) + ranObNum7);
                ob8.setY((ob8.getY()) + ranObNum8);
                ob9.setY((ob9.getY()) + ranObNum9);
                ob10.setY((ob10.getY()) + ranObNum10);
                ob11.setY((ob11.getY()) + ranObNum11);
                ob12.setY((ob12.getY()) + ranObNum12);
                ob13.setY((ob13.getY()) + ranObNum13);
                ob14.setY((ob14.getY()) + ranObNum14);
                ob15.setY((ob15.getY()) + ranObNum15);
                ob16.setY((ob16.getY()) + ranObNum16);

                //If one of the obstacles hits the bottom of the screen, regenerate it up top with a random X coordinate and increase score by one.
                if (ob1.getY() > height) {
                    ob1.setX(randomWithRange(0, width));
                    ob1.setY(0);

                } else if (ob2.getY() > height) {
                    ob2.setX(randomWithRange(0, width));
                    ob2.setY(0);

                } else if (ob3.getY() > height) {
                    ob3.setX(randomWithRange(0, width));
                    ob3.setY(0);

                } else if (ob4.getY() > height) {
                    ob4.setX(randomWithRange(0, width));
                    ob4.setY(0);

                } else if (ob5.getY() > height) {
                    ob5.setX(randomWithRange(0, width));
                    ob5.setY(0);

                } else if (ob6.getY() > height) {
                    ob6.setX(randomWithRange(0, width));
                    ob6.setY(0);

                } else if (ob7.getY() > height) {
                    ob7.setX(randomWithRange(0, width));
                    ob7.setY(0);

                } else if (ob8.getY() > height) {
                    ob8.setX(randomWithRange(0, width));
                    ob8.setY(0);

                } else if (ob9.getY() > height) {
                    ob9.setX(randomWithRange(0, width));
                    ob9.setY(0);

                } else if (ob10.getY() > height) {
                    ob10.setX(randomWithRange(0, width));
                    ob10.setY(0);

                } else if (ob11.getY() > height) {
                    ob11.setX(randomWithRange(0, width));
                    ob11.setY(0);

                } else if (ob12.getY() > height) {
                    ob12.setX(randomWithRange(0, width));
                    ob12.setY(0);

                } else if (ob13.getY() > height) {
                    ob13.setX(randomWithRange(0, width));
                    ob13.setY(0);

                } else if (ob14.getY() > height) {
                    ob14.setX(randomWithRange(0, width));
                    ob14.setY(0);

                } else if (ob15.getY() > height) {
                    ob15.setX(randomWithRange(0, width));
                    ob15.setY(0);

                } else if (ob16.getY() > height) {
                    ob16.setX(randomWithRange(0, width));
                    ob16.setY(0);

                }

                ob1.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                        int eventAction = event.getAction();
                        switch (eventAction) {
                            case MotionEvent.ACTION_MOVE:
                                //On button down
                                endGame();
                                break;
                        }
                        return false;
                    }
                });


                h.postDelayed(this, delay);
            }
        }, delay);


        h.postDelayed(new Runnable() {
            public void run() {

                if (start = true) {

                    score[0] = score[0] + 1;
                }
                scoreT.setText("" + score[0]);


                h.postDelayed(this, scoreDelay[0]);
            }
        }, scoreDelay[0]);
    }
}

我怀疑您没有取消调用
startGame()
时启动的循环。当您调用
endGame()
时,视图仍会在屏幕上向下转换,因为循环仍在进行,因此当您再次调用
startGame()
时,每个视图都会转换两次,一次用于以前未取消的旧循环,另一次用于当前循环

此外,您应该考虑使用某种数组或列表来重新编写代码,这样就不必跟踪和处理GasLiLLY实例变量。首先,在xml布局中为每个视图设置一个标记,以便标记遵循特定的命名约定(object1、object2、object3)。另外,在布局中的根视图上设置id。然后,更改此选项:

 //Retrieving views and putting them into variables.
final ImageView ob1 = (ImageView) findViewById(R.id.obstacle1);
ImageView ob2 = (ImageView) findViewById(R.id.obstacle2);
ImageView ob3 = (ImageView) findViewById(R.id.obstacle3);
ImageView ob4 = (ImageView) findViewById(R.id.obstacle4);
ImageView ob5 = (ImageView) findViewById(R.id.obstacle5);
ImageView ob6 = (ImageView) findViewById(R.id.obstacle6);
ImageView ob7 = (ImageView) findViewById(R.id.obstacle7);
ImageView ob8 = (ImageView) findViewById(R.id.obstacle8);
ImageView ob9 = (ImageView) findViewById(R.id.obstacle9);
ImageView ob10 = (ImageView) findViewById(R.id.obstacle10);
ImageView ob11 = (ImageView) findViewById(R.id.obstacle11);
ImageView ob12 = (ImageView) findViewById(R.id.obstacle12);
ImageView ob13 = (ImageView) findViewById(R.id.obstacle13);
ImageView ob14 = (ImageView) findViewById(R.id.obstacle14);
ImageView ob15 = (ImageView) findViewById(R.id.obstacle15);
ImageView ob16 = (ImageView) findViewById(R.id.obstacle16);

//Start off with all of the obstacles invisible
ob1.setVisibility(View.INVISIBLE);
ob2.setVisibility(View.INVISIBLE);
ob3.setVisibility(View.INVISIBLE);
ob4.setVisibility(View.INVISIBLE);
ob5.setVisibility(View.INVISIBLE);
ob6.setVisibility(View.INVISIBLE);
ob7.setVisibility(View.INVISIBLE);
ob8.setVisibility(View.INVISIBLE);
ob9.setVisibility(View.INVISIBLE);
ob10.setVisibility(View.INVISIBLE);
ob11.setVisibility(View.INVISIBLE);
ob12.setVisibility(View.INVISIBLE);
ob13.setVisibility(View.INVISIBLE);
ob14.setVisibility(View.INVISIBLE);
ob15.setVisibility(View.INVISIBLE);
ob16.setVisibility(View.INVISIBLE);
为此:

ArrayList<ImageView> imageViews = new ArrayList<ImageView>();
View rootView = findViewById(R.id.rootView);
ArrayList<View> touchables = rootView.getTouchables();
for(int i = 0; i < touchables.size(); i++)
{
   View touchable = touchables.get(i);
   if(touchable.getTag().toString().contains("object")
   {
      imageViews.add((ImageView) touchable);
   }
}
ArrayList ImageView=新建ArrayList();
视图rootView=findviewbyd(R.id.rootView);
ArrayList touchables=rootView.getTouchables();
对于(int i=0;i

然后,只要您想设置视图位置和可见性状态等属性,就可以通过迭代来操作此列表,操作起来要容易得多。

您可以发布更多代码吗?可能是整个类文件吗?如果没有更多信息,我们将无能为力。我使用
h[0]使可运行项无效。removeCallbacksAndMessages(null);
。非常感谢,您的解决方案非常有效!