Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[cocos2d-js]chipmunk例子(二)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
var TAG_SPRITE_BALL = 1;
var TAG_SPRITE_USER = 2;
var TAG_SPRITE_NPC = 3;
var PTM_RATIO = 32;
var GRABABLE_MASK_BIT = 1<<31;
var NOT_GRABABLE_MASK = ~GRABABLE_MASK_BIT;

var MainLayer = cc.Layer.extend({
    _ball:null,_man::rgb(0,_rpc::rgb(0,_leftGoal::rgb(0,_rightGoal::rgb(0,_leftPower::rgb(0,_rightPower::rgb(0,_time:0,space::rgb(0,ctor:function () {
        this._super();
        var size = cc.director.getWinSize();




        this._ball = new Ball();
        this._ball.x = size.width / 2;
        this._ball.y = size.height / this.addChild(this._ball);


        // 左方向
        var btnLeft = cc.MenuItemImage.create(
            res.btn_left_up,res.btn_left_down,function () {
                this._man.runLeft();
            },this);
        btnLeft.x = 50;

         右方向
        var btnRight = cc.MenuItemImage.create(
            res.btn_right_up,res.btn_right_down,255); line-height:1.5!important">this._man.runRight();
            },255); line-height:1.5!important">this);
        btnRight.x = 125;

         power
        var btnPower = cc.MenuItemImage.create(
            res.btn_power_up,res.btn_power_down,255); line-height:1.5!important">this._man.power();
            },255); line-height:1.5!important">this);
        btnPower.x = 250;

         jump
        var btnJump = cc.MenuItemImage.create(
            res.btn_jump_up,res.btn_jump_down,255); line-height:1.5!important">this._man.jump();
            },255); line-height:1.5!important">this);
        btnJump.x = 330;

         kick
        var btnKick = cc.MenuItemImage.create(
            res.btn_kick_up,res.btn_kick_down,255); line-height:1.5!important">this._man.kick();
            },255); line-height:1.5!important">this);
        btnKick.x = 420;

         暂停
        var btnPause = cc.MenuItemImage.create(
            "res/pause.png",res/pause2.pngthis.onStop();
            },255); line-height:1.5!important">this);
        btnPause.x = size.width - 30;
        btnPause.y = size.height - var menu = cc.Menu.create(btnLeft,btnRight,btnPower,btnJump,btnKick,btnPausE);
        menu.x = 0;
        menu.y = 20;
        this.addChild(menu,10);





         主角
        this._man = new Footballer_cn();
        this._man.flippedX = true;
        this._man.x = 150;
        this._man.y = 50;
        this._man.anchorX = 0.5;
        this._man.anchorY = 0;
        this._man,TAG_SPRITE_USER);
        this._npc = new Footballer_br();
        this._npc.x = size.width - this._npc.y = this._npc.anchorX = this._npc.anchorY = this._npc,TAG_SPRITE_NPC);

         球门
        this._leftGoal = new Goalpost(true);
        this._leftGoal.x = this._leftGoal.y = this._leftGoal.anchorX = this._leftGoal.anchorY = this._leftGoal);

        this._rightGoal = new Goalpost();
        this._rightGoal.x = size.width;
        this._rightGoal.y = this._rightGoal.anchorX = 1;
        this._rightGoal.anchorY = this._rightGoal);



        this._leftPower = new PowerProgress(size.width/2-15,size.height-20,128); line-height:1.5!important">1,128); line-height:1.5!important">0.5,this);
        this._leftPower,128); line-height:1.5!important">10);
        this._rightPower = 2+0,255); line-height:1.5!important">this._rightPower,128); line-height:1.5!important">10);



         系统计划任务,即每帧调用update函数
        this.scheduleupdate();
         自定义计划任务
        this.schedule(this.uischedule,128); line-height:1.5!important">1);

        cc.sys.dumpRoot();
        cc.sys.garbageCollect();

        this.initChipmunk();

        return true;
    },onStop:function () {
        this._bStop = !this._bStop;
        if (this._bStop == true) {
            cc.director.pause();
        }
        else {
            cc.director.resume();
        }
    },update:function (dt) {
        this.space.step(dt);
    },uischedule:function () {
        this._time++;
        this._leftPower.showPower();
        this._rightPower.showPower();
    },initChipmunk:function() {
        this.space = new cp.Space();
        var sprite =  this.createPhysicsSprite( cc.p(cc.director.getWinSize().width/2,cc.director.getWinSize().height-50) );
        this.addChild( sprite,128); line-height:1.5!important">100 );

        this.addWalls();
        this.space.gravity = cp.v(:rgb(128,-100);
    },initPhysics:function() {
        var space = this.space ;
        var staticBody = space.staticBody;
        var winSize = cc.director.getWinSize();

         Walls
        var walls = [ new cp.SegmentShape( staticBody,cp.v(0),cp.v(winSize.width,128); line-height:1.5!important">0 ),0); line-height:1.5!important"> bottom
             top
             left
            0)     right
        ];
        for( var i=0; i < walls.length; i++ ) {
            var shape = walls[i];
            shape.setElasticity(1);
            shape.setFriction(1);
            space.addStaticShape( shape );
        }

         Gravity
        space.gravity = cp.v(:rgb(128,addWalls:function() {
        var winSize = cc.director.getWinSize();
        new cp.SegmentShape( this.space.staticBody,128); line-height:1.5!important">0.8);
            shape.setFriction(0.1);
            this.space.addStaticShape( shape );
        }
    },createPhysicsSprite:function( pos ) {

        var radius = var mass = 1;

        var body = new cp.body(mass,cp.momentForCircle(mass,radius,128); line-height:1.5!important">0)));
        body.setPos( pos );
        this.space.addBody( body );
        var shape = new cp.CircleShape(body,128); line-height:1.5!important">0)); new cp.boxShape( body,48,108);
        shape.setElasticity( 1 );
        shape.setFriction( 0.1 );

        this.space.addShape( shape );

        var sprite = cc.PhysicsSprite.create(res.b_ball_01);
        sprite.setBody( body );
        return sprite;
    },setupDebugNode:function()
    {
         debug only
        this._debugNode = cc.PhysicsDebugNode.create( this.space );
        this._debugNode.visible = false ;
        this.addChild( this._debugNode );
    }
});

var MainScene = cc.Scene.extend({
    onEnter:function () {
        new GameBACkgroundLayer());
        new MainLayer());
    }
});

大佬总结

以上是大佬教程为你收集整理的[cocos2d-js]chipmunk例子(二)全部内容,希望文章能够帮你解决[cocos2d-js]chipmunk例子(二)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。