libgdx相机拖动、场景拖动、实现明日方舟平滑拖动效果

2023年11月22日10:39:05最新依赖版本

GIF 2023-11-22 10-36-18

package camera;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.DragListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import org.junit.jupiter.api.Test;

/**
 * @author lingkang
 * created by 2023/11/13
 * 实现明日方舟拖动效果
 */
public class MoveCameraStage extends ApplicationAdapter {

    @Test
    public void test() {
        Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
        config.setForegroundFPS(60);
        config.setTitle("yzcy");
        config.setWindowedMode(800, 600);
        new Lwjgl3Application(this, config);
    }


    private OrthographicCamera cam;
    private SpriteBatch batch;

    private Sprite mapSprite;
    private Stage stage;
    private Viewport viewport;

    @Override
    public void create() {
        batch = new SpriteBatch();

        int w = Gdx.graphics.getWidth();
        int h = Gdx.graphics.getHeight();
        mapSprite = new Sprite(new Texture(Gdx.files.classpath("img/bg/bg01.png")));
        mapSprite.setPosition(0, 0);
        mapSprite.setSize(1000, 600);

        cam = new OrthographicCamera(h, h);
        cam.position.set(cam.viewportWidth / 2, cam.viewportHeight / 2f, 0);
        cam.update();

        viewport = new ScreenViewport(cam);

        stage = new Stage(viewport);

        stage.addListener(new DragListener() {
            private float o = 0;

            @Override
            public void touchDragged(InputEvent event, float x, float y, int pointer) {
                if (o == 0 && x != 0) {
                    o = x;
                    return;
                }
                cam.position.x = cam.position.x + (x - o);
                o = 0;
                return;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                o = 0;// 拖动释放时,要把原点归零
                super.touchUp(event, x, y, pointer, button);
            }
        });
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render() {
        handleInput();
        cam.update();
        batch.setProjectionMatrix(cam.combined);

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        mapSprite.draw(batch);
        batch.end();

        stage.draw();
    }

    private void handleInput() {
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            cam.translate(-3, 0, 0);
        }
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            cam.translate(3, 0, 0);
        }

        cam.position.x = MathUtils.clamp(cam.position.x, cam.viewportWidth / 2, cam.viewportWidth / 2 + 200);
    }

    public void dispose() {
        mapSprite.getTexture().dispose();
        batch.dispose();
        stage.draw();
    }
}

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.lingkang</groupId>
    <artifactId>yzcy</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <gdx.version>1.12.0</gdx.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx -->
        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx</artifactId>
            <version>${gdx.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-backend-lwjgl3 -->
        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx-backend-lwjgl3</artifactId>
            <version>${gdx.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-platform -->
        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx-platform</artifactId>
            <version>${gdx.version}</version>
            <classifier>natives-desktop</classifier>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype -->
        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx-freetype</artifactId>
            <version>${gdx.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype-platform -->
        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx-freetype-platform</artifactId>
            <version>${gdx.version}</version>
            <classifier>natives-desktop</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.10.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <!-- 编译后保持方法形参名称不变 -->
                    <!--<compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>-->
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>tencent</id>
            <name>tencent</name>
            <layout>default</layout>
            <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
        <repository>
            <id>nexus1</id>
            <name>Nexus1</name>
            <layout>default</layout>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>nexus</id>
            <name>Nexus</name>
            <layout>default</layout>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>aliyunmaven</id>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>