GetBoundingBox() width and height?


#1

I have this code:

    this.initWithSpriteFrameName(spr.Wall1);
    this.setPosition(point);

Inside of a class extended from cc.Sprite.

When I call:

this.GetBoundingBox();

It gives me the x and y but width and height are zero.

I have pre-loaded the sprite sheet like this in main.js:

cc.spriteFrameCache.addSpriteFrames(res.SS_Obstacles);

What am I doing wrong? do I need to preload the texture somehow???


#2

I solved this, after thinking for a few hours. It was timing. Everything I did was right except I needed to leave a small delay in the “OnEnter” callback for my base layer before calling the code to setup the game world.

From this:

onEnter: function () {
    cc.log(this._layerName + '.' + arguments.callee.name);

    this._super();
   
    this._createWorld();
},

To:

onEnter: function () {
    cc.log(this._layerName + '.' + arguments.callee.name);

    this._super();
   
    this.scheduleOnce( function() { this._createWorld(); } , .5);
},

The code where I initialise my sprite was inside of this._createWorld. Something obviously needed time to load the sprite sheets textures and work out the sizes.