cocos-creator使用记录6_节点的操作



1.创建新节点
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);
  },
});


2.克隆已有节点
cc.Class({
  extends: cc.Component,


  properties: {
    target: {
      default: null,
      type: cc.Node,
    },
  },


  start: function () {
    var scene = cc.director.getScene();
    var node = cc.instantiate(this.target);


    node.parent = scene;
    node.setPosition(0,0);
  },
});


3.创建预置节点
cc.Class({
  extends: cc.Component,


  properties: {
    target: {
      default: null,
      type: cc.Prefab,
    },
  },


  start: function () {
    var scene = cc.director.getScene();
    var node = cc.instantiate(this.target);


    node.parent = scene;
    node.setPosition(0,0);
  },
});


4.销毁节点
cc.Class({
  extends: cc.Component,


  properties: {
    target: cc.Node,
  },


  start: function () {
    setTimeout(function () {
      this.target.destroy();
    }.bind(this), 5000);
  },


  update: function (dt) {
    //判断 当前节点是否已经被销毁
    if ( !cc.isValid(this.target) ) {
      this.enabled = false;
      return;
    }


    this.target.rotation += dt * 10.0;
  },
});


5.节点上的事件
position-changed
size-changed
anchor-changed
child-added
child-removed
child-reorder
group-changed
touchstart


this.node.on("position-changed", function(event){
cc.log('移动了哦');
var action3 = event.currentTarget.getActionByTag(123);
if(action3.idDone()){
cc.log("执行完毕");
}
});


6.获得子节点
this.endNode.getChildByName("title")

猜你喜欢

转载自blog.csdn.net/haibo19981/article/details/80435603