我正在建立一个游戏,游戏的目标是防止图像下降,我想要发生的是当图像到达屏幕的底部时,它将意图进行新的活动。请帮忙。我设法使图像下降,我想知道的是当图像到达屏幕底部时如何触发意图。
package com.example.crashthetrash;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class Game extends Activity {
int posy;
int posy2;
int posy3;
int posy4;
int posy5;
int posy6;
int dur;
int dur2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
dur = 5000;
dur2 = dur - 10;
final ImageView chips = (ImageView) findViewById(R.id.chips);
final TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 500.0f);
animation.setDuration(dur);
animation.setRepeatCount(0);
animation.setRepeatMode(0);
animation.setFillAfter(true);
chips.startAnimation(animation);
chips.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
posy = chips.getTop();
posy2 = 0 - posy;
dur2 = dur2 - 10;
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, posy2, 500.0f);
animation.setDuration(dur2);
animation.setRepeatCount(0);
animation.setRepeatMode(0);
animation.setFillAfter(true);
chips.startAnimation(animation);
return true;
}
});
final ImageView fish = (ImageView) findViewById(R.id.fish);
final TranslateAnimation animation2 = new TranslateAnimation(0.0f, 0.0f, 0.0f, 500.0f);
animation2.setDuration(dur);
animation2.setRepeatCount(0);
animation2.setRepeatMode(0);
animation2.setFillAfter(true);
fish.startAnimation(animation2);
fish.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
posy3 = fish.getTop();
posy4 = 0 - posy;
dur2 = dur2 - 10;
TranslateAnimation animation2 = new TranslateAnimation(0.0f, 0.0f, posy4, 500.0f);
animation2.setDuration(dur2);
animation2.setRepeatCount(0);
animation2.setRepeatMode(0);
animation2.setFillAfter(true);
fish.startAnimation(animation2);
return true;
}
});
final ImageView paper = (ImageView) findViewById(R.id.paper);
final TranslateAnimation animation3 = new TranslateAnimation(0.0f, 0.0f, 0.0f, 500.0f);
animation3.setDuration(dur);
animation3.setRepeatCount(0);
animation3.setRepeatMode(0);
animation3.setFillAfter(true);
paper.startAnimation(animation3);
paper.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
posy5 = paper.getTop();
posy6 = 0 - posy;
dur2 = dur2 - 10;
TranslateAnimation animation3 = new TranslateAnimation(0.0f, 0.0f, posy6, 500.0f);
animation3.setDuration(dur2);
animation3.setRepeatCount(0);
animation3.setRepeatMode(0);
animation3.setFillAfter(true);
paper.startAnimation(animation3);
return true;
}
});
if(animation.hasEnded() || animation2.hasEnded() || animation3.hasEnded())
{
Intent intent = new Intent(Game.this, Restart.class);
startActivity(intent);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
}
答案 0 :(得分:2)
if(animation.hasEnded() || animation2.hasEnded() || animation3.hasEnded())
{
Intent intent = new Intent(Game.this, Restart.class);
startActivity(intent);
}
应该在点击监听器之外。
答案 1 :(得分:1)
TranslateAnimation继承自动画对象并具有布尔值" hasEnded()"所以你可以测试这个值并触发你的新意图。
答案 2 :(得分:1)
if(animation.hasEnded())
{
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}