在CCCallFunc系列動作的4個類中,CCCallFunc調用的方法不包含參數,CCCallFuncN調用的方法包含一個CCNode*類型的參數,表示執行動作的對象。CCCallFuncND調用的方法包含兩個參數,不僅有一個節點參數,還有一個自定義參數(CCNode*與void*)。CCCallFuncO調用的方法則只包含一個CCObject*類型的參數。
實際上,CCCallFunc系列動作的後綴"N"表示Node參數,指的是執行動作的對象,"D"表示Data參數,指的是用戶自定義的數據,"O"表示對象,指的是一個用戶自定義的CCObject參數。在不同的情況下,我們可以根據不同的需求來選擇不同的CCCallFunc動作。
#include
"HelloWorldScene.h"
02.
#include
"SimpleAudioEngine.h"
03.
04.
using
namespace cocos2d;
05.
using
namespace CocosDenshion;
06.
07.
CCScene*
HelloWorld::scene()
08.
{
09.
CCScene
*scene = CCScene::create();
10.
HelloWorld
*layer = HelloWorld::create();
11.
scene->addChild(layer);
12.
return
scene;
13.
}
14.
bool
HelloWorld::init()
15.
{
16.
if
(
!CCLayer::init() )
17.
{
18.
return
false
;
19.
}
20.
CCSize
size = CCDirector::sharedDirector()->getWinSize();
21.
22.
//CCCallFunc
調用的方法不包含參
23.
CCSprite*
callFuncSp = CCSprite::create(
"Icon.png"
);
24.
callFuncSp->setPosition(ccp(size.width/
4
,
size.height*
0.5
));
25.
this
->addChild(callFuncSp);
26.
callFuncSp->runAction(CCCallFunc::create(
this
,
callfunc_selector(HelloWorld::callback1)));
27.
28.
//CCCallFuncN
調用的方法包含一個CCNode*類型的參數,表示執行動作的對象。
29.
CCSprite*
callFuncNSp = CCSprite::create(
"Icon.png"
);
30.
callFuncNSp->setPosition(ccp(size.width/
2
,
size.height*
0.5
));
31.
this
->addChild(callFuncNSp);
32.
callFuncNSp->runAction(CCCallFuncN::create(
this
,
callfuncN_selector(HelloWorld::callback2)));
33.
34.
//CCCallFuncND,回調,ND表示CCNode和Data,可以傳遞對象本身和一個void*類型參數,void*表示可以是任意類型的參數
35.
CCLabelTTF*
title = CCLabelTTF::create(
"void*
要傳的參數"
,
"Marker
Felt"
,
16
);
//自定義一個CCLabelTTF
36.
title->setPosition(ccp(size.width/
4
*
3
,
size.height*
0.6
));
37.
this
->addChild(title);
38.
39.
CCSprite*
callFuncNDSp = CCSprite::create(
"Icon.png"
);
40.
callFuncNDSp->setPosition(ccp(size.width/
4
*
3
,
size.height*
0.5
));
41.
this
->addChild(callFuncNDSp);
42.
callFuncNDSp->runAction(CCCallFuncND::create(
this
,
callfuncND_selector(HelloWorld::callback3), (
void
*)title));
43.
44.
45.
return
true
;
46.
}
47.
48.
void
HelloWorld::callback1()
49.
{
50.
CCSize
s = CCDirector::sharedDirector()->getWinSize();
51.
CCLabelTTF
*label = CCLabelTTF::create(
"callback
1 called"
,
"Marker
Felt"
,
16
);
52.
label->setPosition(ccp(
s.width/
4
*
1
,s.height/
3
));
53.
addChild(label);
54.
}
55.
56.
//CCNode*
sender爲運行動作的它本身也就是CCSprite* callFuncNSp
57.
void
HelloWorld::callback2(CCNode*
sender)
58.
{
59.
CCSize
s = CCDirector::sharedDirector()->getWinSize();
60.
CCLabelTTF
*label = CCLabelTTF::create(
"callback
2 called"
,
"Marker
Felt"
,
16
);
61.
label->setPosition(ccp(
s.width/
4
*
2
,s.height/
3
));
62.
addChild(label);
63.
64.
//自身做順時針旋轉動作
65.
sender->runAction(CCRepeatForever::create(CCRotateBy::create(
1
,
360
)));
66.
}
67.
68.
//CCNode*
sender也就是CCSprite* callFuncNDSp. void的字面意思是“無類型”,void*則爲“無類型指針”,void*可以指向任何類型的數據。
69.
void
HelloWorld::callback3(CCNode*
sender,
void
*
data)
70.
{
71.
CCSize
s = CCDirector::sharedDirector()->getWinSize();
72.
CCLabelTTF
*label = CCLabelTTF::create(
"callback
3 called"
,
"Marker
Felt"
,
16
);
73.
label->setPosition(ccp(
s.width/
4
*
3
,s.height/
3
));
74.
addChild(label);
75.
76.
//自身做逆時針旋轉動作
77.
sender->runAction(CCRepeatForever::create(CCRotateBy::create(
1
,
-
360
)));
78.
79.
//void*
data也就是CCLabelTTF* title
80.
CCLabelTTF*
title = (CCLabelTTF*)data;
81.
title->runAction(CCRepeatForever::create(CCJumpBy::create(
1
,
ccp(
0
,
0
),
30
,
2
)));