前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【中英双语教程】桌球小游戏(1)

【中英双语教程】桌球小游戏(1)

作者头像
张晓衡
发布2019-09-11 18:27:43
8110
发布2019-09-11 18:27:43
举报

How to make a simple snooker game with Cocos Creator (1)

桌球小游戏是 BigBear 老师设计、撰写的中英双语的 Cocos Creator 入门教程,面向 Cocos Creator 初学者,但不论你技术如何,相信你会在 BigBear 老师的文章中有所收获。

Shawn 昨天推送了完整的中文版本,为了让大家有一个好的学习体验,我将《Cocos Creator入门实战:桌球小游戏》一文拆分成若干篇中英双语的教程,我们一起学习游戏、学习英语(看我公众号的名字就知道Shawn的英语有多low)!

开始之前 Before Start

在开始桌球小游戏之前,我们需要对creator有一定了解以及熟悉,对js语法有一定的了解。在开始同样还是希望大家能够仔细的阅读一遍官方文档,以便理解。

Before start things with Cocos Creator to make our game, you should have a familiarity understanding with Cocos Creator and JavaScript. if there are any questions in the production, pls check those Cocos's offical document:

所涉及到的知识点参考:

  • 物理系统
  • UI系统
  • 监听和发射事件
  • 动作列表
  • 预制体Prefab
  • 图集资源

  • Physics
  • UI System
  • Listen to and launch events
  • Actions and Action Lists
  • Prefab
  • Atlas Asset

ps:本项目所使用的Cocos Creator版本为 v2.0.10

ps: this tutorial is based with Cocos Creator v2.0.10

开启物理系统 Use physics system

为了尽量达到真实的效果,我们对于桌球的运动均采用物理模拟来实现。在Cocos Creator中物理系统是默认关闭的,我们可以通过下面的代码来开启物理系统:

We use physical simulation to acheieve the movement of the balls, so the balls can move as much as reality movement. Physics system is closed by default in Cocos Creator, we can open it with those code:

代码语言:javascript
复制
let physicsManager = cc.director.getPhysicsManager();
physicsManager.enabled = true;

同时我们也可以通过设置debugDrawFlags来开启一些调试信息的显示,方便我们开发预览:

also we can set physics system to a debug mode that we can see more debug info in preview:

代码语言:javascript
复制
cc.director.getPhysicsManager().debugDrawFlags =

你可以复制上面的代码来开启物理系统,但还是建议安装一个叫做"PhysicsManager.js"的脚本,代码如下:

you can copy those code in your script to open the physics system, but usually I suggest that we can create a 'physicsManager' component to implement those setting features. Like:

代码语言:javascript
复制
/**
 * physics manager component script
 */
 
cc.Class({
    extends: cc.Component,

    properties: {
       active: {
           default: true,
           tooltip: 'open physics system on component enabled',
       },
       aabb:{
           default: true,
           tooltip: 'drawing AABB rect debug info',
       },
       pair: {
           default: true,
       },
       centerOfMass: {
           default: true,
           tooltip: 'drawing collider's center of mass'
       },
       joint: {
           default: true,
           tooltip: 'drawing joint info between colliders'
       },
       shape: {
           default: true,
           tooltip: 'drawing shape info of colliders'
       },
       gravity: {
           default: cc.v2(0,-960),
           tooltip: 'the physics worl gravity, default is -960 y-axis'
       }
    },

    onEnable() {
        //open or close physics system
        let physicsManager = cc.director.getPhysicsManager();
        if (physicsManager.enabled && this.active) {
            cc.warn('The physical system is enabled!');
        }
        physicsManager.enabled = this.active;

        if (!this.active) {
            return;
        }
        //set gravity value
        physicsManager.gravity = this.gravity;

        //set physics debug flag info
        let drawBits = cc.PhysicsManager.DrawBits;
        if (CC_PREVIEW) {
            physicsManager.debugDrawFlags =
            (this.aabb && drawBits.e_aabbBit) |
            (this.pair && drawBits.e_pairBit) |
            (this.centerOfMass && drawBits.e_centerOfMassBit) |
            (this.joint && drawBits.e_jointBit) |
            (this.shape && drawBits.e_shapeBit);
        } else {
            physicsManager.debugDrawFlags = 0;
        }
    },
    
    onDisable() {
        //close physics system when this component is disabled
        let physicsManager = cc.director.getPhysicsManager();
        physicsManager.debugDrawFlags = 0;
        physicsManager.enabled = false;
    }
});

编写完脚本后,我们将这个组件挂载到Canvas节点下,在属性面板中我们可以看到:

when you completed this script, you can add it to 'Canvas' node, then you can see its proprty setting panel like:

可视化的编辑,非常的方便。勾上Active开启物理系统,这里调试开关我们开启Shape即可,由于我们这个桌游游戏demo是一个俯视的视角,因此Gravity重力我们设置为(0,0),让小球不会受到重力的影响向下坠去。

make sure that you had set gravity to 0,0 and opened physics system. for a good preview, selected Shape option. why we set the gravity to 0,0 ? because our game is a top view game, if we use defalut gavity 0,-960 , then the balls will fall down influence by gravity.we do not want it happend, so we use 0,0

<end>

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Creator星球游戏开发社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • How to make a simple snooker game with Cocos Creator (1)
    • 开始之前 Before Start
      • 开启物理系统 Use physics system
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档