我正在制作一款游戏,当游戏首次加载时,障碍物会以随机确定的速度从屏幕顶部下降。
public final int ranObNum1 = randomWithRange(1,30);
我的游戏以gameMenu()
,startGame()
和endGameMenu()
的周期运行。我的问题是,在每个周期之后,速度会因某种原因而增加。例如,速度可以从每25毫升15px开始,但每当startGame()
再次运行时增加。
每25mls运行的循环是:
ob1.setY((ob1.getY()) + ranObNum1);
ob1
是在屏幕上运行的16个ImageView之一。
我的想法是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]);
}
}
答案 0 :(得分:0)
我怀疑你没有取消你拨打startGame()
时开始的循环。当您致电endGame()
时,视频仍然会在屏幕上向下转换,因为循环仍在继续,因此当您再次致电startGame()
时,每个视图都会翻译两次,一次用于之前未取消的旧循环,另一次用于当前循环。
除此之外,您应该考虑使用某种数组或列表重新编写代码,这样您就不需要跟踪和操作大量的实例变量。首先,在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);
}
}
然后,只要您想设置视图位置和可见性状态等属性,就可以通过迭代来更轻松地操作此列表。