Reputation: 13
i am building a game, the goal of the game is prevent the images to fall, what i want to happen is that when the image reaches the bottom of the screen, it will intent to a new activity .please help. i manage to make the images fall, what i want to know is how to trigger intent when the image reaches the bottom of the screen.
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;
}
}
Upvotes: 1
Views: 85
Reputation: 148
if(animation.hasEnded() || animation2.hasEnded() || animation3.hasEnded())
{
Intent intent = new Intent(Game.this, Restart.class);
startActivity(intent);
}
Should be outside the click listener.
Upvotes: 2
Reputation: 148
if(animation.hasEnded())
{
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
Upvotes: 1
Reputation: 148
TranslateAnimation inherit from animation object and has a boolean "hasEnded()" so you can test this value and trigger you new intent.
Upvotes: 1