公众号“一枚小工”
两个核心js 文件如下
RedPacket.js +++++++++++++++++++++++++
const points=[5,15,20,50,80,100];
cc.Class({
extends: cc.Component, //编辑器属性,只在编辑状态有效
editor: CC_EDITOR && {
requireComponent: cc.Sprite, //要求节点必须有cc.Sprite组件
},
properties: {
spriteFrames: [cc.SpriteFrame], //定义一个SpriteFrames数组
_index: 0, //以下划线“_”开始的为私用变量
index: { //index属性控制图片切换
type: cc.Integer, //定义属性为整数类型
//这次没使用notify方式实现属性值的变化监听,改用getter/setter方式
get() {
return this._index;
},
//为负数退出
set(value) {
if (value < 0) {
return;
}
//根据spriteFrames组件长度计算this._index
this._index = value % this.spriteFrames.length;
//获取当前节点上的Sprite组件对象
let sprite = this.node.getComponent(cc.Sprite);
//设置Sprite组件的spriteFrame属性,变换图片
sprite.spriteFrame = this.spriteFrames[this._index];
},
},
point: 0,
ratio: 0,
addCallback: null,
removeCallback: null,
},
onLoad () {
this._index = this.getRandomNum(0,5);
this.point = points[this._index];
this.node.on(cc.Node.EventType.TOUCH_START, event => {
this.onTouchCallback();
});
},
onEnable(){
this.node.runAction(cc.sequence(
cc.moveBy(this.ratio, 0, -cc.winSize.height - this.node.height),
cc.callFunc( () => {
if(this.removeCallback != null){
this.removeCallback();
}
}),
cc.removeSelf()
));
},
setRatio(ratio){
this.ratio = ratio;
},
// update (dt) {}
getRandomNum(min, max){
return Math.floor(Math.random()*(max - min + 1) + min);
},
setAddCallback(callback){
this.addCallback = callback;
},
setRemoveCallback(callback){
this.removeCallback = callback;
},
onTouchCallback(){
this.node.stopAllActions();
let sprite = this.node.getComponent(cc.Sprite); //设置Sprite组件的spriteFrame属性,变换图片
sprite.spriteFrame = this.spriteFrames[this._index];
if(this.addCallback != null){
this.addCallback(this.point);
}
this.node.runAction(cc.sequence(
cc.delayTime(0.3),
cc.removeSelf()
));
}
});
Game.js++++++++++++++++++++++++++++++++++++
cc.Class({
extends: cc.Component,
properties: {
nodeResult:cc.Node,
txtPoint:cc.Label,
txtResult:cc.Label,
prefab: {
default: null,
type: cc.Prefab
},
nodeParent:cc.Node,
ratio: 4,//比率
offset : 0.5,
delta: 0, //距离底部的距离
count: 0,//抢到了几个红包
point: 0,//得分
isOver: false//游戏是否结束
},
onLoad () {
this.nodeResult.active = false;
},
update (dt) {
if(this.isOver){
return;
}
this.delta += dt;
if(this.delta >= this.offset){
this.ratio *= 0.99;
this.addRedPacket();
this.delta = 0;
}
},
getRandomNum(min, max){
return Math.floor(Math.random()*(max - min + 1) + min);
},
addRedPacket(){
let node = cc.instantiate(this.prefab);
let component=node.getComponent('RedPacket');
component.setAddCallback( point => {
this.point += point;
this.count++;
this.reloadUI();
this.onPointChanged();
});
component.setRemoveCallback( () => {
this.isOver = true;
this.nodeResult.active = true;
});
component.setRatio(this.ratio);
node.y = cc.winSize.height / 2 + node.height / 2;
//cc.log('##y',node.y);
let index = this.getRandomNum(0,3);
node.x = cc.winSize.width / 8 * (2 * index - 3);
node.parent = this.nodeParent;
},
onPointChanged(){
// this.ratio = this.count / 10;
// this.offset = this.count / 10;
},
onClick(event, data){
switch(data){
case 'again':{
cc.director.loadScene('home');
break;
}
}
},
reloadUI(){
this.txtResult.string = `哇塞!你抢到了${this.count}个红包!`;
this.txtPoint.string = `${this.point}`;
}
});