Andengine has a built-in tiledsprite class that can pass in the texture of tiledtextureregion to construct an animated sequence image that can be played continuously. However, sometimes in game project development, there is a huge workload for the artist, and this tiled also brings about the workload when it needs to be modified. In cocos2d of iPhone, the genie class has a runaction method that can play a group of sequential images to achieve the animation function. However, this method is a little troublesome to use, so I encapsulated a custom tiled genie class.
Package COM. weedong. sprite; import static Org. anddev. andengine. util. constants. constants. vertex_index_x; import static Org. anddev. andengine. util. constants. constants. vertex_index_y; import Java. util. arrays; import javax. microedition. khronos. opengles. gl10; import Org. anddev. andengine. collision. rectangularshapecollisionchecker; import Org. anddev. andengine. collision. shapecollisionchecker; import Org. anddev. and Engine. entity. primitive. baserectangle; import Org. anddev. andengine. entity. scene. scene; import Org. anddev. andengine. entity. scene. scene. ionareatouchlistener; import Org. anddev. andengine. entity. shape. ishape; import Org. anddev. andengine. entity. shape. rectangularshape; import Org. anddev. andengine. input. touch. touchevent; import Org. anddev. andengine. openGL. texture. texture; import Org. anddev. andengine. openGL. text Ure. region. textureregion; import Org. anddev. andengine. openGL. texture. region. textureregionfactory; import Org. anddev. andengine. openGL. texture. region. buffer. textureregionbuffer; import Org. anddev. andengine. openGL. texture. source. itexturesource; import Org. anddev. andengine. openGL. util. glhelper; import Org. anddev. andengine. util. mathutils; import Org. anddev. andengine. util. constants. timeconstants; import COM. we Edong. scene. itextureloadmanager; import COM. weedong. utils. textureutils;/*** custom genie, which can play animations like cocos2d using non-tiled images. <br/> * usage: you can input the relevant image in the constructor * @ author **/public class customtiledsprite extends baserectangle {Private Static final int loop_continuous =-1; private Boolean manimationrunning; private long manimationprogress; private long manimationduration; private long [] mframeendsinnanoseconds; PRI Vate int limit; private int minitialloopcount; private int mloopcount; private ianimationlistener manimationlistener; private int mframecount; private int [] mframes; private textureregion [] Limit = NULL; private textureregion Limit = NULL; private ionareatouchlistener onareatouchlistener; private scene mscene;/*** constructor * @ Param PX * @ Param py * @ Param SCENE * @ Param ar Ytexturesource is passed in to texturesource to construct */Public customtiledsprite (float PX, float py, scene, itexturesource [] arytexturesource) {This (PX, Py, scene, arytexturesource, false );} /*** constructor ** @ Param PX * @ Param py * @ Param SCENE * @ Param arytexturesource pass in texturesource to construct * @ Param bflippedhorizontal whether the image is flipped horizontally */Public customtiledsprite (float px, float py, scene, itexturesource [] arytexturesourc E, Boolean bflippedhorizontal) {super (PX, Py, arytexturesource [0]. getwidth (), arytexturesource [0]. getheight (); this. mscene = scene; loadanimationresource (scene, arytexturesource, bflippedhorizontal );} /*** constructor ** @ Param PX * @ Param py * @ Param SCENE * @ Param arytexture input textureregion to construct */Public customtiledsprite (float PX, float py, scene, textureregion [] arytexture) {super (PX, Py, arytexture [0]. getwidth (), arytexture [0]. getheight (); this. mscene = scene; this. arytextureregion = arytexture; mcurrenttextureregion = arytextureregion [0]; this. initblendfunction ();}/*** Note: If you use this constructor, after instantiating the genie, be sure to use the * loadanimationresource method to load Resources * @ Param PX * @ Param py */Public customtiledsprite (float PX, float Py) {super (PX, Py, 0, 0 );} /*** load the animation resource ** @ author * @ Param scene current scenario * @ Param arytexturesource * @ Pa Ram bflippedhorizontal whether to flip the image horizontally */Public void loadanimationresource (scene, itexturesource [] arytexturesource, Boolean bflippedhorizontal) {int texturewidth = arytexturesource [0]. getwidth (); int textureheight = arytexturesource [0]. getheight (); this. setwidth (texturewidth); this. setheight (textureheight); texturewidth = textureutils. gettextureclosewidth (texturewidth); textureheight = textureutils. get Texturecloseheight (textureheight); arytextureregion = new textureregion [arytexturesource. length]; for (INT I = 0; I <arytexturesource. length; ++ I) {texture = new texture (texturewidth, textureheight, textureutils. autorecognisetextureoptions (); textureregion = textureregionfactory. createfromsource (texture, arytexturesource [I], 0, 0); textureregion. setflippedhorizontal (bflippedhori Zontal); arytextureregion [I] = textureregion; itextureloadmanager textureloadmanager = (itextureloadmanager) scene; textureloadmanager. loadtextureandappendtocontainer (texture);} mcurrenttextureregion = arytextureregion [0]; this. initblendfunction ();} public Boolean isanimationrunning () {return this. manimationrunning;} @ overrideprotected void onmanagedupdate (final float implements condselapsed) {super. onmanagedupda Te (Response condselapsed); If (this. manimationrunning) {final long nanosecondselapsed = (long) (descricondselapsed * timeconstants. nanosecondspersecond); this. manimationprogress + = nanosecondselapsed; If (this. manimationprogress> This. manimationduration) {This. manimationprogress % = This. manimationduration; If (this. minitialloopcount! = Loop_continuous) {This. mloopcount --;} If (this. minitialloopcount = loop_continuous | this. mloopcount> = 0) {final int currentframeindex = This. calculatecurrentframeindex (); If (this. mframes = NULL) {This. setcurrenttileindex (this. mfirsttileindex + currentframeindex);} else {This. setcurrenttileindex (this. mframes [currentframeindex]);} else {This. manimationrunning = false; If (this. manimationlistener! = NULL) {This. manimationlistener. onanimationend (this) ;}}} public void setcurrenttileindex (INT index) {This. mcurrenttextureregion = arytextureregion [Index]; this. updatevertexbuffer (); mcurrenttextureregion. gettexturebuffer (). update ();} public void stopanimation () {This. manimationrunning = false;} public void stopanimation (final int ptileindex) {This. manimationrunning = false; this. setcurrenttileindex (PTI Leindex);} private int calculatecurrentframeindex () {final long animationprogress = This. manimationprogress; Final long [] frameends = This. mframeendsinnanoseconds; Final int framecount = This. mframecount; For (INT I = 0; I <framecount; I ++) {If (frameends [I]> animationprogress) {return I ;}} return framecount-1 ;} public customtiledsprite animate (final long pframedurationeach) {return this. animate (pfram Edurationeach, true);} public customtiledsprite animate (final long pframedurationeach, final Boolean ploop) {return this. animate (pframedurationeach, (ploop )? Loop_continuous: 0, null);} public customtiledsprite animate (final long pframedurationeach, final int ploopcount) {return this. animate (pframedurationeach, ploopcount, null);} public customtiledsprite animate (final long pframedurationeach, final Boolean ploop, final ianimationlistener panimationlistener) {return this. animate (pframedurationeach, (ploop )? Listener: 0, panimationlistener);} public customtiledsprite animate (final long listener, final int ploopcount, final ianimationlistener panimationlistener) {final long [] framedurations = new long [listener. length]; arrays. fill (framedurations, pframedurationeach); return this. animate (framedurations, ploopcount, panimationlistener);} public customtiledsprite animate (final long [] Pframedurations) {return this. animate (pframedurations, true);} public customtiledsprite animate (final long [] conditions, final Boolean ploop) {return this. animate (pframedurations, (ploop )? Loop_continuous: 0, null);} public customtiledsprite animate (final long [] pframedurations, final int ploopcount) {return this. animate (pframedurations, ploopcount, null);} public customtiledsprite animate (final long [] pframedurations, final Boolean ploop, final ianimationlistener panimationlistener) {return this. animate (pframedurations, (ploop )? Loop_continuous: 0, panimationlistener);} public customtiledsprite animate (final long [] pframedurations, final int ploopcount, final ianimationlistener panimationlistener) {return this. animate (pframedurations, 0, arytextureregion. length-1, ploopcount, panimationlistener);} public customtiledsprite animate (final long [] pframedurations, final int pfirsttileindex, final int plasttileindex, final boo Lean ploop) {return this. animate (pframedurations, pfirsttileindex, plasttileindex, (ploop )? Loop_continuous: 0, null);} public customtiledsprite animate (final long [] pframedurations, final int pfirsttileindex, final int plasttileindex, final int ploopcount) {return this. animate (pframedurations, pfirsttileindex, plasttileindex, ploopcount, null);} public customtiledsprite animate (final long [] distinct, final int [] pframes, final int ploopcount) {return this. animate (pframedurations, P Frames, ploopcount, null);}/*** animate specifics frames ** @ Param pframedurations must have the same length as pframes. * @ Param pframes indices of the frames to animate. * @ Param ploopcount * @ Param panimationlistener */Public writable animate (final long [] partial, final int [] pframes, final int ploopcount, final ianimationlistener panimationlistener) {final int framecount = Frames. length; If (pframedurations. length! = Framecount) {Throw new illegalargumentexception ("pframedurations must have the same length as pframes. ");} return this. init (pframedurations, framecount, pframes, 0, ploopcount, panimationlistener);}/**** @ Param pframedurations * must have the same length as pfirsttileindex to * plasttileindex. * @ Param pfirsttileindex * @ Param plasttileindex * @ Param ploopcount * @ Param panimationlistener */Public Customtiledsprite animate (final long [] distinct, final int pfirsttileindex, final int plasttileindex, final int ploopcount, final ianimationlistener panimationlistener) {if) {Throw new illegalargumentexception ("an animation needs at least two tiles to animate. ");} final int framecount = (plasttileindex-pfirsttileindex) + 1; if (pframedurations. le Ngth! = Framecount) {Throw new illegalargumentexception ("pframedurations must have the same length as pfirsttileindex to plasttileindex. ");} return this. init (delimiter, framecount, null, pfirsttileindex, ploopcount, panimationlistener);} private customtiledsprite Init (final long [] pframedurations, final int framecount, final int [] pframes, final int analyze, final int ploopcount, final ianima Tionlistener panimationlistener) {This. mframecount = framecount; this. manimationlistener = panimationlistener; this. minitialloopcount = ploopcount; this. mloopcount = ploopcount; this. mframes = pframes; this. mfirsttileindex = pfirsttileindex; If (this. mframeendsinnanoseconds = NULL | this. mframecount> This. mframeendsinnanoseconds. length) {This. mframeendsinnanoseconds = new long [this. mframecount];} final Long [] frameendsinnanoseconds = This. mframeendsinnanoseconds; mathutils. arraysuminto (pframedurations, frameendsinnanoseconds, timeconstants. nanosecondspermillisecond); Final long lastframeend = frameendsinnanoseconds [this. mframecount-1]; this. manimationduration = lastframeend; this. manimationprogress = 0; this. manimationrunning = true; return this;} public static interface ianimationlistener {public v Oid onanimationend (final customtiledsprite panimatedsprite);} @ overridepublic void reset () {super. reset (); this. initblendfunction () ;}@ overrideprotected void oninitdraw (final gl10 PGL) {super. oninitdraw (PGL); glhelper. enabletextures (PGL); glhelper. enabletexcoordarray (PGL);} @ overrideprotected void onapplytransformations (final gl10 PGL) {super. onapplytransformations (PGL); this. mcurrenttextureregion. onappl Y (PGL);} private void initblendfunction () {If (this. mcurrenttextureregion. gettexture (). gettextureoptions (). mpremultipyalpha) {This. setblendfunction (callback, callback) ;}@ overridepublic Boolean onareatouched (touchevent pscenetouchevent, float ptoucharealocalx, float ptoucharealocaly) {If (this. onareatouchlistener! = NULL) return this. onareatouchlistener. onareatouched (pscenetouchevent, this, ptoucharealocalx, ptoucharealocaly); return Super. onareatouched (pscenetouchevent, ptoucharealocalx, ptoucharealocaly );} /*** set the click listener * @ author * @ Param listener * @ Param bregistertoucharea whether to register the click area */Public void listener (ionareatouchlistener listener, Boolean bregistertoucharea) {If (bregistertoucharea) mscene. registertoucharea (this); this. onareatouchlistener = listener;}/*** click listener * @ author * @ Param listener */Public void addareatouchedlistener (ionareatouchlistener listener) {This. onareatouchlistener = listener;}/*** cancel registration click area */Public void unregistertoucharea () {This. mscene. unregistertoucharea (this);}/**** horizontal flip * @ author * @ Param nfirsttiled * @ Param nlasttiled */Public void setflippedhorizontal (INT nfirsttiled, int nlasttiled) {for (INT I = nfirsttiled; I <nlasttiled; I ++) {arytextureregion [I]. setflippedhorizontal (true) ;}}/*** cancel horizontal flip * @ author */Public void resetflippedhorizontal () {for (textureregion: arytextureregion) {textureregion. setflippedhorizontal (false) ;}/ *** vertical flip * @ author * @ Param nfirsttiled * @ Param nlasttiled */Public void setflippedvertical (INT nfirsttiled, int nlasttiled) {for (INT I = nfirsttiled; I <nlasttiled; I ++) {arytextureregion [I]. setflippedvertical (true) ;}}/*** cancel vertical flip * @ author */Public void resetflippedvertical () {for (textureregion: arytextureregion) {textureregion. setflippedvertical (false);}/*** clone texture * @ author * @ return */Public textureregion [] clonetextureregion () {textureregion [] ret = new textureregion [arytextureregion. length]; for (INT I = 0; I <ret. length; ++ I) {RET [I] = arytextureregion [I]. clone ();} return ret;} @ overrideprotected void finalize () throws throwable {super. finalize (); For (textureregion region: arytextureregion) {final textureregionbuffer = region. gettexturebuffer (); If (textureregionbuffer. ismanaged () {textureregionbuffer. returns () ;}}@ overridepublic Boolean collideswith (final ishape pothershape) {If (pothershape instanceof rectangularshape) {final rectangularshape placement = (rectangularshape) pothershape; return response. checkcollision (this, potherrectangularshape);} else {return false ;}} public static class customrectangularshapecollisionchecker extends shapecollisionchecker {// ======================== ========================================/// constants // ========== ========================================================== ============= Private Static final int rectangularshape_vertex_count = 4; private Static final float [] vertices_contains_tmp = new float [2 * rectangularshape_vertex_count]; Private Static final float [] rows = new float [2 * rectangularshape_vertex_count]; private Static final float [] rows = new float [2 * rectangularshape_vertex_count]; public static Boolean checkcontains (final rectangularshape prectangularshape, final float PX, final float Py) {rows. fillvertices (prectangularshape, vertices_contains_tmp); Return shapecollisionchecker. checkcontains (values, 2 * values, PX, Py);} public static Boolean checkcollision (final rectangularshape values, final rectangularshape values) {fillvertices (prectangularshapea, values); fillvertices (values, values, vertices_collision_tmp_ B); Return shapecollisionchecker. checkcollision (2 * Rows, counts, 2 * Rows, counts);} public static void fillvertices (final rectangularshape prectangularshape, final float [] pvertices) {final float left = 0; final float top = 0; Final float right = prectangularshape. getwidth () * prectangularshape. getscalex (); Final float Bottom = prectangularshape. getheight () * prectangularshape. getscaley (); pvertices [0 + vertex_index_x] = left; pvertices [0 + Keys] = top; pvertices [2 + vertex_index_x] = right; pvertices [2 + vertex_index_y] = top; pvertices [4 + vertex_index_x] = right; pvertices [4 + vertex_index_y] = bottom; pvertices [6 + vertex_index_x] = left; pvertices [6 + segments] = bottom; prectangularshape. getlocaltoscenetransformation (). transform (pvertices); // log. I ("customtiledsprite", "collision width:" + right + ", height:" + bottom + ", final collision width:" + pvertices [2 + vertex_index_x] + ", height: "+ pvertices [6 + vertex_index_y]) ;}}