Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();  
share|improve this question
    
You asked this twice on purpose? –  bummzack Nov 27 '11 at 22:30
    
possible duplicate of AS3: limit objects to stage width? –  bummzack Nov 27 '11 at 22:30
    
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. –  bummzack Nov 27 '11 at 22:40
    
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. –  bummzack Nov 27 '11 at 22:54

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Is on the main timeline, the first frame to be precise. –  Gabriel Meono Nov 27 '11 at 21:53
    
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? –  annonymously Nov 27 '11 at 22:11
    
I'm not sure, this is the project: gabrielmeono.com/download/BOX2D.zip –  Gabriel Meono Nov 27 '11 at 22:32
    
Your problem is not the stage becuase I can assign a variable to stagewidth –  annonymously Nov 27 '11 at 23:31
    
I edited my answer now that I know what's wrong –  annonymously Nov 28 '11 at 0:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.