cocos Creator 学习日志
犹犹豫豫很久,终于还是选择了cocos creator。为了养成一个良好的习惯,决定把每天的点点滴滴记录下来。
啥都不会,当然是各种找教程各种找资源啦,顺序就是我找到的顺序,要从哪里开始看各位看官可以自行决定。
1、技术胖的视频
2、司令部的教程贴,据说很久没更新了
3、一个完整的小游戏–极限跳跃
4、其实这位大神已经整理的非常好了,我还傻不拉几的到处找
-遇到的第一个坑——creator提示不支持webgl,场景加载不出来——先更新显卡驱动
去看技术胖的教程视频,很尴尬,第一部就放不出来,接着第二部
-今天学的几个方法:
1、cc.moveby(时长,坐标(坐标用cc.p(x,y)))).easing(cc.easeCubicActionOut());
ease-in 规定以慢速开始的过渡效果; ease-out 规定以慢速结束的过渡效果;
2、cc.repeatForever()不断的重—多个动作连接里面就填cc.sequence(动作1,动作2)
3、this.node.on(‘mousedown’,function(){
cc.director.loadScene(‘Scene2’);})鼠标点击加载某场景
4、this.schedule(function,时间)延时执行
5、cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);某个按键的监听事件,技术胖视频上用的方法好像已经废弃了,还是群里大佬告诉我的。。。这里坑了好久详情看官网
6、cc.instantiate(node); //复制节点,应该是跟预制资源一起用的
7、cc.random0To1( ) ; //生成0和1之间的随机数,好东西啊
然后根据学的,我把某些东西给改了一下,下面展示一下今天的成果:
下面放上代码,嘿嘿
cc.Class({
extends: cc.Component,
properties: {
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
onLoad:function () {
this.node.on('mousedown',function () {
cc.director.loadScene('PlanGmaeScene');
})
},
start () {
},
// update (dt) {},
});
cc.Class({
extends: cc.Component,
properties: {
speed:0,
player:{
default:null ,
type:cc.Node
}
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
onLoad:function () {
var accUp=false;
var accDown=false;
var accLeft=false;
var accRight=false;
// this.setInputControl();
// var self=this;
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
},
start () {
},
onKeyDown:function (event) {
switch (event.keyCode){
case cc.KEY.a:
this.accLeft=true;
break;
case cc.KEY.d:
this.accRight=true;
break;
case cc.KEY.w:
this.accUp=true;
break;
case cc.KEY.s:
this.accDown=true;
break;
}
},
onKeyUp:function(event){
switch (event.keyCode){
case cc.KEY.a:
this.accLeft=false;
break;
case cc.KEY.d:
this.accRight=false;
break;
case cc.KEY.w:
this.accUp=false;
break;
case cc.KEY.s:
this.accDown=false;
break;
}
},
onDestroy () {
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
},
update:function (dt) {
if (this.accLeft){
if (this.player.x<-210){
this.player.x=-210
}else{
this.player.x -=this.speed;
console.log('横坐标:'+this.player.x)
}
}
if (this.accRight){
if (this.player.x>210){
this.player.x=210
}else{
this.player.x +=this.speed;
console.log('横坐标:'+this.player.x)
}
}
if (this.accUp){
if (this.player.y>438){
this.player.y=438;
}else{
this.player.y +=this.speed;
}
}
if (this.accDown){
if(this.player.y<-438){
this.player.y=-438;
}else{
this.player.y -=this.speed;
}
}
},
});
cc.Class({
extends: cc.Component,
properties: {
//敌人预制战机
enemy:{
default:null,
type:cc.Prefab
}
},
//生产敌人
newEnemy:function () {
//复制节点
var newEnemy=cc.instantiate(this.enemy);
//把生产出来的节点放到cavans上
this.node.addChild(newEnemy,100);//100是层级
//设置生产的敌人的初始位置
newEnemy.setPosition(this.makeRandomPosition());
//生产的敌人从上往下移动 (时间,目的地坐标)横坐标不变,纵坐标到最下面
//var moveTo= cc.moveTo(1,cc.p(newMoney.getPositionX(),-this.node.height/2-50));
var moveto=cc.moveTo(3,cc.p(newEnemy.getPositionX(),-this.node.height/2-50));
//当敌人移出屏幕后移除
var finish=cc.callFunc(newEnemy.removeFromParent,newEnemy);
//动画
var myAction=cc.sequence(moveto,finish);
//启动动画
newEnemy.runAction(myAction);
},
//生成一个随机的坐标
makeRandomPosition:function () {
//原点在canvas中央
var positionX=cc.random0To1()*400-200;
var positionY=this.node.height/2+100;
return cc.p(positionX,positionY);
},
// LIFE-CYCLE CALLBACKS:
onLoad:function () {
//每0.2秒生产一个敌人
this.schedule(function(){
this.newEnemy();
},0.5);
// this.schedule(function () {
// this.newEnemy();
// },0.2);
},
// onLoad () {},
start () {
},
// update (dt) {},
});
cc.Class({
extends: cc.Component,
properties: {
timeLable:{
default:null,
type:cc.Label
},
jumpDuration:1,
jumpHeight:300
},
jumpAction:function () {
var jumpUp=cc.moveBy(this.jumpDuration,cc.p(0,this.jumpHeight)).easing(cc.easeCubicActionInOut());
var jumpDown=cc.moveBy(this.jumpDuration,cc.p(0,-this.jumpHeight)).easing(cc.easeCubicActionIn());
return cc.repeatForever(cc.sequence(jumpUp,jumpDown));
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
onLoad:function () {
var timeInt=5;
this.schedule(function () {
timeInt--;
this.timeLable.string=timeInt;
if (timeInt==0){
cc.director.loadScene('GameOverScene');
}
},1);
this.node.runAction(this.jumpAction());
},
start () {
},
// update (dt) {},
});