I am using box2D flash library in my game. In the world there are some static, kinematic and dynamic bodies, all stored into Global.scrolB2 array, created in same world. The main character of game is box2d body too, moving left and right with linear velocity and jumping up with impulse. I made world scrolling:
function MoveWorld(l){
trace(l);
if (Math.abs(l)>0.3){
t = Global.scrolB2;
var pos:b2Vec2;
for (i = 0; i<t.length; i++)
{
pos = t[i].GetPosition();
pos.Set(pos.x+l/Global.PRate, pos.y);
t[i].SetPosition(pos);
}
t = Global.scrol;
for (i = 0; i<t.length; i++)
{
t[i].x += l;
}
pos = Body.GetPosition();
pos.Set(pos.x+l/Global.PRate, pos.y);
Body.SetPosition(pos);
}
}
The idea is that, on each frame, the script counts the distance between the very first main body position and changed position. After it script applies MoveWorld function. There are some problems, which i cant fix in it, can you help me? Errors:
1)With this function character sometimes gets stuck when its moving around, jumping launches the character up in air and left.
Jump and movement:
var vel = Body.GetLinearVelocity();
vel.x = -2;
Body.SetLinearVelocity(vel);
b2dpos = Body.GetPosition();
tempvec = Body.GetPosition();
var tempi = -1 * Body.GetMass() *
Math.sqrt(2*10*5000/(Global.PRate*Global.PRate));
tempvec.Set(0, tempi);
Body.ApplyImpulse(tempvec, b2dpos);
2)The Box2D bodies and images(which are stored in Global.scrol) are moving with different speed(Global.PRate is convertion rate from pixels to meters, which are used with Box2D). This is probably rounding problem(after l/Global.PRate part, PRate is 40, 0<=l<=2);