LIBGDX Study notes: Encapsulating your own actor

Source: Internet
Author: User
Tags getcolor libgdx

Why do we have to encapsulate our own actors?

A: The actor itself may not be able to meet our development needs, or can support a variety of effects, which need to be expanded on its basis.

The following stickers I two times the package of Chactor code, for your reference:

The 1.CHActor uses an object cache pool, which automatically manages releases, which solves the problem of using a large number of objects in the game to cause a lower number of frames.

2. Set the drawn texture yourself without using the image. Creating too many images can cause the number of frames to drop very sharply.

3. Can inherit the chactor freely, the convenience oneself expands again.

Usage:

Chactor chactor = Chactor.obtain ();//using the cache pool

Chactor chactor = new Chactor ();//Pure NEW

Youractor extends Chactor {...}

Youractor youractor = Chactor.obtain (Youractor.class);

Chgame framework based on LIBGDX two-time package:

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> *  Two-time package 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 ()  {} @Overridepublic  void draw (batch batch, float  Parentalpha)  {color color = getcolor (); Batch.setcolor (Color.r, color.g, color.b, &NBSP;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> *  using the cache pool  *  * 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 =&Nbsp;pool.obtain (); Actor.setbgtexture (null);return  (T)  actor;} Public static chactor obtain ()  {return obtain (Chactor.class);} @Overridepublic  void reset ()  {        //  Initialize 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);} @Overridepublic  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;}}


LIBGDX Study notes: Encapsulating your own actor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.