用cocos creator开发音乐小游戏

前言

本文介绍使用cocos creator开发小游戏,用到了碰撞检测粒子动画等一些功能。可以点击链接体验一下

玩法

点击屏幕,游戏开始。音符开始往下走,点击屏幕,控制小人跟音符碰撞即可得分。场景中的发光小星星,碰撞时的效果,以及发光小人的拖尾,都是使用的粒子特效。

核心代码

1. 玩家移动

js 复制代码
 /**
 * 玩家移动,利用贝塞尔曲线,这样移动起来更丝滑
 */
move() {
    if(!this.isMoving) {
        this.isMoving = true;
        let nowPs = this.node.getPosition();
        this.position = this.position === "left" ? "right" : "left";
        this.node.runAction(
            cc.sequence(
                cc.bezierTo(0.3, [nowPs, cc.v2(0, -50), nowPs.neg()]),
                cc.callFunc(() => {
                    this.isMoving = false;
                    this.node.angle = 0;
                    this.node.getComponent(cc.BoxCollider).enabled = true;
                })
            )
        );
    }
}

2. 点击时按钮,重置粒子系统

js 复制代码
this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
    let pos = event.getLocation();
    this.clickNode.position = this.player.parent.convertToNodeSpaceAR(pos); //地址转换
    this.clickNode.getComponent(cc.ParticleSystem).resetSystem();
    this.player.getComponent("Player").move();
})

3. 碰撞检测

将元素添加碰撞体,然后分组,不同的物体执行不同的方法

js 复制代码
   onCollisionEnter(other, self) {
        let tag = other.tag; //0:普通音符;1:竖线;2:音符上方线;3:终点线;4:游戏结束
        switch(tag){
            case 0:
                this.node.getComponent(cc.BoxCollider).enabled = false;
                cc.find("Canvas").emit("noteCollide", other);
                this.showStateByPos();
                break;
            case 1:
                if(!this.node.getComponent(cc.BoxCollider).enabled) return;
                cc.find("Canvas").emit("gameOver", "fail");
                break;
            case 2: 
                cc.find("Canvas").emit("gameOver", "fail");
                break;
            case 3: 
                cc.find("Canvas").emit("toTheEnd");
                break;
            case 4: 
                cc.find("Canvas").emit("gameOver", "success");
                break;
        }
    }

参考链接

cocos官网:docs.cocos.com/creator/man...

粒子特效网:www.effecthub.com/item/featur...

相关推荐
LcGero10 天前
TypeScript 快速上手:泛型与工具类型
typescript·cocos creator·游戏开发
LcGero11 天前
Cocos Creator 3.x 高维护性打字机对话系统设计与实现
cocos creator·打字机
LcGero11 天前
Cocos Creator 三端接入穿山甲 SDK
sdk·cocos creator·穿山甲
LcGero12 天前
Cocos Creator平台适配层框架设计
cocos creator·平台·框架设计
LcGero13 天前
Cocos Creator 业务与原生通信详解
android·ios·cocos creator·游戏开发·jsb
LcGero14 天前
TypeScript 快速上手:前言
typescript·cocos creator·游戏开发
Setsuna_F_Seiei15 天前
CocosCreator 游戏开发 - 多维度状态机架构设计与实现
前端·cocos creator·游戏开发
CodeCaptain3 个月前
cocoscreator 2.4.x 场景运行时的JS生命周期浅析
cocos creator·开发经验
CodeCaptain3 个月前
CocosCreator 3.8.x [.gitignore]文件内容,仅供参考
经验分享·cocos creator
VaJoy5 个月前
Cocos Creator Shader 入门 (21) —— 高斯模糊的高性能实现
前端·cocos creator