【问题标题】:How can I add a transition between activities in this code structure?如何在此代码结构中添加活动之间的转换?
【发布时间】:2022-01-25 07:43:18
【问题描述】:

我有一个代码结构。我有什么必要把这里的死屏,也就是角色死后的画面,转移到另一个java文件中。我一直在努力,但找不到。我创建了一个新的 java 文件,当角色死亡时,我想去那个屏幕并在那里显示分数。

这是一款飞扬的小鸟风格游戏。角色在击中来袭的生物时死亡。当它死掉时,我想将它重定向到一个新片段并在那里创建一个结构,例如 score 并重试。这是唯一的代码sn-p。

package com.nejdetkadirr.survivorbird;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Intersector;

import java.util.Random;

import javax.swing.JColorChooser;

public class SurvivorBird extends ApplicationAdapter {
    SpriteBatch batch;
    Texture background;
    Texture bird;
    Texture ufo1;
    Texture ufo2;
    Texture ufo3;
    float birdY=0;
    float birdX=0;
    int gameStart = 0;
    float velocity = 0; //Fast
    float ufoVelocity = 5; //Fast
    float gravity = 0.4f;
    int intNumberofUfo = 4;
    int score=0;
    int scoredUfo = 0;
    Random random;
    Circle birdCircle;
    ShapeRenderer shapeRenderer;
    BitmapFont font;
    float[] ufoX = new float[intNumberofUfo];
    float[] ufoOfset1 = new float[intNumberofUfo];
    float[] ufoOfset2 = new float[intNumberofUfo];
    float[] ufoOfset3 = new float[intNumberofUfo];
    float distance = 0;
    Circle[] ufoCircles1;
    Circle[] ufoCircles2;
    Circle[] ufoCircles3;

    @Override
    public void create () {
        batch = new SpriteBatch();
        background = new Texture("background.png");
        bird = new Texture("bird.png");
        ufo1 = new Texture("ufo.png");
        ufo2 = new Texture("ufo.png");
        ufo3 = new Texture("ufo.png");
        birdY=Gdx.graphics.getHeight()/2;
        birdX=Gdx.graphics.getWidth()/4;
        shapeRenderer = new ShapeRenderer();
        font = new BitmapFont();
        font.setColor(Color.WHITE);
        font.getData().setScale(4);
        birdCircle = new Circle();
        ufoCircles1 = new Circle[intNumberofUfo];
        ufoCircles2 = new Circle[intNumberofUfo];
        ufoCircles3 = new Circle[intNumberofUfo];

        distance = Gdx.graphics.getWidth()/2;
        random = new Random();
        for (int i = 0; i < intNumberofUfo; i++) {
            ufoOfset1[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);
            ufoOfset2[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);
            ufoOfset2[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);


            ufoX[i] = Gdx.graphics.getWidth() - ufo1.getWidth() / 2 + i * distance;
            ufoCircles1[i] = new Circle();
            ufoCircles3[i] = new Circle();
            ufoCircles2[i] = new Circle();

        }
    }

