美文网首页
cocos刚体

cocos刚体

作者: RyanVan_c58b | 来源:发表于2018-06-27 11:01 被阅读0次

    function boxAddBound(box, offset, size) {

        let bound = box.addComponent(cc.PhysicsBoxCollider);

        bound.offset = offset;

        bound.size.width = size.x; bound.size.height = size.y;

    }

    cc.Class({

        extends: cc.Component,

        properties: {

            box_width: 0,

            box_heigth: 0,

        },

        onLoad () {

            // append a bound box

            let boundbox = new cc.Node("boundbox");

            // boundbox.group = "map";

            boundbox.addComponent(cc.RigidBody).type = cc.RigidBodyType.Static; // attach a rigid body to the new node.

            boundbox.enabledContactListener = true;

            // size of box

            let width = this.box_width || this.node.width;

            let height = this.box_height || this.node.height;

            // add right bound

            let offset = cc.p(width/2, 0);

            let size = cc.p(20, height);

            boxAddBound(boundbox, offset, size);

            //add left bound

            offset = cc.p(-width/2, 0);

            boxAddBound(boundbox, offset, size);

            // add top bound

            offset = cc.p(0, height/2);

            size = cc.p(width, 20);

            boxAddBound(boundbox, offset, size);

            //add bottom bound

            offset = cc.p(0, -height/2);

            boxAddBound(boundbox, offset, size);

            //

            boundbox.addComponent(cc.Graphics);

            // attach this node to the scene tree after physics things attached to the node.

            this.node.addChild(boundbox); //attach it to the script related node

        },

        update() {

            var graphics = this.node.getChildByName("boundbox").getComponent(cc.Graphics);

            graphics.clear();

            graphics.circle(100, 200, 10);

            graphics.stroke();

        },

    });

    // let collider = this.node.addComponent(cc.PhysicsCircleCollider);

            // collider.radius = this.node.width / 2;

    // // 开启碰撞检测

    //        var mngr = cc.director.getCollisionManager();

    //        mngr.enabled = true;

    //        mngr.enabledDebugDraw = true;

    //        // 开启物理系统

    //        var physics = cc.director.getPhysicsManager();

    //        physics.enabled = true;

    //        physics.debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit || cc.PhysicsManager.DrawBits.e_pairBit;

    //        physics.enabledDebugDraw = true;

    //        // mouseJoint owned by boundbox, but manage the area of parent of the boundbox

    //        let joint = this.node.addComponent(cc.MouseJoint);

    //        joint.mouseRegion = this.node;

    相关文章

      网友评论

          本文标题:cocos刚体

          本文链接:https://www.haomeiwen.com/subject/sztrcftx.html