代码例子
1.小球不断上下跳动
cc.Class({
extends: cc.Component,
properties: {
jumpDuration:2,
jumpHeight:300
},
ballJumpAction:function(){
//向上跳动
var jumpUp = cc.moveBy(this.jumpDuration,cc.p(0,this.jumpHeight)).easing(cc.easeCubicActionOut());
//下落
var jumpDown = cc.moveBy(this.jumpDuration,cc.p(0,-this.jumpHeight)).easing(cc.easeCubicActionIn());
//不断重复
return cc.repeatForever(cc.sequence(jumpUp ,jumpDown));
},
// use this for initialization
onLoad: function () {
this.jumpAction = this.ballJumpAction();
this.node.runAction(this.jumpAction);
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
2.场景切换、倒计时
场景切换:
onLoad: function () {
this.node.on('mousedown',function(){
cc.director.loadScene('Scene2');
})
},
倒计时自动转换场景
//直接拖至画布上
cc.Class({
extends: cc.Component,
properties: {
timeLabel:{
default:null,
type:cc.Label
}
},
// use this for initialization
onLoad: function () {
var timeIn=5;
this.schedule(function(){
timeIn--;
this.time_Label.string=timeIn;
if(timeIn===0){
cc.director.loadScene('Scene3');
}
},1);
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
3.输入事件监听操作
// 键盘控制飞机移动ASWD控制;js挂到飞机(图片)上
cc.Class({
extends: cc.Component,
properties: {
accl:0,
plane:{
default:null,
type:cc.Node
}
},
setInputControl:function(){
var self = this;
var listener= {
event:cc.EventListener.KEYBOARD,
onKeyPressed:function(keyCode,event){
switch(keyCode){
case cc.KEY.a:
self.accLeft= true;
break;
case cc.KEY.d:
self.accRight= true;
break;
case cc.KEY.w:
self.accUp= true;
break;
case cc.KEY.s:
self.accDown= true;
break;
}
},
onKeyReleased:function(keyCode,event){
switch(keyCode){
case cc.KEY.a:
self.accLeft= false;
break;
case cc.KEY.d:
self.accRight= false;
break;
case cc.KEY.w:
self.accUp= false;
break;
case cc.KEY.s:
self.accDown= false;
break;
}
}
}
cc.eventManager.addListener(listener, self.node)
},
// use this for initialization
onLoad: function () {
this.accLeft = false;
this.accRight = false;
this.accUp = false;
this.accDoen = false;
this.setInputControl();
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
if(this.accLeft){
this.plane.x -=this.accl;
}
if(this.accRight){
this.plane.x +=this.accl;
}
if(this.accUp){
this.plane.y +=this.accl;
}
if(this.accDown){
this.plane.y -=this.accl;
}
},
});