libgdx [Stage]

本文深入探讨了游戏开发领域的核心技术,包括Unity3D引擎的应用、游戏引擎的比较、游戏程序、游戏美术、游戏策划等关键环节,为游戏开发者提供了一站式的学习指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

StateStatus.java

package com.mygdx.game;

public class StateStatus {
   public static int MAINMENU_STAGE = 1;
   public static int GAME_STAGE = 2;
   public static int STORE_STAGE = 3;

   public static int flag = MAINMENU_STAGE;
}

GameStage.java

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.ui.Image;

public class GameStage extends Stage {

   Texture texture;
   TextureRegion textureRegion;
   Image bgImage;
   Image newGameImage;
   TextureRegion newGameTextureRegion;

   public GameStage() {
       super();
       init();
   }

   public void init() {
       texture = new Texture(Gdx.files.internal("data/game_stage.jpg"));
       textureRegion = new TextureRegion(texture, 0, 48, 256, 208);
       bgImage = new Image(textureRegion);
       bgImage.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

       newGameTextureRegion = new TextureRegion(texture, 0, 0, 256, 48);
       newGameImage = new Image(newGameTextureRegion);
       newGameImage.setPosition((Gdx.graphics.getWidth()-256 * 2)/2, (Gdx.graphics.getHeight()-48)/2);
       newGameImage.setSize(256 * 2, 48 *2);

       this.addActor(bgImage);
       this.addActor(newGameImage);

       newGameImage.addListener(new InputListener(){
           @Override
           public boolean touchDown(InputEvent event, float x, float y,
                   int pointer, int button) {
               StateStatus.flag = StateStatus.STORE_STAGE;
               return true;
           }            
       });
   }
}

MainMenuStage.java

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.ui.Image;

public class MainMenuStage extends Stage {

   Texture texture;
   TextureRegion textureRegion;
   Image bgImage;
   Image newGameImage;
   TextureRegion newGameTextureRegion;

   public MainMenuStage() {
       super();
       init();
   }

   public void init() {
       texture = new Texture(Gdx.files.internal("data/mainmenu_stage.jpg"));
       textureRegion = new TextureRegion(texture, 0, 48, 256, 208);
       bgImage = new Image(textureRegion);
       bgImage.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

       newGameTextureRegion = new TextureRegion(texture, 0, 0, 256, 48);
       newGameImage = new Image(newGameTextureRegion);
       newGameImage.setPosition((Gdx.graphics.getWidth()-256 * 2)/2, (Gdx.graphics.getHeight()-48)/2);
       newGameImage.setSize(256 * 2, 48 *2);

       this.addActor(bgImage);
       this.addActor(newGameImage);

       newGameImage.addListener(new InputListener(){
           @Override
           public boolean touchDown(InputEvent event, float x, float y,
                   int pointer, int button) {
               StateStatus.flag = StateStatus.GAME_STAGE;
               return true;
           }            
       });
   }
}

StoreStage.java

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.ui.Image;

public class StoreStage extends Stage {

   Texture texture;
   TextureRegion textureRegion;
   Image bgImage;
   Image newGameImage;
   TextureRegion newGameTextureRegion;

   public StoreStage() {
       super();
       init();
   }

   public void init() {
       texture = new Texture(Gdx.files.internal("data/store_stage.jpg"));
       textureRegion = new TextureRegion(texture, 0, 48, 256, 208);
       bgImage = new Image(textureRegion);
       bgImage.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

       newGameTextureRegion = new TextureRegion(texture, 0, 0, 256, 48);
       newGameImage = new Image(newGameTextureRegion);
       newGameImage.setPosition((Gdx.graphics.getWidth()-256 * 2)/2, (Gdx.graphics.getHeight()-48)/2);
       newGameImage.setSize(256 * 2, 48 *2);

       this.addActor(bgImage);
       this.addActor(newGameImage);

       newGameImage.addListener(new InputListener(){
           @Override
           public boolean touchDown(InputEvent event, float x, float y,
                   int pointer, int button) {
               StateStatus.flag = StateStatus.MAINMENU_STAGE;
               return true;
           }            
       });
   }
}

MyGdxGame.java

package com.mygdx.game;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

public class MyGdxGame extends ApplicationAdapter {

    GameStage gameStage;
    MainMenuStage mainMenuStage;
    StoreStage storeStage;

    @Override
    public void create() {
        gameStage = new GameStage();
        mainMenuStage = new MainMenuStage();
        storeStage = new StoreStage();
    }

    public void stageUpdate(){
        if(StateStatus.flag == StateStatus.MAINMENU_STAGE){
            Gdx.input.setInputProcessor(mainMenuStage);
            mainMenuStage.act();
            mainMenuStage.draw();
        }else if (StateStatus.flag == StateStatus.GAME_STAGE) {
            Gdx.input.setInputProcessor(gameStage);
            gameStage.act();
            gameStage.draw();
        }else if (StateStatus.flag == StateStatus.STORE_STAGE) {
            Gdx.input.setInputProcessor(storeStage);
            storeStage.act();
            storeStage.draw();
        }
    }
    @Override
    public void dispose() {

    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stageUpdate();
    }

    @Override
    public void resize(int width, int height) {
    }

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值