标签:
为什么要去封装我们自己的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框架 :
https://git.oschina.net/oahcfly/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;
/**
*
* <pre>
* 二次封装的actor
*
* date: 2014-12-11
* </pre>
*
* @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);
}
/**
*
* <pre>
* 使用缓存池
*
* date: 2015-1-3
* </pre>
*
* @author caohao
* @return
*/
@SuppressWarnings("unchecked")
public static <T extends CHActor> T obtain(Class<T> type) {
Pool<CHActor> pool = (Pool<CHActor>) 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;
}
}
标签:
原文地址:http://my.oschina.net/oahcfly/blog/465675