1

I am making a Flappy Bird for my project. Almost all the parts are done but i'm stuck at the final part. My goal is launch (An activity from game over class which displays the score, player name and store this data on the database ). I can do the storing to database part easily if i know how to launch the that new Activity somwhow.

If i need to upload somethings more, please tell me

I tried:

Issue starting activity from non-activity class

How can I start an Activity from a non-Activity class?

But still no solution! I will upload the code of the game over class: My thought is to start that new Activity from this class:

I tried the above link's code in diffrent parts of the class but the game doesn't give is stuck at the game over screen.

public class GamePlayScene implements Scene{

    int cHeight;
    int cWidth;

    float x;
    float y;

    private Rect r = new Rect();

    private Player player;
    private Point playerPoint;
    private ObstacleManager obstacleManager;
    private boolean movingPlayer = false;
    private boolean gameOver = false;
    private long gameOverTime;

    public GamePlayScene(){
        player = new Player(new Rect(100,100,200,200), Color.rgb(255,0,0));
        //starting position of the player
        playerPoint = new Point(Constants.SCREEN_WIDTH/2,3*Constants.SCREEN_HEIGHT/4);
        player.update(playerPoint);
        //make gamePanel focusable so it can handle events
        obstacleManager = new ObstacleManager(Color.BLACK);         //control the rectangles height width and gap
    }

    public void reset(){

        //starting position of the player
        playerPoint = new Point(Constants.SCREEN_WIDTH/2,3*Constants.SCREEN_HEIGHT/4);
        player.update(playerPoint);
        obstacleManager = new ObstacleManager(Color.BLACK);
        movingPlayer = false;
        score = 0;
    }

    @Override
    public void terminate(){
        SceneManager.ACTIVE_SCENE = 0;
    }

    @Override
    public void draw(Canvas canvas){
        canvas.drawColor(Color.WHITE);  //makes our canvas white except our player is red

        player.draw(canvas);
        obstacleManager.draw(canvas);

        if(gameOver){
            Paint paint = new Paint();
            paint.setTextSize(70);
            paint.setColor(Color.BLACK);
            x = cWidth / 2f - r.width() / 2f - r.left;
            y = cHeight / 2f + r.height() / 2f - r.bottom;
            drawCenterText(canvas,paint,"Game Over");

            x = cWidth / 2.4f - r.width() / 2f - r.left;
            y = cHeight / 1.9f + r.height() / 1f - r.bottom;
            paint.setColor(Color.BLACK);
            paint.setTextSize(65);
            drawCenterText(canvas,paint,"Click To Replay");
        }
    }

    @Override
    public void update(){
        if (!gameOver) {
            player.update(playerPoint);
            obstacleManager.update();
            if(obstacleManager.playerCollide(player)){
                gameOver = true;
                gameOverTime = System.currentTimeMillis();
            }
        }
    }

    @Override           //waiting after the game is over
    public void receiveTouch(MotionEvent event){
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(!gameOver && player.getRectangle().contains((int) event.getX(), (int)event.getY()))
                    movingPlayer = true;
                if(gameOver && System.currentTimeMillis() - gameOverTime >= 2000){
                    reset();
                    gameOver = false;
                }
                break;

            case MotionEvent.ACTION_MOVE:
                if(!gameOver && movingPlayer)
                    playerPoint.set((int)event.getX(),(int)event.getY());
                break;

            case MotionEvent.ACTION_UP:
                movingPlayer = false;
                break;
        }
    }

    //DRAWING GAME OVER TEXT
    private void drawCenterText(Canvas canvas, Paint paint, String text) {
        paint.setTextAlign(Paint.Align.LEFT);
        canvas.getClipBounds(r);
         cHeight = r.height();
         cWidth  = r.width();
        paint.getTextBounds(text, 0, text.length(), r);
        canvas.drawText(text, x, y, paint);
    }
}

Class that make the object of GamePlayscene

public class SceneManager {
    private ArrayList<Scene> scenes = new ArrayList<>();
    public static int ACTIVE_SCENE;

    public SceneManager( ){
        ACTIVE_SCENE = 0;
        scenes.add(new GamePlayScene());
    }

    public void recieveTouch(MotionEvent event){
        scenes.get(ACTIVE_SCENE).receiveTouch(event);
    }

    public void update(){
        scenes.get(ACTIVE_SCENE).update();
    }

    public void draw(Canvas canvas){
        scenes.get(ACTIVE_SCENE).draw(canvas);
    }
}
Community
  • 1
  • 1

0 Answers0