TiledLayer 类型
继承于 Component
模块: cc
渲染 TMX layer。
索引
属性(properties)
方法
Details
属性(properties)
__eventTargets
注册所有相关的 EventTargets,所有事件回调将在 _onPreDestroy 中删除。
node
该组件被附加到的节点。组件总会附加到一个节点。
示例
cc.log(comp.node);
uuid
组件的 uuid,用于编辑器。
示例
cc.log(comp.uuid);
_enabled
enabled
表示该组件自身是否启用。
示例
comp.enabled = true;
cc.log(comp.enabled);
enabledInHierarchy
表示该组件是否被启用并且所在的节点也处于激活状态。
示例
cc.log(comp.enabledInHierarchy);
_isOnLoadCalled
返回一个值用来判断 onLoad 是否被调用过,不等于 0 时调用过,等于 0 时未调用。
示例
cc.log(this._isOnLoadCalled > 0);
_name
_objFlags
name
该对象的名称。
示例
obj.name = "New Obj";
isValid
表示该对象是否可用(被 destroy 后将不可用)。
当一个对象的 destroy 调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始 isValid 就会返回 false,而当前帧内 isValid 仍然会是 true。如果希望判断当前帧是否调用过 destroy,请使用 cc.isValid(obj, true),不过这往往是特殊的业务需求引起的,通常情况下不需要这样。
示例
var node = new cc.Node();
cc.log(node.isValid);
node.destroy();
cc.log(node.isValid);
cc.log(node.isValid);
方法
enableCulling
开启或关闭裁剪。
参数列表
addUserNode
添加用户节点。
参数列表
removeUserNode
移除用户节点。
参数列表
destroyUserNode
销毁用户节点。
参数列表
getLayerName
获取层的名称。
示例
let layerName = tiledLayer.getLayerName();
cc.log(layerName);
setLayerName
设置层的名称
参数列表
示例
tiledLayer.setLayerName("New Layer");
getProperty
获取指定属性名的值。
参数列表
示例
let property = tiledLayer.getProperty("info");
cc.log(property);
getPositionAt
获取指定 tile 的像素坐标。
参数列表
示例
let pos = tiledLayer.getPositionAt(cc.v2(0, 0));
cc.log("Pos: " + pos);
let pos = tiledLayer.getPositionAt(0, 0);
cc.log("Pos: " + pos);
setTilesGIDAt
设置给定区域的 tile 的 gid (gid = tile 全局 id),
参数列表
gids Array an array contains gid
beginCol Number begin col number
beginRow Number begin row number
totalCols Number count of column
示例
tiledLayer.setTilesGIDAt([1, 1, 1, 1], 10, 10, 2)
setTileGIDAt
设置给定坐标的 tile 的 gid (gid = tile 全局 id),
tile 的 GID 可以使用方法 “tileGIDAt” 来获得。
如果一个 tile 已经放在那个位置,那么它将被删除。
参数列表
示例
tiledLayer.setTileGIDAt(1001, 10, 10, 1)
getTiles
返回 tiles 数据. 由GID构成的一个数组.
getTileGIDAt
通过给定的 tile 坐标、flags(可选)返回 tile 的 GID.
如果它返回 0,则表示该 tile 为空。
参数列表
示例
let tileGid = tiledLayer.getTileGIDAt(0, 0);
getLayerOrientation
获取 Layer 方向(同地图方向)。
示例
let orientation = tiledLayer.getLayerOrientation();
cc.log("Layer Orientation: " + orientation);
getProperties
获取 layer 的属性,可以使用 Tiled 编辑器添加属性。
示例
let properties = tiledLayer.getProperties();
cc.log("Properties: " + properties);
getTiledTileAt
通过指定的 tile 坐标获取对应的 TiledTile。
如果指定的坐标没有 tile,并且设置了 forceCreate 那么将会在指定的坐标创建一个新的 TiledTile 。
在渲染这个 tile 的时候,将会使用 TiledTile 的节点的旋转、缩放、位移、颜色属性。
参数列表
x Integer
y Integer
forceCreate Boolean
示例
let tile = tiledLayer.getTiledTileAt(100, 100, true);
cc.log(tile);
setTiledTileAt
将指定的 tile 坐标替换为指定的 TiledTile。
参数列表
x Integer
y Integer
tiledTile cc.TiledTile
getTexture
获取纹理。
参数列表
index Unknown The index of textures
getTextures
获取纹理。
setTexture
设置纹理。
参数列表
setTexture
设置纹理。
参数列表
getLayerSize
获得层大小。
示例
let size = tiledLayer.getLayerSize();
cc.log("layer size: " + size);
getMapTileSize
获取 tile 的大小( tile 的大小可能会有所不同)。
示例
let mapTileSize = tiledLayer.getMapTileSize();
cc.log("MapTile size: " + mapTileSize);
getTileSet
获取 layer 索引位置为0的 Tileset 信息。
参数列表
index Unknown The index of tilesets
getTileSet
获取 layer 所有的 Tileset 信息。
setTileSet
设置 layer 的 tileset 信息。
参数列表
setTileSets
设置 layer 的 Tileset 信息。
参数列表
update
如果该组件启用,则每帧调用 update。
该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
参数列表
dt Number the delta time in seconds it took to complete the last frame
lateUpdate
如果该组件启用,则每帧调用 LateUpdate。
该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
参数列表
dt Number the delta time in seconds it took to complete the last frame
__preload
__preload is called before every onLoad.
It is used to initialize the builtin components internally,
to avoid checking whether onLoad is called before every public method calls.
This method should be removed if script priority is supported.
onLoad
当附加到一个激活的节点上或者其节点第一次激活时候调用。onLoad 总是会在任何 start 方法调用前执行,这能用于安排脚本的初始化顺序。
该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
start
如果该组件第一次启用,则在所有组件的 update 之前调用。通常用于需要在所有组件的 onLoad 初始化完毕后执行的逻辑。
该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
onEnable
当该组件被启用,并且它的节点也激活时。
该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
onDisable
当该组件被禁用或节点变为无效时调用。
该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
onDestroy
当该组件被销毁时调用
该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
onFocusInEditor
onLostFocusInEditor
resetInEditor
用来初始化组件或节点的一些属性,当该组件被第一次添加到节点上或用户点击了它的 Reset 菜单时调用。这个回调只会在编辑器下调用。
addComponent
向节点添加一个组件类,你还可以通过传入脚本的名称来添加组件。
参数列表
typeOrClassName Function | String the constructor or the class name of the component to add
示例
var sprite = node.addComponent(cc.Sprite);
var test = node.addComponent("Test");
getComponent
获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
传入参数也可以是脚本的名称。
参数列表
示例
var sprite = node.getComponent(cc.Sprite);
var test = node.getComponent("Test");
getComponents
返回节点上指定类型的所有组件。
参数列表
示例
var sprites = node.getComponents(cc.Sprite);
var tests = node.getComponents("Test");
getComponentInChildren
递归查找所有子节点中第一个匹配指定类型的组件。
参数列表
示例
var sprite = node.getComponentInChildren(cc.Sprite);
var Test = node.getComponentInChildren("Test");
getComponentsInChildren
递归查找自身或所有子节点中指定类型的组件
参数列表
示例
var sprites = node.getComponentsInChildren(cc.Sprite);
var tests = node.getComponentsInChildren("Test");
_getLocalBounds
如果组件的包围盒与节点不同,您可以实现该方法以提供自定义的轴向对齐的包围盒(AABB),
以便编辑器的场景视图可以正确地执行点选测试。
参数列表
out_rect Rect the Rect to receive the bounding box
onRestore
onRestore 是用户在检查器菜单点击 Reset 时,对此组件执行撤消操作后调用的。
如果组件包含了“内部状态”(不在 CCClass 属性中定义的临时成员变量),那么你可能需要实现该方法。
编辑器执行撤销/重做操作时,将调用组件的 get set 来录制和还原组件的状态。然而,在极端的情况下,它可能无法良好运作。
那么你就应该实现这个方法,手动根据组件的属性同步“内部状态”。一旦你实现这个方法,当用户撤销或重做时,组件的所有 get set 都不会再被调用。这意味着仅仅指定了默认值的属性将被编辑器记录和还原。
同样的,编辑可能无法在极端情况下正确地重置您的组件。如果你需要支持组件重置菜单,则需要在该方法中手工同步组件属性到“内部状态”。一旦你实现这个方法,组件的所有 get set 都不会在重置操作时被调用。这意味着仅仅指定了默认值的属性将被编辑器重置。
此方法仅在编辑器下会被调用。
schedule
调度一个自定义的回调函数。
如果回调函数已调度,那么将不会重复调度它,只会更新时间间隔参数。
参数列表
callback function The callback function
interval Number Tick interval in seconds. 0 means tick every frame.
repeat Number The selector will be executed (repeat + 1) times, you can use cc.macro.REPEAT_FOREVER for tick infinitely.
delay Number The amount of time that the first tick will wait before execution. Unit: s
示例
var timeCallback = function (dt) {
cc.log("time: " + dt);
}
this.schedule(timeCallback, 1);
scheduleOnce
调度一个只运行一次的回调函数,可以指定 0 让回调函数在下一帧立即执行或者在一定的延时之后执行。
参数列表
callback function A function wrapped as a selector
delay Number The amount of time that the first tick will wait before execution. Unit: s
示例
var timeCallback = function (dt) {
cc.log("time: " + dt);
}
this.scheduleOnce(timeCallback, 2);
unschedule
取消调度一个自定义的回调函数。
参数列表
callback_fn function A function wrapped as a selector
示例
this.unschedule(_callback);
unscheduleAllCallbacks
取消调度所有已调度的回调函数:定制的回调函数以及 update 回调函数。动作不受此方法影响。
示例
this.unscheduleAllCallbacks();
destroy
销毁该对象,并释放所有它对其它对象的引用。
实际销毁操作会延迟到当前帧渲染前执行。从下一帧开始,该对象将不再可用。
您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
示例
obj.destroy();
_destruct
清除实例中的所有引用。
注意:此方法不会清除在 CCObject 实例中定义的 getter 或 setter。如果需要,你可以重写 _destruct 方法。例如:
_destruct: function () {
for (var key in this) {
if (this.hasOwnProperty(key)) {
switch (typeof this[key]) {
case 'string':
this[key] = '';
break;
case 'object':
case 'function':
this[key] = null;
break;
}
}
}
_onPreDestroy
在对象被销毁之前调用。
_serialize
为此对象定制序列化。
参数列表
_deserialize
从自定义序列化数据初始化此对象。
参数列表
data Object the serialized json data
ctx _Deserializer