第一次碰撞后如何让分数上升
目前我的分数在物体碰撞时不断增加,我希望在第一次碰撞后我的分数只上升一点。如何完成此操作并在碰撞时从屏幕上删除imageview?我认为这种情况正在发生,因为如果发生碰撞,thread.sleep每30毫秒更新一次并且每30毫米增加1分,但我不知道在第一次碰撞后如何检查和停止。
//collision bool and variables
public boolean Collision(ImageView net, ImageView ball)
{
Rect BallRect = new Rect();
ball.getHitRect(BallRect);
Rect NetRect = new Rect();
net.getHitRect(NetRect);
return BallRect.intersect(NetRect);
}
更新并检查碰撞
//increasing score
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(30);
runOnUiThread(new Runnable() {
@Override
public void run(){Render();}});}
}catch (InterruptedException e) {}}};
t.start();
}
public void Render()
{
changePos();
if(Collision(net, ball))
{
points += 1;//POINT INCREASE
this.score.setText("Score: " + points);
}
}
ALL MAIN.JAVA CODE TOGETHER:
public class MainActivity extends AppCompatActivity
{
//Layout
private RelativeLayout myLayout = null;
//Screen Size
private int screenWidth;
private int screenHeight;
//Position
private float ballDownY;
private float ballDownX;
//Initialize Class
private Handler handler = new Handler();
private Timer timer = new Timer();
//Images
private ImageView net = null;
private ImageView ball = null;
//score
private TextView score = null;
//for net movement along x-axis
public float x = 0;
public float y = 0;
//points
private int points = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
this.myLayout = (RelativeLayout) findViewById(R.id.myLayout);
this.score = (TextView) findViewById(R.id.score);
//net & ball imgs
this.net = (ImageView) findViewById(R.id.net);
this.ball = (ImageView) findViewById(R.id.ball);
//retrieving screen size
WindowManager wm = getWindowManager();
Display disp = wm.getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
//move to out of screen
this.ball.setX(-80.0f);
this.ball.setY(screenHeight + 80.0f);
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(30);
runOnUiThread(new Runnable() {
@Override
public void run(){Render();}});}
}catch (InterruptedException e) {}}};
t.start();
}
public void Render()
{
changePos();
if(Collision(net, ball))
{
points += 1;//POINT INCREASE
this.score.setText("Score: " + points);
}
}
//changes position of ball along top x-axis
public void changePos() {
//ball falls downwards
ballDownY += 20;
if (ball.getY() > screenHeight) {
ballDownX = (float) Math.floor((Math.random() * (screenWidth -
ball.getWidth())));
ballDownY = -100.0f;
}
ball.setY(ballDownY);
ball.setX(ballDownX);
//make net follow finger, along x and y axis
myLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
x = event.getX();
y = event.getY();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
net.setX(x);
net.setY(y);
}
return true;
}
});
}
//collision method and variables
public boolean Collision(ImageView net, ImageView ball)
{
Rect BallRect = new Rect();
ball.getHitRect(BallRect);
Rect NetRect = new Rect();
net.getHitRect(NetRect);
return BallRect.intersect(NetRect);
}
}
答案 0 :(得分:2)
为什么不使用布尔值来表示第一次碰撞?
//points
private int points = 0;
//indicate first collision
private boolean isCollide = false; //set default to false
然后在Render()
更改该布尔值
if(Collision(net, ball))
{
isCollide = true; //first collision occur
points += 1;//POINT INCREASE
this.score.setText("Score: " + points);
}
最后,停止并使视图消失
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(30);
runOnUiThread(new Runnable() {
@Override
public void run(){
if (isCollide) {
net.setVisibility(View.GONE);
ball.setVisibility(View.GONE);
} else {
Render();
}
}
});
if (isCollide) Thread.currentThread().interrupt(); // stop the thread
}
}catch (InterruptedException e) {}}};
t.start();