例子源码:使用到的(Person类请见 7 演员类的使用 Actor 类)
package com.qsuron; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; 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 MyDemo implements ApplicationListener{ public static boolean GameIsRunning = false; public static boolean ShopIsRunning = false; public static boolean SuccessIsRunning = false; SpriteBatch batch; Image image_stage_background, image_openStage_background, image_openStage_btn_newGame, image_openStage_btn_shop, image_shopStage_background, image_shopStage_btn_heart, image_shopStage_btn_coin, image_shopStage_btn_close, image_successStage_success; Stage stage, openStage, shopStage, successStage; @Override public void create() { batch = new SpriteBatch(); stage = new Stage(800, 480, false, batch); openStage = new Stage(800, 480, false, batch); shopStage = new Stage(800, 480, false, batch); successStage = new Stage(800, 480, false, batch); //开始游戏舞台 Texture texture_open = new Texture(Gdx.files.internal("data/open.png")); Texture texture_button = new Texture(Gdx.files.internal("data/btn_newgame.png")); Texture texture_button2 = new Texture(Gdx.files.internal("data/btn_shop.png")); image_openStage_background = new Image(new TextureRegion(texture_open,800,480)); image_openStage_background.setPosition(0,0); image_openStage_btn_newGame = new Image(texture_button); image_openStage_btn_newGame.setPosition(80,380); image_openStage_btn_shop = new Image(texture_button2); image_openStage_btn_shop.setPosition(80,80); openStage.addActor(image_openStage_background); openStage.addActor(image_openStage_btn_newGame); openStage.addActor(image_openStage_btn_shop); //商店舞台 Texture texture_shop = new Texture(Gdx.files.internal("data/shop.png")); image_shopStage_background = new Image(new TextureRegion(texture_shop,0,85,512,350)); image_shopStage_btn_heart = new Image(new TextureRegion(texture_shop,0,0,102,85)); image_shopStage_btn_coin = new Image(new TextureRegion(texture_shop,102,0,102,85)); image_shopStage_btn_close = new Image(new TextureRegion(texture_shop,300,10,50,40)); image_shopStage_background.setPosition(0, 0); image_shopStage_background.setSize(480,320); image_shopStage_btn_heart.setPosition(190,50); image_shopStage_btn_coin.setPosition(50,50); image_shopStage_btn_close.setPosition(400,275); shopStage.addActor(image_shopStage_background); shopStage.addActor(image_shopStage_btn_heart); shopStage.addActor(image_shopStage_btn_coin); shopStage.addActor(image_shopStage_btn_close); //购买舞台 image_successStage_success = new Image(new TextureRegion(texture_shop,512,0,255,255)); image_successStage_success.setPosition(100,100); successStage.addActor(image_successStage_success); //游戏舞台 image_stage_background = new Image(new Texture(Gdx.files.internal("data/background.jpg"))); Gdx.input.setInputProcessor(stage); Person p = new Person(100,170); stage.addActor(image_stage_background); stage.addActor(p); stage.addActor(p.btn_L); stage.addActor(p.btn_R); //设置监听 this.setListener(); } public void setListener() { image_openStage_btn_newGame.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { GameIsRunning = true; return true; } }); image_openStage_btn_shop.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { ShopIsRunning = true; return true; } }); image_shopStage_btn_heart.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { SuccessIsRunning = true; return true; } }); image_shopStage_btn_coin.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { SuccessIsRunning = true; return true; } }); image_shopStage_btn_close.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { ShopIsRunning = false; return true; } }); image_successStage_success.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { SuccessIsRunning = false; return true; } }); } //重置舞台监听 public void setStage(){ if(GameIsRunning){ Gdx.input.setInputProcessor(stage); }else{ Gdx.input.setInputProcessor(openStage); if(ShopIsRunning){ Gdx.input.setInputProcessor(shopStage); if(SuccessIsRunning){ Gdx.input.setInputProcessor(successStage); } } } } //重置舞台绘制 public void stageRender(){ if(GameIsRunning){ stage.act(); stage.draw(); }else{ openStage.act(); openStage.draw(); if(ShopIsRunning){ shopStage.act(); shopStage.draw(); if(SuccessIsRunning){ successStage.act(); successStage.draw(); } } } } @Override public void dispose() { batch.dispose(); stage.dispose(); openStage.dispose(); shopStage.dispose(); successStage.dispose(); } @Override public void render() { Gdx.gl.glClearColor(0,0,0, 0); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); this.stageRender(); this.setStage(); } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } }
这个例子,只是比较简单的舞台切换,当然了,正常游戏中是不这样设置舞台切换的,
这里只是拿最浅显易懂的东西来给大家说明,Stage类的使用和简单的切换,大家千万别以为游戏中就要这么使用。
希望大家通过简单的代码能理解Stage类的使用,能明白他的原理就可以了。
Pages: 1 2
Comments NOTHING