CountMoney.zip是一款简单的计数钱小游戏的开源代码包,适用于想要学习或开发类似游戏的人士。此项目允许用户修改和扩展功能。
Creator数钱小游戏源码CountMoney.zip
使用cc.Class定义游戏脚本:
```javascript
cc.Class({
extends: cc.Component,
properties: {
nodeMoney: cc.Node,
nodeGuide: cc.Node,
nodeStart: cc.Node,
txtCount: cc.Label,
txtTime: cc.Label,
nodeMoneyBg: cc.Node,
nodeTimerBg: cc.Node,
nodeSpillMoney: cc.Node,
txtTotal: cc.Label
},
onLoad () {
this.node.on(cc.Node.EventType.TOUCH_START, event => {this.onTouchStart(event);});
this.node.on(cc.Node.EventType.TOUCH_MOVE, event => {this.onTouchMove(event);});
this.node.on(cc.Node.EventType.TOUCH_END, event => {this.onTouchEnd(event);});
this.nodeMoneyBg.zIndex = 10;
this.nodeTimerBg.zIndex = 10;
this.nodeStart.zIndex = 20;
this._touchNode = null;
this._count = 0;
this._isPlaying = false;
},
start () {
},
startGame(){
this._isPlaying = true;
this.nodeStart.active = false;
this.txtTime.string = 10;
this.txtCount.string = 0;
this.startTimer();
this.spillMoney();
},
startTimer(){
this.schedule(this.timeCallback, 1);
},
timeCallback(){
let time = parseInt(this.txtTime.string);
time--;
this.txtTime.string = + time;
if(time <= 0){
this.unschedule(this.timeCallback);
this.onTimeout();
}
},
onTimeout(){
this.onGameEnd();
},
onGameEnd(){
this.nodeStart.active = true;
this._isPlaying = false;
this.node.stopActionByTag(0xff);
let totalMoney = `${this._count * 100}`;
this.txtTotal.string = `¥${totalMoney}`;
},
onTouchStart(event){
if(!this._isPlaying){ return; }
this._touchNode = cc.instantiate(this.nodeMoney);
this._touchNode.active = true;
let parentNode = this.nodeMoney.parent;
this._touchNode.parent = parentNode;
this.nodeGuide.active = false;
},
onTouchMove(event){
if(!this._isPlaying){ return; }
let pos = event.getLocation();
pos = this._touchNode.parent.convertToNodeSpaceAR(pos);
if(pos.y > this._touchNode.y){
this._touchNode.y = pos.y;
}
},
onTouchEnd(event){
if(!this._isPlaying){ return; }
let nowPos = event.getLocation();
let startPos = event.getStartLocation();
if(nowPos.y - startPos.y > 10){
let seqAction = cc.sequence(
cc.spawn(cc.moveBy(0.3, 0, cc.winSize.height), cc.scaleTo(0.3, 0.7)),
cc.removeSelf()
);
this._touchNode.runAction(seqAction);
}
this._count++;
let countMoney = `${this._count * 100}`;
this.txtCount.string = countMoney;
},
onClick(){
this.startGame();
},
spillMoney(){
let seq = cc.sequence(
cc.delayTime(0.2),
cc.callFunc(() => {
let xPosition = Math.random() * cc.winSize.width - (cc.winSize.width / 2);
let node = cc.instantiate(this.nodeSpillMoney);
node.active = true;
let parentNode1 = this.nodeSpillMoney.parent;
node.parent = parentNode1;
node.runAction(cc.sequence(
cc.place(xPosition, cc.winSize.height/2 + node.height / 2),
cc.spawn(cc.moveBy(1, 0, -cc.winSize.height-node.height / 2), cc.rotateBy(1, 1080)),
cc.removeSelf()
));
})
);
seq.setTag(0xff);
this.node.runAction(seq.repeatForever());
},
});
```
以上是游戏脚本的定义,包括初始化、开始游戏、计时器设置和处理触摸事件等方法。