What is wrong on my HelloWorld::update() method? FPS is decreasing. Every hint is welcome.

What is wrong on my HelloWorld::update() method? FPS is decreasing. Every hint is welcome.

I got also messsages like below:
cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from to .

void HelloWorld::update(float dt)
{
        CCSprite* wall[200];
        CCSprite* way[200];

        CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("tiles_standard.png");
        CCSpriteBatchNode*  playerBN = CCSpriteBatchNode::create("characters1.png");

        int xxx = 0;
        int yyy = 0;
        for (int x = 0; x < 13; x++)
        {
            for (int y = 0; y < 13; y++)
            {
                wall[x*sizeMaze+y] = CCSprite::createWithTexture(batchNode->getTexture(), 
                    CCRectMake(1*32, 0*32, 32,32));
                way[x*sizeMaze+y] = CCSprite::createWithTexture(batchNode->getTexture(), 
                    CCRectMake(9*32, 9*32, 32,32));

                if (maze[xxx][yyy] == WALL)
                {
                    wall[x*sizeMaze+y]->setPosition(ccp(16+y*32, 464-x*32));
                    batchNode->addChild(wall[x*sizeMaze+y], 0);
                } 
                else
                {
                    way[x*sizeMaze+y]->setPosition(ccp(16+y*32, 464-x*32));
                    batchNode->addChild(way[x*sizeMaze+y], 0);
                }   
            }
        }
        this->addChild(batchNode);
}

Do you really need to create 338 new sprites on every call to update?

If you schedule update for 1/60 then you are trying to create 20,280 sprites per second.

I’m not surprised you are getting a FPS drop.

You should only do this once in your init code, not every time in update.

You also don’t seem to be changing xxx or yyy in the loop so if (maze[xxx][yyy] == WALL) will always be if (maze[0][0] == WALL)

Adam Reed wrote:

Hello Adam,
Thanks for answering.

Do you really need to create 338 new sprites on every call to update?
it is a tile based game with an 13x13 field.
Each field need an sprite but currently I need only 2 different ‘textures’.
So how can I solve it to use only 2 sprites which has to draw on 13x13 positions?

>

If you schedule update for 1/60 then you are trying to create 20,280 sprites per second.
>
I’m not surprised you are getting a FPS drop.
>
You should only do this once in your init code, not every time in update.
You mean: I should create the sprites on the init code?
and only update (draw) it on the update() methode?
Maybe you know an example source?

>

You also don’t seem to be changing xxx or yyy in the loop so if (maze[xxx][yyy] == WALL) will always be if (maze[0][0] == WALL)
I know: I removed some sources which blow up the ‘example’ code here.
On the real source xxx and yy is not always 0,0.

Its fine to create a 13x13 sprite field using individual sprites as they will share texture(s).
You just need to only create them once (init or some other setup code).

It does not look like you need an array of CCSprite.
And it looks like you are only adding one of the sprites in each loop.
Why do you need to create both and then only add 1 to the batch node?
You should just be able to do this in your for loop:-

if (maze[xxx][yyy] == WALL)  //I'm not sure what you are doing here as I don't know what WALL and maze[][] is
{
  CCSprite* wall = CCSprite::createWithTexture(batchNode->getTexture(), CCRectMake(1*32, 0*32, 32,32));
  wall->setPosition(ccp(16+y*32, 464-x*32));
  batchNode->addChild(wall, 0);
} 
else
{
  CCSprite* way = CCSprite::createWithTexture(batchNode->getTexture(), CCRectMake(9*32, 9*32, 32,32));
  way->setPosition(ccp(16+y*32, 464-x*32));
  batchNode->addChild(way, 0);
}

What do you need to update in the update method?
If you want to move all the sprites like a scrolling effect then you might be better off just moving the layer around.#

Adam Reed wrote:

Its fine to create a 13x13 sprite field using individual sprites as they will share texture(s).
You just need to only create them once (init or some other setup code).
Yes you are right I can create the CCSpriteBatchNode on the init section.

It does not look like you need an array of CCSprite.
That’s correct. And I understand the batchsprite little better now.

And it looks like you are only adding one of the sprites in each loop.
Why do you need to create both and then only add 1 to the batch node?
You should just be able to do this in your for loop:-
Good point! You are right. I will change it.

What do you need to update in the update method?
Game logic and all animated grafic stuff. Right?

If you want to move all the sprites like a scrolling effect then you might be better off just moving the layer around.#
Thanks this is a good idea.
I will change the ‘update() methode and make a short information about the ’FPS’ tonight again.

Yes you are right I can create the CCSpriteBatchNode on the init section.

Not just the CCSpriteBatchNode but all the sprites can/should be added in the init.

If you do need an array of sprites that you can access later (rather than FOREACH node->getChildren() loop)
You would create your CCSprite* wall[200] in your class and populate in init.

In the Update, your game logic would just need to setPosition on the sprites you need to move (if any).

Of course you could keep a seperate reference to your player sprite so you can move that one around.

I have changed something.
I got FPS 60 again but there is no longer an update of the Sprites.
(All on the HelloWorld::init() method).

I think anything of Cocos2Dx is missunderstanding by me.

What are you doing now in your update function?
You should be updating the sprites in there.

Your code from before was not updating anything so any changes would have come from recreating the sprites.

@Adam,

Can I send you an mail with the sources?

Thanks
P.E.

//Edit:
I think I understand the layer behavior. I will try it by myself first.