cocosCreator项目 打地鼠

本文档介绍了如何利用cocosCreator框架进行打地鼠游戏的开发,包括主要的游戏逻辑实现、角色控制及交互等关键部分。
摘要由CSDN通过智能技术生成

game.js


cc.Class({
    extends: cc.Component,
    properties: {
 
//老鼠的预设体数组
mousePrefab: [cc.Prefab],
//老鼠的位置数组
        pointmouse:[cc.Vec2],
 //锤子的的预设体                default:系统默认值    cc.Prefab预制文件实例
     chuziPrefab:{
            default:null,
            type:cc.Prefab
        }

    },
spawnNewMouse: function() {
    //获取0-2的数
        var num=Math.floor(Math.random()*3)
//cc.instantiate来进行初始化(可以理解为实例化创建)
        var newMouse=cc.instantiate(this.mousePrefab[num]);
// 将新增的节点添加到 Canvas 节点下面
            this.node.addChild(newMouse);

            this.mouse=newMouse;

// 为mouse设置一个随机位置
          var num1=Math.floor(Math.random()*9)
            newMouse.setPosition(this.pointmouse[num1]);

            this.mousepoint=newMouse.getPosition()
Creator打地鼠游戏hitmouse.zip // 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: { mouse: [cc.Node], hammer: cc.Node, txtCount: cc.Label, }, // LIFE-CYCLE CALLBACKS: 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.g
以下是一个简单的Cocos Creator打地鼠游戏的代码实现: 1. 创建场景和精灵 首先,在Cocos Creator中创建一个新场景,然后添加一个背景精灵和一个地鼠精灵。 2. 地鼠的运动 在地鼠脚本中,我们可以使用schedule函数来实现地鼠的运动。 ```javascript cc.Class({ extends: cc.Component, properties: { speed: 5, // 地鼠的速度 moveRange: 100, // 地鼠移动的范围 }, onLoad () { // 记录地鼠的初始位置 this.initX = this.node.x; // 地鼠向左或向右移动 this.direction = Math.random() > 0.5 ? 1 : -1; // 开始地鼠运动 this.schedule(this.move, 0.1); }, move () { // 计算地鼠新的位置 let x = this.node.x + this.direction * this.speed; let y = this.node.y; // 判断是否超出移动范围 if (Math.abs(x - this.initX) > this.moveRange) { this.direction *= -1; } // 更新地鼠的位置 this.node.setPosition(cc.v2(x, y)); }, }); ``` 3. 点击地鼠地鼠脚本中,我们可以添加一个鼠标点击事件来处理玩家点击地鼠的行为。 ```javascript cc.Class({ extends: cc.Component, properties: { scoreLabel: cc.Label, // 分数显示 speed: 5, // 地鼠的速度 moveRange: 100, // 地鼠移动的范围 score: 0, // 分数 }, onLoad () { // 记录地鼠的初始位置 this.initX = this.node.x; // 地鼠向左或向右移动 this.direction = Math.random() > 0.5 ? 1 : -1; // 开始地鼠运动 this.schedule(this.move, 0.1); // 添加鼠标点击事件 this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onClick, this); }, move () { // 计算地鼠新的位置 let x = this.node.x + this.direction * this.speed; let y = this.node.y; // 判断是否超出移动范围 if (Math.abs(x - this.initX) > this.moveRange) { this.direction *= -1; } // 更新地鼠的位置 this.node.setPosition(cc.v2(x, y)); }, onClick () { // 增加分数 this.score += 10; this.scoreLabel.string = 'Score: ' + this.score; // 隐藏地鼠 this.node.active = false; // 等待一段时间后重新显示地鼠 this.scheduleOnce(() => { this.node.active = true; }, 1); }, }); ``` 4. 游戏结束 在场景脚本中,我们可以添加一个定时器来控制游戏时间。 ```javascript cc.Class({ extends: cc.Component, properties: { timerLabel: cc.Label, // 时间显示 time: 30, // 游戏时间 }, onLoad () { // 开始倒计时 this.schedule(this.countdown, 1); }, countdown () { this.time--; this.timerLabel.string = 'Time: ' + this.time; // 时间到了,游戏结束 if (this.time <= 0) { cc.director.loadScene('GameOver'); } }, }); ``` 5. 游戏重置 在游戏结束场景中,我们可以添加一个按钮来重新开始游戏。 ```javascript cc.Class({ extends: cc.Component, properties: { scoreLabel: cc.Label, // 分数显示 }, onLoad () { // 显示分数 let score = cc.sys.localStorage.getItem('score'); this.scoreLabel.string = 'Score: ' + score; }, restart () { // 重新开始游戏 cc.director.loadScene('Game'); }, }); ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值