前几讲我们讲了Sprite类,这一讲我们来看一下它的父类GameObject。下面我们看一下GameObject的源码:
package com.stickycoding.rokon;
public class GameObject extends DrawableObject {
protected boolean isTouchable = false;
protected boolean isOnScene = false;
public GameObject(float x, float y, float width, float height) {
super(x, y, width, height);
}
/**
* @return TRUE if the DrawableObject has been added to the current Scene
*/
public boolean isAdded() {
return isOnScene;
}
/* (non-Javadoc)
* @see com.stickycoding.rokon.DrawableObject#onRemove()
*/
@Override
public void onRemove() {
super.onRemove();
isOnScene = false;
}
/**
* Sets the DrawableObject to a touchable, it will be checked when Scene handles input events
*/
public void setTouchable() {
isTouchable = true;
}
/**
* Sets the DrawableObject as un-touchable
*/
public void removeTouchable() {
isTouchable = false;
}
/**
* @return TRUE if the object is touchable, FALSE otherwise
*/
public boolean isTouchable() {
return isTouchable && !invisible;
}
/* (non-Javadoc)
* @see com.stickycoding.rokon.DrawableObject#onAdd(com.stickycoding.rokon.Layer)
*/
@Override
public void onAdd(Layer layer) {
super.onAdd(layer);
killNextUpdate = false;
isOnScene = true;
parentScene = layer.parentScene;
parentLayer = layer;
if(texture != null && texture.textureIndex == -1) {
if(layer.parentScene != null) {
if(Rokon.verbose) Debug.verbose("DrawableObject.onAdd", "Scene does not already contain the this objects Texture, adding automatically.");
}
}
}
}
下面我们就来看一下这几个方法。我们从Sprite构造方法角度来看GameObject的构造方法.我相信你肯定明白我想表达什么意思。你懂得对不。然后再看一下下面的四个方法:
isAdded(),是否已添加到场景中
isTouchable(),是否接收触摸事件
removeTouchable(),设置为不可触摸
setTouchable(),设置为可触摸
类名知意,相信即使很菜的我们,也明白了。
我们可以设置Sprite的这些属性,让游戏中的人物可以接收如上事件,至于如何处理事件前一讲我已经讲过了。
下面进入本讲重点:
就是这一句话,我们可以清醒的想到GameObject又继承于DrawableObject。在DrawableObject中有许多方法可以让我们的Sprite更加绚丽多彩。
第一步:让我们来看一下这个类中的方法
DrawableObject,继承BasicGameObject,GameObject的父类,不能接收触摸事件
animate(int[] animationTiles, long frameTime),自定义播放动画帧序列,以frameTime时间间隔一直循环播放
animate(int[] animationTiles, long frameTime, int loops, boolean returnToStart),自定义播放动画帧序列,以frameTime时间间隔播放loops次,returnToStart为true表示动画结束显示第一帧
animate(int startTile, int endTile, long frameTime),从startTile帧播放至endTile帧,以frameTime时间间隔一直循环播放
animate(int startTile, int endTile, long frameTime, int loops, boolean returnToStart),从startTile帧播放至endTile帧,以frameTime时间间隔播放loops次,returnToStart为true表示动画结束显示第一帧
fade(float startAlpha, float alpha, int time, int movementType),从初始startAlpha值经历一段时间time后变为alpha值,movementType取值通常为Movement.LINEAR,实现对象的渐现(失)
fade(float alpha, int time)
fade(float alpha, int time, int movementType)
从当前透明度值经历一段时间time后变为alpha值
forceDrawType(int drawType),设置对象绘制类型,3种类型,取值0,1,2,详见DrawPriority,一般不需要调用该函数
getParentLayer(),获取父层
getTextureTile(),获取当前显示的帧序号
getZ(),获取该对象的层序
setAlpha(float alpha),设置透明度
setBorder(boolean border),设置是否有边框
setBorder(float red, float green, float blue, float alpha),设置边框颜色,仅对没有图片呈现的对象起作用,会绘制一个边框
setZ(int z),设置该对象层序,影响对象绘制先后顺序。调用该函数后,须调用layer.setDrawOrder(DrawOrder.Z_ORDER)才会起作用
noBorder(),设置无填充
setTexture(Texture texture),设置需呈现的图片Texture实例
setTextureTile(int tileIndex),设置显示某帧图片,从0计数
setTextureTile(int column, int row),设置显示图片上某行某列的帧图片
setTextureTile(Texture texture, int tileIndex),设置需呈现的图片Texture实例和要显示的某帧图片
show(),显示对象
hide(),隐藏对象
isAlive(),对象是否存活,若已删除了该对象,则为false,否则为true
isOnScreen(),是否在屏幕可视区域内
isVisible(),是否可见
stopFade(),停止渐现(失)动画
remove(),删除该对象,等价于Scene.remove(drawableObject)
onRemove(),删除对象时自动调用,如有必要,须在子类中覆写
onUpdate(),动画播放和渐现(失)更新函数
可能大家也都看出来了,就是动画操作animation
第二步:今天我先不讲动画,先来看一个简单的Alpha的变化用到的是fade方法。如下:

相信大家有Android基础都能明白,它的四个参数的意义。我只解示一下Movement.LINEAR的意思:“实现对象渐变”。
注明:为什么我每天都只讲一点是因为我想让大家逐渐消化。为后面讲引擎核心代码打基础。
作业:增加一个新的Sprite实现由1.0f到0.0f的渐变。
源码下载:myandroid.ys168.com
如有不明白的Q我:1130437154
7127





