sbiermanlytle / iioEngine

iio Engine: A JavaScript game engine for HTML5 Canvas
http://iioengine.com
455 stars 81 forks source link

Uncaught TypeError: Cannot read property 'pos' of null #23

Closed jamessimo closed 10 years ago

jamessimo commented 10 years ago

Hey guys, when call my function createCar inside of a box2d collision handler the engine returns the error

> Uncaught TypeError: Cannot read property 'pos' of null                   iioEngine.js:2753

This function works every where else in my code but when I try to call it it from the Box2D collision listener it seems that something in the object changes and breaks the engine. I have tried to call the function in many different ways but the same problem keeps appearing so I think its a problem with the engine. (I console.log() the position and car vars and nothing seems to change).


lvl1.prototype.createCar = function(x,y,color){
    //Box2D Defines  + iio image stuff

        //ADD CAR TO IO CANVAS AND BOX2D WORLD
    this.prepShape( bodyDef, fixDef,x,y);
}

//helper to set shape properties
lvl1.prototype.prepShape = function(bodyDef, fixDef,x,y){
       //I have console logged these and they are aways the correct number
    bodyDef.position.x = x;
    bodyDef.position.y = y;
    return  this.io.addObj(world.CreateBody(bodyDef))
            .CreateFixture(fixDef)
            .GetShape()
            .prepGraphics(this.io.b2Scale);
}

lvl1.prototype.step = function(){
        var lio = this;

         //WORKS!
        lio.createCar(-100/PTM,100/PTM,'red'));
        listener.EndContact = function(contact) {
            //BROKEN! Returns pos == null
             lio.createCar(-100/PTM,100/PTM,'red'));

    }
}
jamessimo commented 10 years ago

SO Found the problem. Its BOX2D

Box2D creates a lock when inside a collision, so when I tired to create my car (which is also a box2d object), Box2d failes to create anything and iioEngine gets NULL.

That was quite the tricky customer. The solution is to put the create car call into a setTimeout