
Creator开发的打地鼠游戏hitmouse.zip
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
HitMouse.zip是由Creator精心打造的一款趣味横生的经典打地鼠游戏。玩家需快速反应,精准点击屏幕上随机出现的小老鼠,挑战高分纪录,享受轻松愉快的游戏时光。
Creator打地鼠游戏hitmouse.zip
使用cc.Class创建一个组件:
```javascript
cc.Class({
extends: cc.Component,
properties: {
mouse: [cc.Node],
hammer: cc.Node,
txtCount: cc.Label,
},
onLoad () {
var manager = cc.director.getCollisionManager();
manager.enabled = true;
this.count = 0;
this.txtCount.string = this.count;
},
start () {
cc.director.getScheduler().schedule(this.logic, this, 0.5, false);
this.node.on(cc.Node.EventType.TOUCH_START, function(event){
for(let i = 0; i < this.mouse.length; i++){
if(this.mouse[i].opacity == 0){
continue;
}
let parent = this.mouse[i].parent;
let maskPos = parent.parent.convertToWorldSpaceAR(parent.position);
let maskRect = cc.rect(maskPos.x - parent.width / 2 , maskPos.y, parent.width, parent.height);
let mouse = this.mouse[i];
let mousePos = parent.convertToWorldSpaceAR(mouse.position);
let mousRect = cc.rect(mousePos.x - mouse.width / 2, mousePos.y - mouse.height / 2, mouse.width, mouse.height);
let mixRect = new cc.Rect();
maskRect.intersection(mixRect, mousRect);
if(mixRect.contains(event.getLocation())){
this.hammer.position = parent.position;
this.hammer.active = true;
this.count++;
this.txtCount.string = this.count;
break;
}
}
},this);
this.node.on(cc.Node.EventType.TOUCH_END, function(event){
this.hammer.active = false;
},this);
},
logic(){
let index = Math.floor(Math.random() * 9);
if(index < 0 || index >= this.mouse.length){
return;
}
this.mouse[index].getComponent(mouse).appear();
},
});
```
这段代码定义了一个用于打地鼠游戏的组件,包含鼠标、锤子和计分板。通过监听触摸事件来控制玩家是否成功击中地鼠,并更新分数显示。同时每0.5秒随机选择一个位置让地鼠出现。
全部评论 (0)


