0
\$\begingroup\$

I know BOX2D uses meters, and AS3 uses pixels.

I'm trying to create objects which are limited to the stageWidth.

If I do this variable:

for (var i:int = 0; i<(stage.stageWidth); i++){...} 

The animation will freeze, and this output appears:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Box2D.Collision::b2BroadPhase/CreateProxy()
at Box2D.Collision.Shapes::b2Shape/CreateProxy()
at Box2D.Dynamics::b2Body/CreateShape()
at com.actionsnippet.qbox.objects::CircleObject/build()
at com.actionsnippet.qbox::QuickObject/init()
at com.actionsnippet.qbox::QuickObject()
at com.actionsnippet.qbox.objects::CircleObject()
at com.actionsnippet.qbox::QuickBox2D/create()
at com.actionsnippet.qbox::QuickBox2D/addCircle()
at BOX2D_Test_Tutorial_fla::MainTimeline/frame1()

Does anyone know how to fix this?

Full Code:

    [SWF(width = 350, height = 600, frameRate = 60)]  
import com.actionsnippet.qbox.*;  
var sim:QuickBox2D = new QuickBox2D(this);  
sim.createStageWalls();  
// make a heavy circle  
sim.addCircle({x:3, y:3, radius:0.4, density:1});  
// create a few platforms  

// make pins 
for (var i:int = 0; i<(stage.stageWidth); i++){  
//End
     sim.addCircle({x:1 + i * 1.5, y:18, radius:0.1, density:0});
     sim.addCircle({x:2 + i * 1.5, y:17, radius:0.1, density:0});
     sim.addCircle({x:1 + i * 1.5, y:16, radius:0.1, density:0});
     sim.addCircle({x:2 + i * 1.5, y:15, radius:0.1, density:0});

//Mid end

      sim.addCircle({x:0 + i * 2, y:14, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:13, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:12, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:11, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:10, radius:0.1, density:0});

}  
sim.start();  
sim.mouseDrag();  
\$\endgroup\$
4
  • \$\begingroup\$ You asked this twice on purpose? \$\endgroup\$
    – bummzack
    Commented Nov 27, 2011 at 22:30
  • \$\begingroup\$ possible duplicate of AS3: limit objects to stage width? \$\endgroup\$
    – bummzack
    Commented Nov 27, 2011 at 22:30
  • \$\begingroup\$ I updated my answer in the other question. I think your problem is because you're accessing stage at a point of time where it's not initialized, eg. null. Therefore you get this error. \$\endgroup\$
    – bummzack
    Commented Nov 27, 2011 at 22:40
  • \$\begingroup\$ I had a look at your flash file. The issue isn't the stage. That works fine. I don't know exactly what the problem is, because the error occurs during the creation of the objects. It could be, because you're adding a huge amount of objects. The stageWidth is 350 and you're creating 350 * 9 objects in that loop. Most of these objects will be outside your physics world, because the stage is in pixels, while your physics-world has another unit (meters). That's probably causing the crash. \$\endgroup\$
    – bummzack
    Commented Nov 27, 2011 at 22:54

1 Answer 1

0
\$\begingroup\$

You are surpassing the limit of bodies in Box2D, at 3150 circles I can't imagine why you would need so many, is there a logic error or will you divide this number by something?

Edit:

If you want to evenly space objects across the stage you can do this:

var meterWidth = stage.stageWidth/meter;// meter can be 30 or whatever your scale is

// Adds the distance between each object to i on each iteration and stops at the width
for (var i = 0; i < meterWidth; i += meterWidth/numberOfObjects)
{
    sim.addCircle({x:i, y:18, radius:0.1, density:0});// i is the x coordinate
}

Edit:

For anyone interested, the maximum body count in Box2D can be changed in b2Settings.

\$\endgroup\$
7
  • \$\begingroup\$ Is on the main timeline, the first frame to be precise. \$\endgroup\$ Commented Nov 27, 2011 at 21:53
  • \$\begingroup\$ I just looked at your error and it seems the problem is inside the loop, do you know if it is the stage that you can't access? \$\endgroup\$ Commented Nov 27, 2011 at 22:11
  • \$\begingroup\$ I'm not sure, this is the project: gabrielmeono.com/download/BOX2D.zip \$\endgroup\$ Commented Nov 27, 2011 at 22:32
  • \$\begingroup\$ Your problem is not the stage becuase I can assign a variable to stagewidth \$\endgroup\$ Commented Nov 27, 2011 at 23:31
  • \$\begingroup\$ I edited my answer now that I know what's wrong \$\endgroup\$ Commented Nov 28, 2011 at 0:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .