在Android Studio中从类跳转到新活动

时间:2016-12-19 11:11:03

标签: android android-layout android-activity

我为我的项目制作了Flappy Bird。几乎所有部件都已完成,但我在最后部分卡住。我的目标是启动(来自游戏的活动,显示分数,玩家名称并将此数据存储在数据库中)。如果我知道如何启动那个新的Activity somwhow,我可以轻松地存储到数据库部分。

如果我需要更多上传一些东西,请告诉我

我试过了:

Issue starting activity from non-activity class

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

但仍然没有解决方案!我会在课堂上传游戏代码: 我的想法是从这个类开始这个新的Activity:

我在课程的不同部分尝试了上述链接的代码,但游戏并未通过屏幕卡在游戏中。

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

制作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);
    }
}

0 个答案:

没有答案