Game is hangs and slow down

Game is hangs and slow down
0.0 0

#1

i want to generate ball like fire up game .

i generate 10 ball in every 5 interval of update method . but game is not working properly it hang .@slackmoehrle


#2

Make sure you are removing balls after done with it.
You can use apply RemoveSelf runaction.
If you are using vectors then clear them also.


#3

yes i do this but
if i create 100 sprite in every interval of update then hang after 10 to 15 sec.


#4

Tell us more. Cocos2d-x version? Are you testing on hardware or emulator? What hardware? Show us code.

How can we help without any of this info?


#5

cocos2dx 3.16
testing on real device : ipad mini ,os version is 11.2.3

void PlayArea::update(float dt)
{
for(int i=0;i<10;i++)
{
Sprite *sp=Sprite::create(“ball.png”);
sp->setPosition(location);// location is touch move location
this->addChild(sp,10,tag);
tag++;
}
for (int i=0; i<tag; i++)
{
if(this->getChildByTag(i))
{
if(this->getChildByTag(i)->getNumberOfRunningActions()==0)
{
this->getChildByTag(i)->runAction(Sequence::create(MoveBy::create(0.0001, Vec2(0,30)),NULL));
}
if(this->getChildByTag(i) && this->getChildByTag(i)->getPositionY()>1024)
{
this->getChildByTag(i)->removeFromParent();
}
}
}
}


#6

Use object pooling, create around 100 objects and store in some data structure like vector(or queue, stack) at the start. In the update method take objects from the vector, enable it and use it, once done disable it and put back in the vector.

Creating and destroying objects in update method is not the very efficient way of doing this.


#7

First of all you should consider object pooling as pointed out by lazydevx. Moreover you can optimize your code a lot by not using getChildByTag you don’t need it. you can simply do something like this.

for(int i=0;i<10;i++)
{
Sprite *sp=Sprite::create(“ball.png”);
sp->setPosition(location);// location is touch move location
this->addChild(sp,10);
sp->runAction(Sequence::create(MoveBy::create(0.0001, Vec2(0,30)), RemoveSelf::create() ,NULL)); //It will remove itself after completing action. 
}

#8

@lazydevx @tink3r_t have the answers.


#9

sp->runAction(Sequence::create(MoveBy::create(0.0001, Vec2(0,30)), RemoveSelf::create() ,NULL)); //It will remove itself after completing action.

it’s not working because it override previous action


#10

Which action? you just created it. Can you describe what exactly is the problem?


#11

i use MoveBy so ball is move by 30 px in every time interval and its move in uo diraction.and in your code ball is move only once

this is my code
if(this->getChildByTag(i))
{
if(this->getChildByTag(i)->getNumberOfRunningActions()==0)
{
this->getChildByTag(i)->runAction(Sequence::create(MoveBy::create(moveSpeed, Vec2(0,30)),NULL));
}
}


#12

i want to do like this


#13

Instead of adding movemnet again and again. You can use MoveTo() for this purpose.

sp->runAction(Sequence::create(MoveTo::create(0.0001, Vec2(0,1024)), RemoveSelf::create() ,NULL));

#14

if i move ball to 1024 px in 0.0001 time ball is not moving . you just wire this code in update you will relies the problem


#15

You might want to increase the time to check it correctly.

sp->runAction(Sequence::create(MoveTo::create(1, Vec2(0,1024)), RemoveSelf::create() ,NULL));

#16

if i set time to 1 then its work fine but i want to move in very high speed like this video please check this video https://www.youtube.com/watch?v=fvNFQUKGAVY


#17

Simply reduce the time and see the effect. Actually I don’t have access to a device with cocos installed right now. And i suggest you to go through this tutorial to increase your understanding about actions. And this tutorial for a complete overview.


#18

actually every thing is working file like this video https://www.youtube.com/watch?v=fvNFQUKGAVY but its not work proper when i create more then 10 sprite in update at same time


#19

Well that’s solution is object pooling. Unfortunately I have no tutorial in cocos2dx for that but if you want to know what is object pooling go through this link. I will probably make a class for generic object pooling in cocos2dx when I get time.


#20

thank you i will try object pooling method .