跟我来玩IGFrame(四) GAnimator精灵动画

各位转载请注明出处,谢谢合作~作者:ioozhuangzi

精灵动画,就是精灵动画……org.ioo.igframe.sprite.GAnimator类扩展自GSprite类,也就是说它拥有精灵的所有属性和方法。但它重写了渲染方式和更新方式以实现如下图这样的动画播放效果。

示例代码 org.ioo.igframe.samples.sample03.AnimatorTest2

这张图是一个爆炸的效果,6x5一共30帧,图片宽度为768x640,那么每一帧的宽高是128x128,一共6列(行数就无所谓了,1行的也是可以的,实际播放多少跟设定的总帧数有关)。

好了,需要的参数信息已经收集齐了。

我们来看示例代码的onLoad里的如下代码

g = new GAnimator("/res/images/explosion.png", 6, 128, 128, 30 , 2);

最后的2代表的是每2帧播放一帧动画。

剩下的就是调用renderupdate方法了~示例代码里使用的是SpriteManager托管,为什么可以用?说过了因为GAnimator继承了GSprite。手工调用也写了,只是注释掉了而已。

记得别忘了要在main方法里调用gFrame.autoRender(true);才能使用SpriteManager

GAnimator的构造方法也有很多,但基本都是默认了某些参数而已:

所有的参数名称描述如下:

// filePath图片的路径;columns动画列数;

//frameWidth每一帧宽度;frameHeight每一帧高度

// totalFrames动画总帧数;frameDelay延迟的帧数

// bufferedImage 图片;gAnimator已存在的GAnimator

public GAnimator()//什么都没做,自己手工配置,后边会将配置相关的方法

public GAnimator(String filePath, int columns, int frameWidth, int frameHeight, int totalFrames)

public GAnimator(String filePath, int columns, int frameWidth, int frameHeight, int totalFrames, int frameDelay)

public GAnimator(BufferedImage bufferedImage, int columns, int frameWidth, int frameHeight, int totalFrames)

public GAnimator(BufferedImage bufferedImage, int columns, int frameWidth, int frameHeight, int totalFrames, int frameDelay)

public GAnimator(GAnimator gAnimator, int columns, int frameWidth, int frameHeight, int totalFrames)

public GAnimator(GAnimator gAnimator)

public GAnimator(GAnimator gAnimator, int columns, int frameWidth, int frameHeight, int totalFrames, int frameDelay)

 

现在说说手工配置动画相关参数的相关方法:

//init方法的参数跟构造方法几乎一样,就不不用多说了吧

public void init(int columns, int frameWidth, int frameHeight, int totalFrames, int frameDelay)

//以下都是单独设置参数的方法,参数名都一样,应该能看明白吧

public void setStartFrame(int startFrame) / public int getStartFrame()

public void setTotalFrames(int totalFrames) / public int getTotalFrames()

public void setFrameWidth(int frameWidth) / public int getFrameWidth()

public void setFrameHeight(int frameHeight) / public int getFrameHeight()

public void setColumns(int columns) / public int getColumns()

public void setFrameDelay(int frameDelay) / public int getFrameDelay()

//这个需要单独说一下,用来设置帧幅。默认为1,就是一帧一帧的播放。

//如果设为2,就会是1357……以加2的方式递增要播放的帧

//实际播放的总帧数变成了原来的一半了,也就是15帧。其它的以此类推

public void setFrameDirection(int frameDirection) / public int getFrameDirection()

 

再来看上边的图片,虽然是30帧,但我们不一定完全播放所有的帧,也可以只播放其中的连续子集帧动画,比如只播放前6帧或者从第8帧播放到第15帧。这时可以用到下面的方法来设置子动画:

// startFrame起始帧,从0开始;totalFrames要播放的帧数

public void setChildAnimate(int startFrame, int totalFrames)

//下面这个方法有点特殊,它是用来设定某一行为一个子动画,从0开始。

//当然也可以写出起始帧和总帧数的方式

public void setChildAnimate(int rowIndex)

 

最后在说一点,默认动画是无限循环播放的,如果想指定播放次数可以使用一下方法:

public void setLoop(int loop)//设置动画播放的次数,0为无限循环播放

 

其它的可以参考GSprite相关的内容。再次说明以下,GAnimator扩展自GSprite类,你完全可以像GSprite一样使用它。

 

GAnimator就先说到这里,具体的使用方法您可以瞧瞧我提供的org.ioo.igframe.samples.sample03包下的示例代码,从中您会得到更多的GAnimator的相关用法。

本人文笔水平实在有限,看的不明白的地方还请您留言提问或者加Q210816248~验证:IGFrame

 

补充:

org.ioo.igframe.image.ImageLoader类,

该类使用来加载图片的工具类。提供了一些加载图片的静态方法。

public static BufferedImage loadBufferedImage(String filePath)

public static BufferedImage loadBufferedImage(URL url)

public static BufferedImage loadBufferedImage(InputStream inputStream)

public static BufferedImage loadBufferedImage(File file)

 

猜你喜欢

转载自blog.csdn.net/ioozhuangzi/article/details/17916925