libgdx实现淡入淡出过渡

libgdx实现淡入淡出过渡

libgdx实现淡入淡出过渡,环境jdk17+libgdx 1.12.02023年11月1日11:02:50最新

依赖

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

    <dependencies>
        
        <dependency>
            <groupId>com.badlogicgames.gdxgroupId>
            <artifactId>gdxartifactId>
            <version>${gdx.version}version>
        dependency>
        
        <dependency>
            <groupId>com.badlogicgames.gdxgroupId>
            <artifactId>gdx-backend-lwjgl3artifactId>
            <version>${gdx.version}version>
        dependency>
        
        <dependency>
            <groupId>com.badlogicgames.gdxgroupId>
            <artifactId>gdx-platformartifactId>
            <version>${gdx.version}version>
            <classifier>natives-desktopclassifier>
        dependency>
        
        <dependency>
            <groupId>com.badlogicgames.gdxgroupId>
            <artifactId>gdx-freetypeartifactId>
            <version>${gdx.version}version>
        dependency>
        
        <dependency>
            <groupId>com.badlogicgames.gdxgroupId>
            <artifactId>gdx-freetype-platformartifactId>
            <version>${gdx.version}version>
            <classifier>natives-desktopclassifier>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13.2version>
        dependency>

        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>2.0.9version>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-simpleartifactId>
            <version>2.0.9version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.30version>
            <scope>providedscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.8.1version>
                <configuration>
                    <source>17source>
                    <target>17target>
                    
                    
                configuration>
            plugin>
        plugins>
    build>

    <repositories>
        <repository>
            <id>tencentid>
            <name>tencentname>
            <layout>defaultlayout>
            <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/url>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
            <releases>
                <enabled>trueenabled>
            releases>
        repository>
        <repository>
            <id>nexusid>
            <name>Nexusname>
            <layout>defaultlayout>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshotsurl>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
            <releases>
                <enabled>trueenabled>
            releases>
        repository>
        <repository>
            <id>aliyunmavenid>
            <url>https://maven.aliyun.com/repository/publicurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>

实现

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.ScreenUtils;
import org.junit.Test;

/**
 * @author lingkang
 * created by 2023/11/1
 */
public class TestFade extends ApplicationAdapter {
    @Test
    public void test() {
        Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
        config.setForegroundFPS(30);
        config.setTitle("yzcy");
        config.setWindowedMode(800, 600);
        new Lwjgl3Application(this, config);
    }

    Stage stage;
    Image img;
    Texture texture;
    long inTime = 0l;

    @Override
    public void create() {
        stage = new Stage();
        texture = new Texture(Gdx.files.internal("badlogic.jpg"));
        img = new Image(texture);
        img.setSize(texture.getWidth(), texture.getHeight());
        img.setOrigin(img.getWidth() / 2, img.getHeight() / 2);
        stage.addActor(img);

        float dur = 0.7f;
        //  Actions.moveBy 移动   Actions.scaleBy 大小    Actions.rotate 旋转
        // 淡入
        AlphaAction fadeIn = Actions.fadeIn(dur);
        img.addAction(fadeIn);
        // 点击图片,淡出
        AlphaAction fadeOut = Actions.fadeOut(dur);
        img.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                img.clearActions();
                img.addAction(fadeOut);
                return true;
            }
        });

        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render() {
        ScreenUtils.clear(Color.WHITE);
        stage.act();
        stage.draw();
    }

    @Override
    public void dispose() {
        stage.dispose();
        texture.dispose();
    }
}

效果

libgdx实现淡入淡出过渡_第1张图片

libgdx实现淡入淡出过渡_第2张图片

你可能感兴趣的:(libgdx,游戏开发,java,libgdx,游戏效果)