LibGDX_4.5: 演员组(Group)

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

LibGDX 基础教程(总目录)

1. 概述

演员组类(Group)表示的是一组演员,可以包含多个不同层次演员,将多个演员添加到组中作为演员的父节点对多个演员统一管理。Group 类继承自 Actor,本质上也是一样演员,Group 是对于 Actor 的增强封装。实际上 Group 中维护了一个数组用于存放添加到组中的演员,在绘制时根据数组中的顺序依次绘制演员。

Actor 添加到 Group 中后,Actor 的父节点则变为 Group,因此 Actor 的坐标将相对于 Group 的左下角,关系如下图所示:

sy_stage_group_actor.png

2. Group 常用方法

Group 继承自 Actor,因此演员拥有的特性和方法 Group 均拥有。作为管理众多演员的组,还增加了许多操作组中演员的方法:

  • void addActor(Actor actor): 将演员作为子节点添加到组中。
  • void addActorAfter(Actor actorAfter, Actor actor): 在已在组中的演员 actorAfter 的索引位置后面添加演员 actor。绘制时 actorAfter 先绘制,actor 后绘制。
  • void addActorBefore(Actor actorBefore, Actor actor): 在已在组中的演员 actorBefore 的索引位置前面添加演员 actor。绘制时 actor 先绘制,actorBefore 后绘制。
  • void addActorAt(int index, Actor actor): 在指定索引位置添加一个演员(索引越大先后绘制,即越显示在屏幕前面)。
  • boolean removeActor(Actor actor): 移除组中的一个演员。
  • boolean swapActor(Actor first, Actor second): 交换组中两个演员的位置。
  • boolean swapActor(int first, int second): 根据索引交换组中两个演员的位置。
  • void clearChildren(): 移除组中所有演员。
  • void clear(): 移除组中所有演员,组的所有动作和监听。
  • SnapshotArray<Actor> getChildren(): 获取组中所有的演员。

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.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;

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

	private Texture texture;
	
	private Stage stage;
	
	private Group group;
	
	private MyActor actor1;
	
	private MyActor actor2;
	
	@Override
	public void create() {
		// 创建纹理, badlogic.jpg 图片的宽高为 256 * 256
		texture = new Texture(Gdx.files.internal("badlogic.jpg"));
		
		// 创建舞台, 舞台宽高默认为屏幕宽高
		stage = new Stage();
		
		// 创建演员组
		group = new Group();
		// 设置 group 在舞台中的位置
		group.setPosition(50, 100);
		
		// 添加 group 到舞台(group 本身也是一个演员)
		stage.addActor(group);
		
		// 创建第一个演员, 并设置属性
		actor1 = new MyActor(new TextureRegion(texture));
		actor1.setScale(0.5F);
		actor1.setPosition(0, 0);	// 位置设置到 group 的左下角
		
		// 创建第二个演员, 并设置属性
		actor2 = new MyActor(new TextureRegion(texture));
		actor2.setScale(0.25F);
		actor2.setPosition(100, 200);
		actor2.setRotation(45);		// 逆时针旋转 45 度
		
		// 添加演员到组中
		group.addActor(actor1);
		group.addActor(actor2);

		// 还可以设置演员的绘制顺序(ZIndex 属性)
	
		/*
		 * ZIndex 属性说明:
		 * 
		 * 当组中有许多演员时, 如果某些演员有重叠部分, 往往需要规定演员的绘制顺序, 例如背景先绘制,
		 * 人物后绘制, 在 Actor 中使用 ZIndex 属性来表示绘制顺序, ZIndex 值越大越后绘制, 即显示越靠前。
		 * 
		 * ZIndex 属性值其实对应的是 Group 中的演员数组对象中演员所在的索引位置(添加到 Group 中的演员的默认添加到数组的最后位置)。
		 * 绘制时 Group 将遍历数组依次绘制演员, 则越靠后(ZIndex/index 越大)的元素越后绘制(即显示越靠前)。
		 * 
		 * 因此, 必须先将 Actor 添加到 Group(或 Stage, 因为 Stage 也是通过 Group 管理其中的演员)中之后才能操作 ZIndex 属性, 
		 * 否则 ZIndex 属性没有意义。
		 * 
		 * // 获取演员的索引, 如果 actor 没有添加到 Group/Stage 中, 将返回 -1
		 * int index = actor.getZIndex();
		 * 
		 * // 设置演员的索引(实际上是将数组中的演员元素移动到指定的索引位置, 可能会引起其他演员的 ZIndex 值跟着改变)
		 * // 如果 actor 没有添加到 Group/Stage 中, 设置 ZIndex 属性无效
		 * actor.setZIndex(int index);
		 */
	}

	@Override
	public void render() {
		// 黑色清屏
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		// 更新舞台逻辑
		stage.act();
		// 绘制舞台
		stage.draw();
	}
	
	@Override
	public void dispose() {
		// 释放资源
		if (texture != null) {
			texture.dispose();
		}
		if (stage != null) {
			stage.dispose();
		}
	}

}

运行结果:

sy_result.png


  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谢TS

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

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

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

打赏作者

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

抵扣说明:

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

余额充值