CocosCocos Creatorcocos creator 基础教程

cocoscreator 碰撞组件

2018-08-19  本文已影响7人  lazyTai
cocos creator 碰撞组件
    编辑碰撞组件
        多边形碰撞组件
        圆形碰撞组件
        矩形碰撞组件
    碰撞分组管理
    碰撞系统脚本控制
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        manager.enabledDebugDraw = true;
        碰撞系统回调
            onCollisionEnter
            onCollisionStay
            onCollisionExit
    demo
        点击测试
        1.添加碰撞组件
        2.js控制点击事件
        3.点击事件的回调中,判断是否碰撞
// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        canvas: {
            default: null,
            type: cc.Node,
        },
        testNode: {
            default: null,
            type: cc.PolygonCollider
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        manager.enabledDebugDraw = true;
    },

    start() {
        var self = this;
        /*    demo
           点击测试
           1.添加碰撞组件
           2.js控制点击事件
           3.点击事件的回调中,判断是否碰撞 */
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            onTouchBegan: (touch, event) => {
                var tochLoc = touch.getLocation();
                var pointers = self.testNode.world.points
                if (cc.Intersection.pointInPolygon(tochLoc, pointers)) {
                    cc.log("hit")
                } else {
                    cc.log("not hit")
                }
                return true;
            }
        }, this.canvas)
    },

    // update (dt) {},
});

上一篇下一篇

猜你喜欢

热点阅读