DavidBalas
DavidBalas

Reputation: 333

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

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();
        }
    }

}

Upvotes: 0

Views: 48

Answers (1)

bwegs
bwegs

Reputation: 3767

Assuming I understand your question correctly:

Every time the ball moves -- in your onSensorChanged(..) method -- you need to check if x == upper x bound or lower x bound and if y == upper y bound or lower y bound... If any of these conditions resolves to true that means the center of the ball is touching an edge and GameOver.java should be started:

EDIT:

// where 0 is the lower bound and 50 is the upper bound of our canvas
if(x <= 0 || x >= 50 || y <= 0 || y >= 50) {
    Intent myIntent = new Intent(this, GameOver.class);
    startActivity(myIntent);
}

Upvotes: 1

Related Questions