Cocos creator实现《滑雪趣挑战》滑雪小游戏资源及代码
最近在学习Cocos Creator,作为新手,刚刚开始学习Cocos Creator,上线了两个微信小游戏,刚刚入门,这里记录一下《滑雪趣挑战》实现及上线过程的过程。

](https://img-blog.csdnimg.cn/9f2f8c7f066a41a6807841ffe90673e9.jpeg#pic_center)

https://wxaurl.cn/jYSrvkJBQYh

一 安装CocosDashBoard

这里就不介绍如何安装了

本案例使用的是编译器版本为2.4.10

二 新建2D项目SkiMan

2.1、管理项目目录
在资源管理器中新建文件夹

anim 动画
preb 预制体
res 图片、语音资源
scene 场景
scripts 脚本资源
将资源文件拖到res目录下

2.1、搭建界面
该案例中的场景界面为主界面main_scene、游戏界面game_scene.fire、关卡界面

三 关键代码

在该小游戏中障碍物的小树和石头需要向上移动,滑雪小人左右移动躲避小树和石头,得到星星即可得分。

3.1、创建障碍物

创建小树预制体,增加碰撞检测

创建石头预制体,增加碰撞检测

定时器移动小树与石头

createObstacle() {if (this.isStoped || !this.isStartPlay) {return;}// 随机2~4秒;var random = Math.random() * 100;let type = Math.floor(random);this.createElement(type);}createElement(type) {let bNum = 3;if (type % bNum == 0) {var graySone = this.spawnStone();this.node.addChild(graySone);var x = -this.canvasWidth / 2.0 + Math.random() * (this.canvasWidth - graySone.width);graySone.setPosition(cc.v2(x, -(this.canvasHeight / 2 + graySone.height)));} else {var tree = this.spawnTree();this.node.addChild(tree);var x = -this.canvasWidth / 2.0 + Math.random() * (this.canvasWidth - tree.width);tree.setPosition(cc.v2(x, -(this.canvasHeight / 2 + tree.height)));}}startCreateObstacle() {for (let index = 0; index < 1; index++) {var tree = this.spawnTree();this.node.addChild(tree);var x = -this.canvasWidth / 2.0 + Math.random() * (this.canvasWidth - tree.width);var y = -this.canvasHeight / 2.0 + Math.random() * (this.canvasHeight / 2.0) + this.canvasHeight / 8;tree.setPosition(cc.v2(x, y));}}

之后在update中移动障碍物

update(dt) {if (this.isStoped || !this.isStartPlay) {return;}for (let index = 0; index < this.node.childrenCount; index++) {let sub = this.node.children[index];sub.y += this.moveSpeed * dt;if (sub.y >= -this.canvasHeight / 3.0 && this.isOpenMagic) {let graySt = sub.getComponent(GrayStone);if (graySt) {graySt.showOpenCubeDestory();} else {let tree = sub.getComponent(ChristmasTree);if (tree) {tree.showOpenCubeDestory();}}}if (sub.y >= this.canvasHeight / 2.0) {let graySt = sub.getComponent(GrayStone);if (graySt) {this.despawnStone(sub);} else {let tree = sub.getComponent(ChristmasTree);if (tree) {this.despawnTree(sub);} else {sub.removeFromParent();sub.destroy();}}}}}

3.2、魔法棒与磁铁道具

在游戏制作过程中新增了两个道具,
一个魔法棒道具
魔法棒道具:得到魔法棒道具可以在10s内时间内清除小树、石头障碍物

判断是否开启魔法棒,开启魔法棒后,障碍物消失

if (sub.y >= -this.canvasHeight / 3.0 && this.isOpenMagic) {let graySt = sub.getComponent(GrayStone);if (graySt) {graySt.showOpenCubeDestory();} else {let tree = sub.getComponent(ChristmasTree);if (tree) {tree.showOpenCubeDestory();}}}

魔法棒道具创建

createMagic() {if (this.isStoped || !this.isStartPlay) {return;}var magicWand = cc.instantiate(this.magicWand);this.node.addChild(magicWand);var x = -this.canvasWidth / 2.0 + Math.random() * (this.canvasWidth - magicWand.width);magicWand.setPosition(cc.v2(x, -(this.canvasHeight / 2 + magicWand.height)));}

磁铁道具
磁铁道具:得到磁铁道具可以在10s内时间内吸收掉星星哦

// 生成磁铁beginManetSchedule() {var time = 10 + Math.random() * 3 + this.currentLevelInfo['level'] / 30;this.schedule(this.createManet, time);}createManet() {if (this.isStoped || !this.isStartPlay) {return;}var magnetTool = cc.instantiate(this.magnetTool);this.node.addChild(magnetTool);var x = -this.canvasWidth / 2.0 + Math.random() * (this.canvasWidth - magnetTool.width);magnetTool.setPosition(cc.v2(x, -(this.canvasHeight / 2 + magnetTool.height)));}

之后在update中移动星星的位置到滑雪小人上,星星与小人会触发碰撞检测,即可获得得分。

update(dt) {if (this.isStoped || !this.isStartPlay) {return;}for (let index = 0; index < this.node.childrenCount; index++) {let sub = this.node.children[index];if ((this.isOpenMagnet && (sub.y >= -this.canvasHeight / 2.0 && sub.y <= this.canvasHeight / 2.0))) {// 将星星移动到目标角色的位置let aStar = sub.getComponent(Star);if (aStar) {// 只处理星星移动aStar.moveToTargetPos(this.playPos);} else {sub.y += this.moveSpeed * dt;}} else {sub.y += this.moveSpeed * dt;}if (sub.y >= this.canvasHeight / 2.0) {let aStar = sub.getComponent(Star);if (aStar == null) {this.despawnStar(sub);                    } else {sub.removeFromParent();sub.destroy();}}}}

3.3、关卡内容,设计关卡

每个关卡控制不同的移动速度。所以在关卡代码中设置Settings
settings.ts

let settings = [{                               level: 1,                   // 第1关star: 0,                     // star获得数量duration: 60,               // 倒计时时长,单位为秒sspeed: 200,                 // 倒计时时长,单位为秒slevelState: 'UNLOCKED',     // 关卡状态},{                               level: 2,                   // 关卡等级star: 0,                    // star获得数量duration: 60,               // 倒计时时长,单位为秒sspeed: 250,                 // 倒计时时长,单位为秒slevelState: 'LOCKED',       // 关卡状态},
]

关卡展示是一个layout节点

每个关卡是一个预制体,在关卡中代码中生成具体的展示layout效果

export default class LevelRoot extends cc.Component {@property(cc.Sprite)levelBG: cc.Sprite = null;@property(cc.Node)levelLayout: cc.Node = null;@property(cc.Node)closeNode: cc.Node = null;@property(cc.Prefab)levelItemPreb: cc.Prefab = null;onLoad () {this.closeNode.on(cc.Node.EventType.TOUCH_START, this.closeLevelRoot, this);this.setupLevel();}start () {}update (dt) {}setupLevel () {this.levelLayout.removeAllChildren();if (!cc.sys.localStorage.getItem('settings')) {for (let i=0; i<settings.length; i++) {let level = cc.instantiate(this.levelItemPreb);level.getComponent(LevelItem).levelSetting = settings[i];level.getComponent(LevelItem).changeLevel();this.levelLayout.addChild(level);}// 将所有关卡信息存入本地(针对首次游戏)cc.sys.localStorage.setItem('settings', JSON.stringify(settings));}else {// 如果玩家已经玩过,则从本地存储中获取关卡配置信息let localSettings = JSON.parse(cc.sys.localStorage.getItem('settings'));for (let i=0; i<localSettings.length; i++) {let level = cc.instantiate(this.levelItemPreb);level.getComponent(LevelItem).levelSetting = localSettings[i];level.getComponent(LevelItem).changeLevel();this.levelLayout.addChild(level);}}}closeLevelRoot() {this.node.active = false;}
}

最后在GameManager.ts获取当前的关卡内容,更改移动速度。

四、上传微信小游戏

上线微信小游戏
在https://mp.weixin.qq.com 创建账号并登录,这里注意选择小程序的账号哦,需要绑定微信。
登录之后,在小游戏基本设置,设置一下内容

1、上线微信小游戏
小程序名称
小程序简称
小程序头像
小程序服务类目

在游戏设置中需要设置 上传小游戏自审自查文档照片,签字,日期,按手印哦。

2、Cocos Creator代码打包上传
在Cocos Creator的菜单的项目中,构建发布

选择发布平台,微信小游戏
根据游戏的横竖屏设置方向
设置appid(来自微信小程序后台)

最后点击构建,点击构建成功后。使用微信开发工具导入编译build后的项目。在微信开发工具中预览可以手机扫码看到游戏的开发版本。
上传游戏、提交版本及更新内容,上传成功后可以在微信小程序后台的版本管理中看到上传新版本。点击提交审核即可。

3、上线微信小游戏出现问题
上线微信小游戏经常出现“小游戏需具有完整的游戏玩法、不能为简单的素材堆砌”
根据这个问题,在案例中新增了关卡、游戏玩法介绍后重新发布新的版本,继续等待审核即可。暂时这么审核通过了。

参考
https://blog.csdn.net/gloryFlow/article/details/130690146

五、游戏效果

最后大致看下在微信小游戏上的效果

Cocos creator实现《滑雪趣挑战》滑雪小游戏资源及代码相关推荐

  1. Cocos creator实现飞机大战空中大战《战击长空》小游戏资源及代码

    Cocos creator实现飞机大战空中大战小游戏资源及代码 最近在学习Cocos Creator,作为新手,刚刚开始学习Cocos Creator,刚刚入门,这里记录一下飞机大战小游戏实现.搜索微 ...

  2. Cocos creator小游戏实现套牛小游戏资源及代码

    Cocos creator实现套牛小游戏资源及代码 一 安装CocosDashBoard 二 新建2D项目RunCow 1.管理项目目录 2.搭建界面 三 上线微信小游戏 1.上线微信小游戏 2.Co ...

  3. Cocos Creator开发技术研究:微信小游戏中音效中断问题处理

    转载自麒麟子博客:https://qilinzi.blog.csdn.net/article/details/89488323 音效可谓是一个小游戏的灵魂了. 某些玩法离开了音效更是不可能,比如别踩白 ...

  4. 初识cocos creator,做一款H5小游戏

    分享内容预览 小游戏体验. cocos creator 前世今生. 基本开发环境的了解. 小游戏场景制作相关知识. 基础语法讲析. sunlands-cow demo的讲解. 构建,发布.(h5, 微 ...

  5. 【Cocos Creator 实战】06 - 如何给拼图游戏添加计时器

    文章目录 概览 主要内容 项目资源 开搞 项目结构 字体 如何控制节点的显示&隐藏 如何设置节点的相对位置 & 自动大小 & 对齐策略 如何防止节点的点击穿透 如何倒计时 总结 ...

  6. python小游戏-16行代码实现3D撞球小游戏!-源码下载

    python小游戏-16行代码实现3D撞球小游戏!-源码下载 所属网站分类: 资源下载 > python小游戏 作者:搞笑 链接: http://www.pythonheidong.com/bl ...

  7. java代码实现打气球游戏_关于javascript和css3开发打气球小游戏的完整代码

    这篇文章主要介绍了关于javascript和css3开发打气球小游戏的完整代码,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 这是一个简单但是印象深刻的小游戏,打气球小游戏的实现代码, ...

  8. android h5游戏图片不缓存,H5小游戏资源缓存方法与流程

    本发明涉及H5资源缓存领域,尤其涉及H5小游戏资源缓存方法. 背景技术: 随着移动互联网的发展和手机硬件性能的不断提升,H5小游戏这种不需要下载安装即可使用的全新游戏应用得到了爆发式发展.这种用完即走 ...

  9. unity 转微信小游戏 资源优化

    资源优化 可通过转换工具配套提供的资源优化工具,将游戏内纹理资源针对webgl导出做优化. 工具入口 菜单栏-微信小游戏-资源优化工具 工具介绍 Texture 区域1: 选中目录,以设定规则扫描目录 ...

最新文章

  1. php reader oleread,PHP 实用技巧集锦
  2. matlab randint函数
  3. C++ edmond karp和ford fulkerson求最大流算法(附完整源码)
  4. 问题 G: 最小的回文数
  5. 4G(LTE)是如何实现智慧农业物联网的?
  6. 揭秘2019双11背后的云网络 – 双11网络架构和洛神系统
  7. [置顶] 怎么对待重复的代码
  8. AtCoder Regular Contest 071
  9. GMA Round 1 三视图
  10. MySQL获取汉字的拼音首字母
  11. 路由器概述(作用功能、工作过程、内部组成【RAM、ROM区别】、接口)
  12. 换手机了,换用三星S559
  13. Ubuntu 16.04 LTS 初体验
  14. 好养活的“狗剩儿”和“胖丫儿”。龙芯电脑测评!
  15. 4.7W防削顶单声道D类音频功率放大器HT6872介绍
  16. android 定位蓝牙,蓝牙如何定位,简易蓝牙定位系统的实现方法
  17. 中南大学杰出校友_杰出客户服务的10个要点。
  18. 第2章第27节:英文排版技巧:大间距与大行距的应用 [PowerPoint精美幻灯片实战教程]
  19. android dashboard 开源,android dashboard布局
  20. Qt c++5.15 mingw 64位编译 ---ricky.chu

热门文章

  1. 7-3 藏头诗 (15分)
  2. 互联网产品运营的组织架构
  3. 密室NPC的演技吊打流量明星
  4. JustAuth新版发布,建议升级到最新版【v1.9.4】
  5. JustAuth1.9.0版本正式来袭!Teambition、人人、Pinterest、Stack Overflow等尽收眼底!
  6. Worthington核糖核酸测定详细攻略
  7. 陈超-商业思维投资之道
  8. 【英语:基础高阶_经典外刊阅读】L2.阅读理解必备技能—词义推测
  9. Kafka 安装与配置
  10. LeetCode每日一题——380. O(1) 时间插入、删除和获取随机元素