Cocos2d-x注意隐藏的保留内存管理



如何制作一个横版格斗过关游戏Cocos2d-x 2.0.4

1
2
3
4
5
6 7 8


9

英雄:ActionSprite
{
:
,,英雄();
,,~英雄();

,,CREATE_FUNC(英雄);
}; <强>
1
2
3
4
5
6 7 8


9
10 11

12
13 14
15

16

CREATE_FUNC (__TYPE__) \
__TYPE__ *创建()\
{\
,,__TYPE__ *成衣的=__TYPE__ ();\
(成衣的,,成衣的→init ()) \
,,{\
,,,,现成的→autorelease ();\
现成的;\
,,}\
\
,,{\
现成的;\
,,,,现成的=;\
;\
,,}\
}

1
2
3
4
5
6 7 8


9
10

SimpleDPad:是cocos2d: CCSprite,是cocos2d: CCTargetedTouchDelegate
{
:
,,SimpleDPad ();
,,~ SimpleDPad ();

SimpleDPad * dPadWithFile(是cocos2d:: CCString *文件名,半径),
initWithFile(是cocos2d:: CCString *文件名,半径),
};
1
2
3
4
5
6 7 8


9
10 11

12
13 14
15


SimpleDPad * SimpleDPad:: dPadWithFile (CCString *文件名,半径)
{
,,SimpleDPad *成衣的=SimpleDPad ();
(成衣的,,成衣的→initWithFile(文件名,半径))
,,{
,,,,现成的→autorelease ();
成衣的;
,,}

,,{
成衣的;
,,,,现成的=,
,
,,}
}



1
2
3
4
5
6 7 8


9
10

GameLayer:是cocos2d: CCLayer, SimpleDPadDelegate
{
:
,,GameLayer ();
,,~ GameLayer ();

,,CREATE_FUNC (GameLayer);
,,CC_SYNTHESIZE_RETAIN(是cocos2d:: CCArray *, _robots,机器人),
};
1
2
3
4
5
6 7 8


9
10 11


GameLayer:: GameLayer ()
{
,,_robots=,
}

GameLayer:: ~ GameLayer ()
{
,,CC_SAFE_RELEASE_NULL (_robots);
}

1
2
3
4
5
6 7 8


9

ActionSprite:是cocos2d: CCSprite
{
:
,,ActionSprite ();
,,~ ActionSprite ();

,,CC_SYNTHESIZE_RETAIN(是cocos2d:: CCAction *, _attackAction AttackAction);
};
1
2
3
4
5
6 7 8


9
10 11

12
13

英雄::init ()
{
bRet=,

,,{
→setAttackAction (CCSequence:创建(CCAnimate:创建(attackAnimation) CCCallFunc::创建,callfunc_selector(英雄::闲置)),));

,,}();

bRet;
},<强>
1
2
3
4
5
6
7
8
9
10 11

12
13
14
15 16

17
18 19

20
21

22日23日24


25日26日

27日28日

CCCallFunc * CCCallFunc::创建(CCObject * pSelectorTarget SEL_CallFunc选择器)
{
,,CCCallFunc *成衣的=CCCallFunc ();

(成衣的,,成衣的→initWithTarget (pSelectorTarget)) {
,,,,现成的→m_pCallFunc=选择器;
,,,,现成的→autorelease ();
成衣的;
,,}

,,CC_SAFE_DELETE(现成的);
,
}

CCCallFunc:: initWithTarget (CCObject * pSelectorTarget) {
(pSelectorTarget)
,,{
,,,,pSelectorTarget→保留();
,,}

(m_pSelectorTarget)
,,{
,,,,m_pSelectorTarget→释放();
,,}

,,m_pSelectorTarget=pSelectorTarget;
,
}
1
2
3
4

~ CCCallFunc ()
{
,,CC_SAFE_RELEASE (m_pSelectorTarget);
}

1
2
3
4
5
6

ActionSprite:是cocos2d: CCSprite
{
:
,虚拟清理();
};,
1
2
3
4
5
6 7 8



ActionSprite::清理()
{
,,CC_SAFE_RELEASE_NULL (_idleAction);
,,CC_SAFE_RELEASE_NULL (_attackAction);
,,CC_SAFE_RELEASE_NULL (_walkAction);
,,CC_SAFE_RELEASE_NULL (_hurtAction);
,,CC_SAFE_RELEASE_NULL (_knockedOutAction);

,null

Cocos2d-x注意隐藏的保留内存管理