LibGDX_4.3: 舞台(Stage)

本文链接: http://blog.csdn.net/xietansheng/article/details/50187111

LibGDX 基础教程(总目录)

1. 概述

舞台(Stage)是众多演员(Actor)“表演”的地方,可以看做是一个包含不同层次演员的 2D 场景,能够处理视口(整体缩放显示 / 屏幕适配)和 接收输入事件(触屏,鼠标点击,按键按下等)自己处理以及将事件分发给演员。

2. 舞台类(Stage)

舞台类中的部分方法:

  • float getWidth(): 获取舞台的宽度
  • float getHeight(): 获取舞台的高度
  • void act(float delta): 更新舞台逻辑,并批处理舞台中的演员(自动逐个调用演员的 act() 方法更新演员逻辑)
  • void draw(): 绘制舞台,并批处理舞台中的演员(自动逐个调用演员的 draw() 方法绘制演员)
  • void dispose() : 释放舞台中的所有资源
  • boolean addListener(EventListener listener): 添加事件监听到舞台
  • boolean removeListener(EventListener listener): 移除监听器
  • void addActor(Actor actor): 增加一个演员到舞台中
  • void clear(): 移除舞台中的所有演员
  • Array<Actor> getActors(): 获取舞台中的所有演员
  • Group getRoot(): 获取舞台中维护所有演员的演员组

Group: 演员组,它也继承了 Actor,也是一个演员,舞台中维护了一个 Group 对象,添加到舞台中的演员其实最终添加到了 Group 中,舞台类中有增加演员的方法,但没有移除单个演员的方法,如果需要移除某个演员,需要调用 stage.getRoot() 方法先获取到舞台的 Group 对象,然后调用 Group 对象的 group.removeActor(actor) 方法。

3. 代码示例

这里引用前面章节中自定义的演员:

package com.libgdx.test;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

/**
 * 自定义演员
 */
public class MyActor extends Actor {

    private TextureRegion region;

    public MyActor(TextureRegion region) {
        super();
        this.region = region;
        setSize(this.region.getRegionWidth(), this.region.getRegionHeight());
    }

    public TextureRegion getRegion() {
        return region;
    }

    public void setRegion(TextureRegion region) {
        this.region = region;
        setSize(this.region.getRegionWidth(), this.region.getRegionHeight());
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        if (region == null || !isVisible()) {
            return;
        }
        batch.draw(
                region, 
                getX(), getY(), 
                getOriginX(), getOriginY(), 
                getWidth(), getHeight(), 
                getScaleX(), getScaleY(), 
                getRotation()
        );
    }

}

游戏主程序的启动入口类:

package com.libgdx.test;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;

/**
 * 游戏主程序的启动入口类
 */
public class MainGame extends ApplicationAdapter {

	// 纹理
    private Texture texture;

    // 自定义的演员
    private MyActor actor;
    
    // 舞台
    private Stage stage;

	@Override
	public void create() {
		// 创建纹理, badlogic.jpg 图片的宽高为 256 * 256
        texture = new Texture(Gdx.files.internal("badlogic.jpg"));
        // 创建演员, 位置默认为为 (0, 0), 在舞台左下角
        actor = new MyActor(new TextureRegion(texture));
        
        // 使用默认的构造方法创建舞台, 宽高默认为屏幕宽高
        stage = new Stage();
        
        // 将 演员 添加到舞台中, 由舞台去更新演员的逻辑和绘制
        stage.addActor(actor);
	}

	@Override
	public void render() {
		// 黑色清屏
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		/*
		 * 舞台自己维护了纹理画布(SpriteBatch), 并且在绘制前后自动分别调用了 begin() 和 end() 方法,
		 * 所以渲染舞台时, 只需要调用舞台的 act() 和 draw() 方法即可
		 */
		
		// 更新舞台逻辑,并批处理舞台中的演员(自动逐个调用演员的 act() 方法更新演员逻辑)
		stage.act();
		
		// 绘制舞台,并批处理舞台中的演员(自动逐个调用演员的 draw() 方法绘制演员)
		stage.draw();
	}

	@Override
	public void dispose() {
		// 释放纹理资源
		if (texture != null) {
			texture.dispose();
		}
		// 释放舞台资源
		if (stage != null) {
			stage.dispose();
		}
	}

}

运行结果:

sy_result.png


  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢TS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值