目录
14. AudioSource组件是音频源组件, 发出声音的源头
待续...
建议:没有js基础的,建议简单学习下js基础教程
https://blog.youkuaiyun.com/osuckseed/article/details/103560281
1.cc.node (脚本入口函数介绍在第7节)
//获取node 属性并打印
console.log("onload");
console.log(this.node.name);
var item=this.node.getChildByName("sprite");
var pos=item.getPosition();
2.cc.v2 和 cc.Rect
//坐标点定义
// var v2 = cc.v2(0, 0);
// var v2=cc.v2(300,300);
// console.log(v2);
//向量加减 (add ,sub)
// var v1= v2.sub(cc.v2(-100,-100));
// console.log(v1);
//向量长度
// var b= v1.mag();//Sqr();
// console.log(b);
//矩形 (xy(起始点) w(宽) h(高))
// var s=new cc.size(200,200);
// console.log(s);
// var r=new cc.Rect(100,100,100,100);
// console.log(r);
// r=cc.rect(0,0,200,200);
//判断点是否在矩形内
// var b=r.contains(cc.v2(59,95));
// console.log(b);
//判断矩形是否相交
// b= r.containsRect(cc.rect(0,-100,100,100));
// console.log(b);
3.节点坐标和屏幕坐标的转换
注意:
a.coscos 默认锚点为原点计算,请使用convertToWorldSpaceAR(带AR的方法进行互转)
b.注意分辨率与canvas 的中心点的关系
// 233 93 /363.5 273.5
//节点坐标转到屏幕坐标
var w_pos= this.node.convertToWorldSpace(cc.v2(0,0)); //图片左下角为原点 (480,270)(减去图片的一半)
console.log(w_pos);
var w_pos= this.node.convertToWorldSpaceAR(cc.v2(0,0));// 以图片锚点为原点
console.log(w_pos);
//世界坐标转节点坐标
var w_pos=cc.v2(480,320);
var node_pos=this.node.convertToNodeSpace(w_pos);
console.log(node_pos);
var node_pos=this.node.convertToNodeSpaceAR(w_pos);
console.log(node_pos);
// w_pos=this.node.convertToWorldSpaceAR(cc.p(0,0));
// console.log(w_pos);
//cc.Size
4.获得node的显示范围
//获取节点包围盒,相对于父亲节点下的包围盒 (以ar为原点 )(可进行矩形判断了)
var box= this.node.getBoundingBox();
console.log(box);
//
var box= this.node.getBoundingBoxToWorld();
console.log(box);
this.node.on(cc.Node.EventType.TOUCH_START,function (params) {
var w_pos=params.getLocation();
var pos=this.node.convertToNodeSpaceAR(w_pos);
console.log(pos);
},this);
this.node.on(cc.Node.EventType.TOUCH_START,function (params) {
var w_pos=params.getLocation();
var pos=this.node.convertToNodeSpaceAR(w_pos);
var t_pos=this.node.convertTouchToNodeSpaceAR(params);
console.log(pos);
console.log(t_pos);
},this);
//把当前的sub 移动到 世界坐标900,600 的位置
//把世界坐标转到相对于他的父亲节点的坐标
//var node_pos=this.node.convertToWorldSpaceAR(cc.p(-300,-300));
//this.node.setPosition(node_pos);
//获取当前节点的世界坐标
this.node.convertToWorldSpaceAR(cc.v2(0,0));
console.log(this.node.x,this.node.y);
this.node.convertToNodeSpaceAR(cc.v2(0,0));
console.log(this.node.x,this.node.y);
this.node.setPosition(cc.v2(100,100));
var node_pos=this.node.convertToWorldSpaceAR(cc.v2(100,100));
console.log(node_pos);
5.node.on( 事件监听)
this.node.on(cc.Node.EventType.TOUCH_START,function(t){
console.log(cc.Node.EventType.TOUCH_START);
},this);
this.node.on(cc.Node.EventType.TOUCH_MOVE,function(t){
console.log(cc.Node.EventType.TOUCH_MOVE);
},this);
this.node.on(cc.Node.EventType.TOUCH_END,function(t){
console.log(cc.Node.EventType.TOUCH_END);
this.on_touch_move(t);
},this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL,function(t){
console.log(cc.Node.EventType.TOUCH_CANCEL);
},this);
6.coscos中的事件(事件对列延迟等等)
//即时 延迟
//move to (目标)/ move By(变化)
// var a= cc.moveTo(1,cc.v2(100,100));
// this.node.runAction(a);
// var b=cc.moveBy(1,cc.v2(100,100));
// this.node.runAction(b);
// var r=cc.rotateTo(1,-120);
// var r2=cc.rotateBy(1,180);// 在原来的基础上变化
// this.node.runAction(r2);
// var s=cc.scaleTo(2,5);//到2.1
// this.node.scale=2;
// var s2=cc.scaleBy(1,1.1);// 变化乘以原来的倍数
// this.node.runAction(s2);
//淡入 淡出 改变透明度
// this.node.opacity=1;
// var f=cc.fadeIn(1);
// var f2=cc.fadeOut(1);
// var f3=cc.fadeTo(1,128);//多长时间渐变到指定值
// this.node.runAction(f3);
//
var func=cc.callFunc(function(){
console.log( " call func");
}.bind(this));
//注意:这里不是顺序执行,runAction 仅仅是调用,可能是异步吧
// console.log( " call func start");
// this.node.runAction(func);
// console.log( " call func end");
// this.node.on(cc.Node.EventType.touch);
//命令队列
// var m1=cc.moveTo(1,100,100);
// var m2=cc.fadeOut(1);
// var seq=cc.sequence(m1,m2);
// this.node.runAction(seq);
//同时运行多个 一边 一边的效果
// this.node.runAction(m1);
// this.node.runAction(m2);
//重复
// var m1=cc.scaleTo(1,1.1);
// var m2=cc.scaleTo(1,0.5);
// var seq=cc.sequence(m1,m2);
// var rf=cc.repeatForever(seq);
// this.node.runAction(rf);
//缓动机制
// var m=cc.moveTo(1,100,0).easing(cc.easeBackOut());
// this.node.runAction(m);
// var r=cc.rotateBy(3,360).easing(cc.easeCubicActionIn());
// var rf=cc.repeatForever(r);
// this.node.runAction(rf);
//停止
// this.node.stopAction(rf);
// this.node.stopAllActions();
// var endfunc=cc.callFunc(function(){
// this.node.removeFromParent();
// }.bind(this));
// var seq=cc.sequence(r,endfunc);
// this.node.runAction(seq);
//延迟操作
var dt=cc.delayTime(3);
var effect=cc.fadeOut(1,0.5);
var end=cc.callFunc(function () {
this.node.removeFromParent();
}.bind(this));
var seq=cc.sequence(dt,effect,end);
this.node.runAction(seq);
7.cc.compenont 的固定入口函数
//返回了一个构造函数,继承了 cc.Component
//当前代码组件也有cc.Component 的方法
//cc.componnet 的固定入口函数
cc.Class({
extends: cc.Component,
properties: {
},
// LIFE-CYCLE CALLBACKS:
//组件在加载的时候
//你可以在onload访问场景节点数据,这里都能访问到,保证你可以获取到
onLoad () {
//获取组件信息
console.log(this.node);
console.log(this.name,this.node.name);
},
//组件在第一次upadte 调用之前
start () {
//cc.component 的规则以及使用
},
//每一次刷新时调用
update (dt) {},
onEnable()
{
//显示
console.log("onenable");
},
onDisable()
{
//隐藏
},
onDestroy()
{
},
lateUpdate()
{
//显示
//console.log("onlateUpdate");
},
onRestore()
{
}
});
8.属性列表的定义
//属性列表 (在编辑器中访问数据)
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
//基本数据类型: 数,bool ,string,color, pos ,szie
speed:100,
isDebug:false,
bossName:"",
color: cc.color,
pos:cc.v2(0,0),
sizw:cc.size(100,100),
//其它组件
//系统组件
sprite_item:{
type: cc.Sprite,
default:[],//null/[] (数组)
},
//代码组件
com_self:{
type:actionTest,
default:null,
},
//其它,去源码中参考 C:\CocosCreator_2.2.1\resources\engine ex:搜所lable
},
9.组件的增删改查
//组件在第一次upadte 调用之前
start () {
//cc.component 的规则以及使用
//组件的添加
this.addComponent("actionTest");
this.node.addComponent(actionTest);
//var l= this.node.addComponent(cc.Label);
//组件的查找
var inst= this.getComponent("actionTest");
var instArry= this.getComponents("actionTest");
//this.getComponentInChildren
console.log(inst,instArry);
//删除组件
this.onDestroy(inst);
console.log(instArry);
},
10.定时器的使用(schedule)
//启动定时器 节点或组件必须时激活状态 (隐藏删除无法启动)
// this.scheduleOnce(function(){
// //只会启动一次
// console.log("schedu once");
// }.bind(this),3);// 3s late start
//5秒后开始 间隔1秒循环执行 cc.repeatForever 可替换为执行次数
// this.schedule(function(){
// console.log("schedu");
// }.bind(this),1,cc.repeatForever,5);
// this.schedule(function(){
// console.log("schedu");
// }.bind(this),1,9,5); //次数10 =9+1
// this.scheduleOnce(function(){
// //取消所有定时器
// this.unscheduleAllCallbacks();
// }