Hi,
how to scroll BG..??
.i am using
body.ApplyForce(new b2Vec2(20,0), body.GetWorldCenter()) ...to control player
and m_sprite.x -= scrollX to make whole BG move...
but since both are working simultaneuosly..its looking jerky...i want ApplyForce effect to BG
Easiest way is to create a body in the world to represent the camera, and turn off collisions, gravity etc. Then instead of subtracting scrollX/Y, you get that bodies position each frame and use that. Then you can control the body the usual way.
Hi thanks ..but still i am confused
package TestBed{
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
import Main;
import General.Input;
import flash.utils.getTimer;
import flash.display.*;
import flash.events.*
public class Test {
public var scrollX=5
public function Test() {
m_sprite = Main.m_sprite;
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(1000.0, 1000.0);
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
dbgDraw.m_sprite = m_sprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.5;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
m_world.SetDebugDraw(dbgDraw);
var wallSd:b2PolygonDef = new b2PolygonDef();
var wallBd:b2BodyDef = new b2BodyDef();
var wallB:b2Body;
wallBd.position.Set(26,13);
wallSd.SetAsBox(3, .1);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
//////////ship creation
var shipShapeDef:b2PolygonDef = new b2PolygonDef();
shipShapeDef.vertexCount=3;
shipShapeDef.vertices[0].Set(6.0,2.5);
shipShapeDef.vertices[1].Set(0.0,5.0);
shipShapeDef.vertices[2].Set(0.0,0.0);
shipShapeDef.friction = 0.5;
shipShapeDef.restitution = 0.3;
var shipBodyDef:b2BodyDef = new b2BodyDef();
shipBodyDef.userData = new Ship();
shipBodyDef.userData.width = 30
shipBodyDef.userData.height=30
shipBodyDef.position.Set(7, 7);
var shipBody = m_world.CreateBody(shipBodyDef);
var myShape:b2Shape=shipBody.CreateShape(shipShapeDef);
////////////
}
public function Update():void {
if (Input.isKeyPressed(39)){
/// what to write here to make BG move ( here BG is having wallB as a object)
}
if (Input.isKeyPressed(37)){
//m_sprite.x += scrollX
}
}