    @Override
    public void render () {
        batch.begin();

        //Draw background
        batch.draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

        if (gameStart == 1) {

            if (ufoX[scoredUfo] < birdX) {
                score++;
                if (scoredUfo < intNumberofUfo-1) {
                    scoredUfo++;
                } else {
                    scoredUfo=0;
                }
            }


            if (birdY >= Gdx.graphics.getHeight()-Gdx.graphics.getWidth()/13) {
                birdY = Gdx.graphics.getHeight()-Gdx.graphics.getWidth()/13;
                velocity+=gravity;
                birdY-=velocity;
            }

            if(Gdx.input.justTouched() && birdY < Gdx.graphics.getHeight()-Gdx.graphics.getWidth()/13) {

                    velocity-=8;
            }

            for (int i = 0; i < intNumberofUfo; i++) {
                if (ufoX[i] < 0) {
                    ufoX[i]+=intNumberofUfo*distance;
                    ufoOfset1[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);
                    ufoOfset2[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);
                    ufoOfset2[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);
                } else {
                    ufoX[i] -= ufoVelocity;
                }
                    batch.draw(ufo1,ufoX[i],Gdx.graphics.getHeight()/2  + ufoOfset1[i],Gdx.graphics.getWidth()/13,Gdx.graphics.getHeight()/10);
                    batch.draw(ufo2,ufoX[i],Gdx.graphics.getHeight()/2  + ufoOfset2[i],Gdx.graphics.getWidth()/13,Gdx.graphics.getHeight()/10);
                    batch.draw(ufo3,ufoX[i],Gdx.graphics.getHeight()/2  + ufoOfset3[i],Gdx.graphics.getWidth()/13,Gdx.graphics.getHeight()/10);

                    ufoCircles1[i] = new Circle(ufoX[i]+Gdx.graphics.getWidth()/26,Gdx.graphics.getHeight()/2  + ufoOfset1[i] +Gdx.graphics.getHeight()/20,Gdx.graphics.getWidth()/26);
                    ufoCircles2[i] = new Circle(ufoX[i]+Gdx.graphics.getWidth()/26,Gdx.graphics.getHeight()/2  + ufoOfset2[i] +Gdx.graphics.getHeight()/20,Gdx.graphics.getWidth()/26);
                    ufoCircles3[i] = new Circle(ufoX[i]+Gdx.graphics.getWidth()/26,Gdx.graphics.getHeight()/2  + ufoOfset3[i] +Gdx.graphics.getHeight()/20,Gdx.graphics.getWidth()/26);
            }



            if (birdY > 0) {
                velocity+=gravity;
                birdY-=velocity;
            } else {
                gameStart=2;
            }

        } else if (gameStart == 0){
            if(Gdx.input.justTouched()) {
                gameStart = 1;
            }
        } else if (gameStart == 2) {
            if(Gdx.input.justTouched()) {
                gameStart = 1;
                birdY=Gdx.graphics.getHeight()/2;
                for (int i = 0; i < intNumberofUfo; i++) {
                    ufoOfset1[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);
                    ufoOfset2[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);
                    ufoOfset2[i] = (random.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() - 200);


                    ufoX[i] = Gdx.graphics.getWidth() - ufo1.getWidth() / 2 + i * distance;
                    ufoCircles1[i] = new Circle();
                    ufoCircles3[i] = new Circle();
                    ufoCircles2[i] = new Circle();

                }

                velocity=0;
                scoredUfo=0;
                score=0;
            } else {
                font.draw(batch,"Game over! If you want to try again you can touch to screen :)",100,Gdx.graphics.getHeight()/2);
            }
        }



        //Draw bird
        batch.draw(bird,birdX,birdY,Gdx.graphics.getWidth()/13,Gdx.graphics.getHeight()/10);
        font.draw(batch,"Score : " + String.valueOf(score),100,200);
        batch.end();
        birdCircle.set(birdX+Gdx.graphics.getWidth()/26,birdY+Gdx.graphics.getHeight()/20,Gdx.graphics.getWidth()/26);
        /*
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(Color.BLUE);
        shapeRenderer.circle(birdCircle.x,birdCircle.y,birdCircle.radius);
        */
        for (int i = 0; i < intNumberofUfo; i++) {
            /*
            shapeRenderer.circle(ufoX[i]+Gdx.graphics.getWidth()/26,Gdx.graphics.getHeight()/2  + ufoOfset1[i] +Gdx.graphics.getHeight()/20,Gdx.graphics.getWidth()/26);
            shapeRenderer.circle(ufoX[i]+Gdx.graphics.getWidth()/26,Gdx.graphics.getHeight()/2  + ufoOfset2[i] +Gdx.graphics.getHeight()/20,Gdx.graphics.getWidth()/26);
            shapeRenderer.circle(ufoX[i]+Gdx.graphics.getWidth()/26,Gdx.graphics.getHeight()/2  + ufoOfset3[i] +Gdx.graphics.getHeight()/20,Gdx.graphics.getWidth()/26);
            */
            if (Intersector.overlaps(birdCircle,ufoCircles1[i]) || Intersector.overlaps(birdCircle,ufoCircles2[i]) || Intersector.overlaps(birdCircle,ufoCircles3[i])) {
                gameStart = 2;
                //System.out.println("collision detection");
            }
        }
        //shapeRenderer.end();

    }
    
    @Override
    public void dispose () {

    }
}

正是我想在这里添加一个过渡。我应该怎么做?

    Intent intent = new Intent(MainActivity.this, DashBoardActivity.class);
    startActivity(intent);

font.draw(batch,"Game over! If you want to try again you can touch to screen :)",100,Gdx.graphics.getHeight()/2);

【问题讨论】:

  • 这句话对我来说没有多大意义What is necessary for me to transfer the screen of death in this, that is, the screen after the character dies, to another java file 鉴于您提供的代码,您的目标并不完全清楚,这是您自己的代码吗?

标签: java android


【解决方案1】:

将此添加到您的动画文件夹中。

Intent intent = new Intent(MainActivity.this, DashBoardActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter_from_left, R.anim.exit_out_right);

enter_from_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set 
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="600"
        android:fromXDelta="100%"
        android:toXDelta="0%" >
    </translate>
</set>

enter_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set 
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="600"
        android:fromXDelta="0%"
        android:toXDelta="100%" >
    </translate>
</set>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多