Search code examples
cocos2d-xchipmunkcocos2d-js

physics body moves in browser but not on mac or ios


i was trying to move the physics body by changing its coordinate in scheduler and i used this code. if i run this code in browser it will work but after js binding it doesn't work on mac or ios. Physics body doesn't move at all on these devices

init: function{
 var mass = 1;    
var width = 1, height = 1;
this.playerBody = new cp.Body(mass , cp.momentForBox(mass, width, height));
this.space.addBody(this.playerBody);

this.schedule(this.move);
},
move: function(dt){
this.space.step(dt);
this.playerBody.getPos().x += 2 * dt;
this.playerBody.getPos().y += 2 * dt;
}

Solution

  • after adding few lines my player started to move in mac

    init: function{
    var mass = 1;    
    var width = 1, height = 1;
    this.playerBody = new cp.Body(mass , cp.momentForBox(mass, width, height));
    this.space.addBody(this.playerBody);
    
    this.schedule(this.move);
    },
    move: function(dt){
    this.space.step(dt);
    var a = this.playerBody.local2World(this.playerBody.p);
    a.y += 2 * dt;
    a.x += 2 * dt ;
    a = this.playerBody.world2Local(a);
    this.playerBody.p = a;
    }
    

    but i don't have explanation of this code