博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Libgdx学习笔记:封装自己的Actor
阅读量:7033 次
发布时间:2019-06-28

本文共 4174 字,大约阅读时间需要 13 分钟。

hot3.png

为什么要去封装我们自己的Actor?

答:Actor本身可能无法满足我们的开发需求,或者无法支持各种各样的效果,由此需要在其基础上进行拓展。

下面贴出本人二次封装的CHActor代码,供大家参考:

  1.CHActor使用了对象缓存池,自动管理释放,很好的解决了游戏中使用大量对象导致帧数较低的问题。 

  2.自行设置绘制的纹理,而不必使用Image。创建过多的Image会导致帧数下降的很厉害。

  3.可自由继承CHActor,方便自己再次扩展。

  用法:

   CHActor chactor  = CHActor.obtain();// 使用缓存池里面的

   CHActor chactor = new CHActor();// 纯新建

   YourActor extends CHActor {...}

   YourActor youractor  = CHActor.obtain(YourActor.class);

   基于libGdx二次封装的CHGame框架 :

     

package com.oahcfly.chgame.core.mvc;import com.badlogic.gdx.graphics.Color;import com.badlogic.gdx.graphics.Texture;import com.badlogic.gdx.graphics.g2d.Batch;import com.badlogic.gdx.graphics.g2d.Sprite;import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;import com.badlogic.gdx.graphics.g2d.TextureRegion;import com.badlogic.gdx.scenes.scene2d.Actor;import com.badlogic.gdx.utils.Align;import com.badlogic.gdx.utils.Pool;import com.badlogic.gdx.utils.Pool.Poolable;import com.badlogic.gdx.utils.Pools;import com.oahcfly.chgame.core.Chao;/** *  * 
 * 二次封装的actor *  * date: 2014-12-11 * 
 *  * @author caohao */public class CHActor extends Actor implements Poolable { private int tag; private Texture bgTexture; private TextureRegion bgTextureRegion; public CHActor() { } @Override public void draw(Batch batch, float parentAlpha) { Color color = getColor(); batch.setColor(color.r, color.g, color.b, color.a); float x = getX(); float y = getY(); float scaleX = getScaleX(); float scaleY = getScaleY(); float width = getWidth(); float height = getHeight(); if (bgTexture != null) { batch.draw(bgTexture, x, y, getOriginX(), getOriginY(), getWidth(), getHeight(), scaleX, scaleY, getRotation(), 0, 0, (int) width, (int) height, false, false); } if (bgTextureRegion != null) { if (bgTextureRegion instanceof Sprite) { Sprite sprite = (Sprite) bgTextureRegion; sprite.setColor(batch.getColor()); sprite.setOrigin(getOriginX(), getOriginY()); sprite.setPosition(x, y); sprite.setScale(scaleX, scaleY); sprite.setSize(width, height); sprite.setRotation(getRotation()); sprite.draw(batch); } else { batch.draw(bgTextureRegion, x, y, getOriginX(), getOriginY(), width, height, scaleX, scaleY, getRotation()); } } } public void setBgTexture(Texture bgTexture) { this.bgTexture = bgTexture; if (bgTexture != null) { setSize(bgTexture.getWidth(), bgTexture.getHeight()); } setOrigin(Align.center); } /**  *   * 
	 * 使用缓存池	 * 	 * date: 2015-1-3	 * 
 *   * @author caohao  * @return  */ @SuppressWarnings("unchecked") public static 
 T obtain(Class
 type) { Pool
 pool = (Pool
) Pools.get(type); CHActor actor = pool.obtain(); actor.setBgTexture(null); return (T) actor; } public static CHActor obtain() { return obtain(CHActor.class); } @Override public void reset() {         // 初始化 this.bgTexture = null; this.bgTextureRegion = null; setScale(1); setRotation(0); clear(); setUserObject(null); this.setColor(new Color(1, 1, 1, 1)); setStage(null); setParent(null); setVisible(true); setName(null); setOrigin(Align.center); setPosition(0, 0); } public Texture getBgTexture() { return bgTexture; } public TextureRegion getBgTextureRegion() { return bgTextureRegion; } public void setBgTextureRegion(TextureRegion textureRegion) { this.bgTextureRegion = textureRegion; if (bgTextureRegion != null) { if (bgTextureRegion instanceof Sprite) { Sprite sprite = (Sprite) bgTextureRegion; setSize(sprite.getWidth(), sprite.getHeight()); } else if (bgTextureRegion instanceof AtlasRegion) { AtlasRegion atlasRegion = (AtlasRegion) bgTextureRegion; bgTextureRegion = Chao.plistCenter.createSprite(atlasRegion); Sprite sprite = (Sprite) bgTextureRegion; setSize(sprite.getWidth(), sprite.getHeight()); } else { setSize(bgTextureRegion.getRegionWidth(), bgTextureRegion.getRegionHeight()); } } setOrigin(Align.center); } @Override public boolean remove() { boolean remove = super.remove(); if (remove) { Pools.free(this); } return remove; } public int getTag() { return tag; } public void setTag(int tag) { this.tag = tag; }}

转载于:https://my.oschina.net/oahcfly/blog/465675

你可能感兴趣的文章
建造者模式
查看>>
【redux篇】middleware 之 redux-thunk
查看>>
数据结构---图的相关总结
查看>>
Linux平台上部署Mongoose服务器的方法介绍
查看>>
Node中间层实践(二)——搭建项目框架
查看>>
erget源码分析(2):全局哈希基类和全局异步函数对象接口
查看>>
解码方法
查看>>
Electron入门介绍
查看>>
从egg.js重新认识node后端开发
查看>>
聊聊springboot session timeout参数设置
查看>>
微信小程序调研
查看>>
window下git多账户管理
查看>>
【327天】我爱刷题系列086(2017.12.29)
查看>>
React.js 小书 Lesson15 - 实战分析:评论功能(二)
查看>>
如何使用JSON和GSON
查看>>
weex脚手架
查看>>
js正则表达式学习
查看>>
C++ 开发 PHP 7 扩展之定义常量
查看>>
windows 命令行禁用密码策略,创建用户
查看>>
游戏小学生01-egret环境搭建
查看>>