fly.js
// Learn cc.Class:
// - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
maxMoveSpeed:400,
accel:200,
//玩家移动速度
MoveSpeed:100
// 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;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
onl oad () {
//加速度开关
this.accleft=false;
this.accright=false;
this.accup=false;
this.accdown=false;
//水平方向速度
this.xspeed=3;
//竖直方向速度
this.yspeed=2;
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
},
start () {
},
onKeyDown(event){
switch(event.keyCode){
case cc.macro.KEY.a:this.accleft=true;
case cc.macro.KEY.d:this.accright=true;
case cc.macro.KEY.w:this.accup=true;
case cc.macro.KEY.s:this.accdown=true;
}
},
onKeyUp(event){
switch(event.keyCode){
case cc.macro.KEY.a:this.accleft=false;this.xspeed=3;
case cc.macro.KEY.d:this.accright=false;this.xspeed=3;
case cc.macro.KEY.w:this.accup=false;this.yspeed=0;
case cc.macro.KEY.s:this.accdown=false;this.yspeed=0;
}
},
update (dt) {//v=at
if(this.accleft){
this.xspeed-=this.accel*dt;
}else if(this.accright){
this.xspeed+=this.accel*dt;
}else if(this.accup){
this.yspeed+=this.accel*dt;
}else if(this.accdown){
this.yspeed-=this.accel*dt;
}
if(Math.abs(this.xspeed)>this.maxMoveSpeed)
this.xspeed=this.maxMoveSpeed*(this.xspeed/Math.abs(this.xspeed));
this.node.x+=this.xspeed*dt;
if(Math.abs(this.yspeed)>this.maxMoveSpeed)
this.yspeed=this.maxMoveSpeed*(this.yspeed/Math.abs(this.yspeed));
this.node.y+=this.yspeed*dt;
},
});
game.js
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// 背景移动速度
bgMoveSpeed:0,
// 背景
bg:{
default:null,
type:cc.Node
},
bg2:{
default:null,
type:cc.Node
},
// 子弹预制体资源
bulletPrefab:{
default:null,
type:cc.Prefab
},
// 玩家节点
player:{
default:null,
type:cc.Node
},
// 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;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// 加载脚本时自动调用
onl oad () {
// 子弹发射计时器
this.bulletTimer = 0;
// 子弹发射频率
this.shootDuration = 0.5;
},
start () {
},
// 发射子弹
shoot(dt){
// 累计时间
this.bulletTimer += dt;
// 是否达到发射时间
if(this.bulletTimer >= this.shootDuration)
{
// 计时器归零
this.bulletTimer = 0;
// 实例化一个预制体对象
var bullet = cc.instantiate(this.bulletPrefab);
// 添加到画布上
this.node.addChild(bullet);
// 获取英雄的位置设置给子弹
var pos = cc.v2(this.player.x + this.player.width/2,this.player.y);
bullet.setPosition(pos);
}
},
// 帧调度器。dt:两帧之间的时间间隔 speed s=vt
update (dt) {
this.bg.x = this.bg.x - this.bgMoveSpeed*dt;
this.bg2.x = this.bg2.x - this.bgMoveSpeed*dt;
if(this.bg.x <= -this.bg.width)
{
this.bg.x = this.bg2.x + this.bg2.width;
}
if(this.bg2.x <= -this.bg2.width)
{
this.bg2.x = this.bg.x + this.bg.width;
}
// 发射子弹
this.shoot(dt);
},
});
bullet.js
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// 子弹移动速度
moveSpeed:0
// 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;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// onl oad () {},
start () {
},
update (dt) {
// 每一帧移动的距离
var s = this.moveSpeed * dt;
// 修改子弹的位置
this.node.x = this.node.x + s;
},
});