当前位置: 移动技术网 > IT编程>开发语言>JavaScript > cocos-creator 脚本逻辑-1

cocos-creator 脚本逻辑-1

2018年09月13日  | 移动技术网IT编程  | 我要评论

1.节点

编辑组件的脚本文件时。可以通过以下语句获得节点

this 就是当前组件

this.node

拿到组件依附的节点 

 

this.node.parent

拿到组件依附的节点 的父节点

 

this.node.children

拿到组件依附的节点 的子节点

 

this.node.getchildbyname(‘’)

通过名称取得特定名的直系子节点

 

cc.removeself() 

从节点树删除该节点 留在内存,但已经不在节点中 如果 args = true 则消失

 

跨直系引用节点

在properties 中声明 node 对象再在编辑器中拖拽绑定

就可以在组建内部直接引用

两个属性

默认组件名样式 name:节点<组件>

isvalid 组件是否还在活动

 

2.动作

所有的动作 api 是由节点提供的,所以我们要先拿到节点

如 node.runaction()

 

1)动作运行:

runaction('action') 开始一个动作

stopaction(‘action’) 停止一个动作

stopallaction() 停止该节点下的所有动作

 

2) 设置tag

action.settag('tag_name')

node.getactionbytag('tag_name')

 

3)移动

cc.moveto(1,20,20) 用 1s 以父节点锚点为坐标中心移动到(20,20)的位置

可以移动到一个点的相对位置,比如可以换成 cc.v2(100,100) 以c2为坐标的(100,100)

cc.rotateto(1,50) 1s 第二个参数是角度

cc.moveby() 相对移动

cc.scaleto(1,2,2) 1s 放大到 2,2 scale

cc.fadeout(1) 1s 消失

 

4)动作包装

包装为一个动作

cc.sequence(

动作1,

动作2

)

 

5)容器动作

顺序动作:cc.sequence

可执行来回往复运动,如:

var seq = cc.sequence(cc.moveby(0.5, 200, 0), cc.moveby(0.5, -200, 0));

node.runaction(seq);//左右来回移动

 

同步动作:cc.spawn

可执行叠加系列动作,如:

var spawn = cc.spawn(cc.moveby(0.5, 0, 50), cc.scaleto(0.5, 0.8, 1.4));

node.runaction(spawn);//向上同时缩放

 

重复动作:cc.repeat

可执行重复一个动作,如:

var seq = cc.repeat(
             cc.sequence(
                 cc.moveby(2, 200, 0),
                 cc.moveby(2, -200, 0)
             ), 5);
 node.runaction(seq);//左右移动,重复5次


永远重复动作:cc.repeatforever
可执行一直重复动作,如:
 var seq = cc.repeatforever(
             cc.sequence(
                 cc.moveby(2, 200, 0),
                 cc.moveby(2, -200, 0)
             ));//一直重复左右移动


改变速度:cc.speed
可执行改变速率,如:
var action = cc.speed(
                 cc.spawn(
                     cc.moveby(2, 0, 50),
                     cc.scaleto(2, 0.8, 1.4)
                 ), 0.5);
 node.runaction(action);//速度加快一倍,2秒变1秒
 
 

 

3.回调函数 callfunc

cc.callfunc(this.callback, this, {'args':args}) 函数,对象,参数

callback :function(targetnode, args){}

 

4.事件/交互内容

添加 label 内置组件来添加名称,方便于判断

在操作过程中记得返回编辑器拖拽补充引用。

 

emit 和 on 配套 发射和监听

this.node.emit(”事件“,{参数}); 发生“事件” 同时给出参数

this.node.on(”事件“,this.event1, this.node1) 发生了“事件”后 执行 event1 执行对象是这个 node

this.node.on(”事件“,this.event2,this.node2)

限制:(参数必须和 on 一致 )

this.node.once() 只监听听一次 

this.node.off() 不再监听 on 

实例:

这里的 this 指的是 node1

  1.  
    event1: function(e){
  2.  
    console.log(e.detail),
  3.  
    this.runaction(cc.moveto(1,200,0))
  4.  
    },

 

5 触摸事件 鼠标事件 键盘事件

1)触摸(因为用的比较多这个写完整,其它都只写一个)

this.node.on(‘touchstart’,this.ontouchstart, this) 开始触摸

this.node.on(‘touchmove’,this.ontouchmove,this); 触摸移动,持续触发

this.node.on(‘touchend’,this.ontouchend, this) 触摸停止

this.node.on(‘touchcancel’, this.ontouchcancel, this) 触摸取消,(节点外释放)

 

this.node.on(‘mouseup’,this.onmouseup,this)  鼠标脱离

mousedown onmousedown 鼠标按下 

mousemove onmousemove 鼠标移动(不一定要点击)

mouseenter onmouseenter 鼠标进入节点区域

mouserleave onmouseleave 鼠标离开节点区域

mousewheel onmousewheel 鼠标滚轮

获取点击

getbutton() 返回 0 1 2 对应 左中右

 

2)键盘事件 

cc.systemevent.on(type,callback,target)

返回一个 keycode 

可选的 type 类型有:

  1. cc.systemevent.eventtype.key_down (键盘按下)
  2. cc.systemevent.eventtype.key_up (键盘释放)
  3. cc.systemevent.eventtype.devicemotion (设备重力传感)

例如

cc.systemevent.off(cc.systemevent.eventtype.key_down, this.onkeydown, this);

if e.keycode == cc.key.w 当 keycode 是 w 时

cc.key.(按键)

 

3)获取坐标

getlocation() 获取全局坐标下的圆心

例子:

ontouchstart: function(e){

    e.getlocation() 

}

坐标转换:从全局坐标系转到本地坐标系

let locationofthisnode = this.node.converttonodespacear(e.getlocation()) ar 忽略锚点 this.node目标坐标系对应节点

让某个节点的坐标更新

this.node.position = locationofthisnode

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网