找到你要的答案

Q:cocos2d-x use create() method in update method

Q:create() cocos2d-x使用方法更新方法

i use cocos2d-x for my game. i have some question about cocos2d-x create method.

i always use create() method to create new Animation , Animate , RepeatForever Object when the game character needs to change the animation in Update Method

but i think this is not good because if i create new Animation, Animate , RepeatForever Object to change my game character's anitation like below (below code is just a part of Update method )

auto anim = cocos2d::Animation::create();
anim->setDelayPerUnit(0.3);

anim->addSpriteFrameWithFile("./Hero/walk1_0.png");

anim->addSpriteFrameWithFile("./Hero/walk1_1.png");


anim->addSpriteFrameWithFile("./Hero/walk1_2.png");

anim->addSpriteFrameWithFile("./Hero/walk1_3.png");

auto animation = cocos2d::Animate::create(anim);

m_pBasicSprite->runAction(animation);

when the game character change the animation frequently, then it will cause create too many cocos object. so how should i gonna do?

p.s

i use class memeber variable for save that Object's instead of create new cocos animation object for each loop in update method, but it cause error the error said "expression _referenceCount > 0"

p.s2

i'm sorry about my terrible english...

i use cocos2d-x for my game. i have some question about cocos2d-x create method.

我总是用create()方法创建新的动画,动画,repeatforever对象当游戏角色需要在更新方法的动画变化

but i think this is not good because if i create new Animation, Animate , RepeatForever Object to change my game character's anitation like below (below code is just a part of Update method )

auto anim = cocos2d::Animation::create();
anim->setDelayPerUnit(0.3);

anim->addSpriteFrameWithFile("./Hero/walk1_0.png");

anim->addSpriteFrameWithFile("./Hero/walk1_1.png");


anim->addSpriteFrameWithFile("./Hero/walk1_2.png");

anim->addSpriteFrameWithFile("./Hero/walk1_3.png");

auto animation = cocos2d::Animate::create(anim);

m_pBasicSprite->runAction(animation);

当游戏角色动画改变频繁,那么它会造成太多的椰子树对象。那我该怎么办?

附笔

i use class memeber variable for save that Object's instead of create new cocos animation object for each loop in update method, but it cause error the error said "expression _referenceCount > 0"

附笔2

我对我糟糕的英语很抱歉…

answer1: 回答1:

Inside ClassName.h:

CCAction* _aniRun;
void createAnimation();

Inside ClassName.cpp:

// create a function
void ClassName::createAnimation()
{
    // run ani
    CCAnimation* animation=CCAnimation::create();
    animation->setDelayPerUnit(0.05f);
    for (int i=0; i<1; i++)
    {
        char str[50];
        sprintf(str,"game/bird/run%d.png", i);
        animation->addSpriteFrameWithFileName(str);
    }
    _aniRun = CCRepeatForever::create(CCAnimate::create(animation));
    _aniRun->retain();
}

Now wherever you want to play animation just call

player->runAction(aniRun);    

Don't forget to release _aniRun inside destructor:

CC_SAFE_RELEASE(_aniRun);

内部类名。H:

CCAction* _aniRun;
void createAnimation();

在ClassName.cpp:

// create a function
void ClassName::createAnimation()
{
    // run ani
    CCAnimation* animation=CCAnimation::create();
    animation->setDelayPerUnit(0.05f);
    for (int i=0; i<1; i++)
    {
        char str[50];
        sprintf(str,"game/bird/run%d.png", i);
        animation->addSpriteFrameWithFileName(str);
    }
    _aniRun = CCRepeatForever::create(CCAnimate::create(animation));
    _aniRun->retain();
}

现在无论你想玩动画只是打电话

player->runAction(aniRun);    

别忘了_anirun析构函数释放内:

CC_SAFE_RELEASE(_aniRun);
answer2: 回答2:

I am also new for cocos2d-x .i do not have full command over it .. i have basic idea about retain() and release().

in cocos 2dx , there is a AutoreleasePool -- which takes care of referancecount , when you call object->autorelease() , this object will be added to this autoreleasepool. this pool will help you to ratain the object for the lifetime of current frame.

There are 4 functions of Ref, the meanings are

retain: add reference count by 1

release: minus reference count by 1

autorelease: put the object in to AutoReleasePool, which means that cocos2d-x will invoke release automatically at the end of this frame

getReferenceCount: as the meaning of the function, it will retain the reference count of this object

cocos2dx has an autorelease pool which drains the objects which have retain count=0 which is a variable to keep in check the scope of the cocos2dx object.

Now when you create new object using the create method it is already added to the autorelease pool and you don't need to release it or delete it anywhere .

But when you create new object using 'new' you definitely need to release it in its destructor or after its use is over.

when your object is added to the autorelease pool but you need it somewhere else you could just retain it , this increments its retain count by one and then you have to manually release it after its use is over.

Whenever you add child your object it is retained automatically but you don't need to release it rather you remove it from the parent.

There are simple rules with retain() and autorelease():

  1. everything I create with new are mine and I’m responsible for deleting them from memory

  2. everything returned using a create()-method is autoreleased and will be released when the CURRENT METHOD ENDS. If the retainCount is zero, they will be purged from memory. When I need them in an instance variable I have to retain them.

  3. everything I retain I’ll have to release this from memory.

See this retain/release thing as a reference counter. If the counter is down to zero the object will be deleted.

我也是新cocos2d-x。我没有完全掌握它。我对retain()和release()基本思想。

in cocos 2dx , there is a AutoreleasePool -- which takes care of referancecount , when you call object->autorelease() , this object will be added to this autoreleasepool. this pool will help you to ratain the object for the lifetime of current frame.

有编号4的功能,意义

retain: add reference count by 1

release: minus reference count by 1

autorelease: put the object in to AutoReleasePool, which means that cocos2d-x will invoke release automatically at the end of this frame

getReferenceCount: as the meaning of the function, it will retain the reference count of this object

cocos2dx的自动释放池的排水,保持数= 0这是一个变量保持在检查的cocos2dx对象范围对象。

现在当你创建新的对象使用Create方法已经添加到自动释放池,你不需要去释放它或删除它的地方。

但是当你创建新的对象使用的'新'你肯定需要释放它的析构函数或使用后结束。

当你的对象添加到自动释放池,但是你需要在其他地方你可以保留它,这使其保留计数加一,然后你必须手动释放它使用后结束。

当您添加子对象时,它会自动保留,但不需要释放子对象,而是将其从父对象移除。

有retain()和autorelease()简单规则:

  1. 我用新创建的都是我的,我负责从内存中删除它们

  2. 一切又恢复使用create()方法是自动释放,将被释放时,当前的方法结束。如果retaincount是零,他们将被从内存中清除。当我在实例变量中需要它们时,我必须保留它们。

  3. 我所保留的一切都将从记忆中释放。

看到这个保留/释放的东西作为参考计数器。如果计数器为零,则对象将被删除。

cocos2d-x