-1

In the book Pro HTML5 games there is a part that shows how to create 2 bodies in Box2dWeb. For example, a circle and a rectangle, one could do the following:

 function createRectangularBody(){
     var bodyDef = new b2BodyDef;
     bodyDef.type = b2Body.b2_dynamicBody;
     bodyDef.position.x = 40/scale;
     bodyDef.position.y = 100/scale;
     var fixtureDef = new b2FixtureDef;
     fixtureDef.density = 1.0;
     fixtureDef.friction = 0.5;
     fixtureDef.restitution = 0.3;
     fixtureDef.shape = new b2PolygonShape;
     fixtureDef.shape.SetAsBox(30/scale,50/scale);
     var body = world.CreateBody(bodyDef);
     var fixture = body.CreateFixture(fixtureDef);
}

And

function createCircularBody(){
 var bodyDef = new b2BodyDef;
 bodyDef.type = b2Body.b2_dynamicBody;
 bodyDef.position.x = 130/scale;
 bodyDef.position.y = 100/scale;
 var fixtureDef = new b2FixtureDef;
 fixtureDef.density = 1.0;
 fixtureDef.friction = 0.5;
 fixtureDef.restitution = 0.7;
 fixtureDef.shape = new b2CircleShape(30/scale);
 var body = world.CreateBody(bodyDef);
 var fixture = body.CreateFixture(fixtureDef);
}

To create the bodies, it is then called both functions:

createRectangularBody();
createCircularBody();

Afterwards, it is called a method that draws the bodies, so we have

createRectangularBody();
createCircularBody();
setupDebugDraw();//Draws the bodies defined by functions above

My questions is, inside both functions we have the same objects bodyDef and fixtureDef and then proceed to change attributes of these objects. After that, we pass both objects to the world.CreateBody() method and then store then in variable body and fixture. How come there is no conflict? Are the objects created treated as different since they are in different functions? I thought that the function declared after the other would modify the objects and the whole thing wouldn't work, but that wasn't the case.

badso
  • 109
  • 1
  • 11
  • Both have different scope – Rino Raj Aug 23 '15 at 06:10
  • Can code outside the functions use the objects declared inside the function's scope? – badso Aug 23 '15 at 06:15
  • 1
    Only if they are used in nested functions (functions within the function) or passed as a parameter to another function. The latter is what happens here. If you see `var variablename=` inside a function, its scope is local to that function. In the line `var body = world.CreateBody(bodyDef);` the local var `bodyDef` is passed to the world.CreateBody function. – Me.Name Aug 23 '15 at 06:25

3 Answers3

1

Both functions are executing separately,I mean line by line execution.

Both variables are defined separately,

If you want to see some conflicts,Please declare the variable outside the function.

When you write

createRectangularBody();
createCircularBody();

together,

the second function will only execute only after the first one and have different scope.Nothing will happens,But is recommended to make separate variable name

  • Why is it recommended to make separate variable names? –  Aug 23 '15 at 06:20
  • @torazaburo So that to avoid a doubt for programmers.This is a small program,so no need to worry,If there is a large program,suppose think many programmers do separate section of a single program.Possibly there will be a chance for a doubt/mistake.Chance for happening is 30%.But remember programmers,we should maintain a 0% chance for creating man made errors/doubt.Thanks sir.. –  Aug 23 '15 at 06:27
  • I disagree. The entire point of function scope is that you **can** use variable names with no fear of conflict. If a variable in one function means the same thing as a variable in another function, then the possibility of mistakes is actually reduced by giving them the same name. –  Aug 23 '15 at 07:18
  • 1
    I accept your answer,I said that possibly there will be a chance for misunderstanding-caused by same variable name. That's it.for eg-: if there are two person-both named 'torazaburo'.one is you and other belongs right next to your house.When someone is calling outside somewhat like 'hei..torazaburooooooooooo..' Who will come first.Dont mind my example.Take it as a fun –  Aug 23 '15 at 07:30
  • the whole point is, the two torazaburo's are both inside their own houses, and cannot hear anyone yelling from outside, only from within each's own house, so there is no confusion. –  Aug 23 '15 at 09:00
1

There is no conflict when declaring same variable locally in a separate function if you are in a function that has a same global variable it will automatically get conflict. So you must aware when declaring a variable local/global.
https://msdn.microsoft.com/library/bzt2dkta(v=vs.94).aspx

0

Because the variables are not globally defined. If you define them globally (outside the two functions) then they will conflict. In your case, the scope of the variables is local to functions in which they are defined. Read about scopes of Javascript.

Check this link http://www.w3schools.com/js/js_scope.asp

Vikas Sharma
  • 745
  • 9
  • 19