用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...

相关推荐
VaJoy13 天前
Cocos Creator Shader 入门 ⑺ —— 图层混合样式的实现与 Render Texture
cocos creator
VaJoy15 天前
Cocos Creator Shader 入门 ⑹ —— 灰阶、反色等滤镜的实现
cocos creator
VaJoy17 天前
Cocos Creator Shader 入门 ⑸ —— 代码复用与绿幕抠图技术
cocos creator
VaJoy19 天前
Cocos Creator Shader 入门 ⑷ —— 纹理采样与受击闪白的实现
cocos creator
VaJoy20 天前
Cocos Creator Shader 入门 ⑶ —— 给节点设置透明度
cocos creator
VaJoy22 天前
Cocos Creator Shader 入门 (2) —— 给节点染色
cocos creator
VaJoy23 天前
Cocos Creator Shader —— 附录
cocos creator
成长ing1213824 天前
多层背景视差滚动Parallax Scrolling
cocos creator
似水流年wxk1 个月前
cocos creator使用jenkins打包微信小游戏,自动上传资源到cdn,windows版运行jenkins
运维·jenkins·cocos creator
成长ing121383 个月前
点击音效系统
前端·cocos creator