dimanche 6 juillet 2014

How to make my app intent to an other screen when something happens


Vote count:

0




Well, I have an app with a ball, moving using the accelerometer sensor, now, I want it to intent me to the screen "GameOver.java" when the ball is touching the corners (top,bottom,left and right of the screen).


Here is my code :



package com.example.theball;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;

@SuppressWarnings("deprecation") public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerometer;
private long lastUpdate;

AnimatedView animatedView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;

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

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
lastUpdate = System.currentTimeMillis();

animatedView = new AnimatedView(this);
setContentView(animatedView);
}

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}


@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

x -= (int) event.values[0];
y += (int) event.values[1];

}
}

public class AnimatedView extends ImageView {

static final int width = 50;
static final int height = 50;

public AnimatedView(Context context) {
super(context);
// TODO Auto-generated constructor stub

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xffffAC23);
mDrawable.setBounds(x, y, x + width, y + height);

}

@Override
protected void onDraw(Canvas canvas) {

mDrawable.setBounds(x, y, x + width, y + height);
mDrawable.draw(canvas);
invalidate();
}
}

}


asked 27 secs ago






Aucun commentaire:

Enregistrer un commentaire