Cocos Creator 创建和销毁节点(官方文档摘录)
发布时间:2020-12-14 17:15:43 所属栏目:百科 来源:网络整理
导读:创建新节点 除了 通过场景编辑器创建节点 外,我们也可以 在脚本中动态创建节点 。通过 new cc.Node() 并将它加入 到场景中,可以实现整个创建过程。 以下是一个简单的例子: cc.Class({ extends: cc.Component,properties: { sprite: { default : null ,type
创建新节点除了通过场景编辑器创建节点外,我们也可以在脚本中动态创建节点。通过 以下是一个简单的例子: cc.Class({
extends: cc.Component,properties: {
sprite: {
default: null,type: cc.SpriteFrame,},start: function () {
var node = new cc.Node('sprite ' + this.count);
var sp = node.addComponent(cc.Sprite);
sp.spriteFrame = this.sprite;
node.parent = this.node;
node.setPosition(0,0);
},});
克隆已有节点有时我们希望动态的克隆场景中的已有节点,我们可以通过cc.instantiate 生成。使用方法如下:
销毁节点通过{ setTimeout(function () { this.target.destroy(); }.bind(this),128);">5000); },update: function (dt) { if ( !cc.isValid(this.target) ) { this.enabled = false; return; } this.target.rotation += dt * 10.0; },}); |