cocos2d-x中CCCallFunc CCCallFuncN CCCallFuncND的区别和使用示例

前端之家收集整理的这篇文章主要介绍了cocos2d-x中CCCallFunc CCCallFuncN CCCallFuncND的区别和使用示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

CCCallFunc CCCallFuncN CCCallFuncND的区别和使用

CCCallFunc CCCallFuncN CCCallFuncND都用来创建带有回调函数的动作,区别主要在于回调函数是否带有参数

CCCallFunc

CCCallFunc是执行对应的回调函数,其中回调函数不可带参数。一般使用静态成员函数create创建实例,create声明如下:

<span class="kwd">static</span><span class="pln"> </span><span class="typ">CCCallFunc</span><span class="pun">*</span><span class="pln"> create	</span><span class="pun">(</span><span class="pln">	</span><span class="typ">CCObject</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> 	pSelectorTarget</span><span class="pun">,</span><span class="pln">
SEL_CallFunc 	selector
</span><span class="pun">)</span>

回调函数通过execute方法执行,CCCallFunc中的execute的实现如下:

<span class="kwd">void</span><span class="pln"> </span><span class="typ">CCCallFunc</span><span class="pun">::</span><span class="pln">execute</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">m_pCallFunc</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="pun">(</span><span class="pln">m_pSelectorTarget</span><span class="pun">->*</span><span class="pln">m_pCallFunc</span><span class="pun">)();</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
	</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">m_nScriptHandler</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
		</span><span class="typ">CCScriptEngineManager</span><span class="pun">::</span><span class="pln">sharedManager</span><span class="pun">()-></span><span class="pln">getScriptEngine</span><span class="pun">()-></span><span class="pln">executeCallFuncActionEvent</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">);</span><span class="pln">
	</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span>

通过(m_pSelectorTarget->*m_pCallFunc)();可以看到回调函数不包含参数

CCCallFuncN

CCCallFuncN也是执行对应的回调函数,其中回调函数带一个参数。一般使用静态成员函数create创建实例,create声明如下:

<span class="kwd">static</span><span class="pln"> </span><span class="typ">CCCallFuncN</span><span class="pun">*</span><span class="pln"> create	</span><span class="pun">(</span><span class="pln">	</span><span class="typ">CCObject</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> 	pSelectorTarget</span><span class="pun">,</span><span class="pln">
SEL_CallFuncN 	selector
</span><span class="pun">)</span>

回调函数通过execute方法执行,CCCallFuncN中的execute的实现如下:

<span class="kwd">void</span><span class="pln"> </span><span class="typ">CCCallFuncN</span><span class="pun">::</span><span class="pln">execute</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">m_pCallFuncN</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="pun">(</span><span class="pln">m_pSelectorTarget</span><span class="pun">->*</span><span class="pln">m_pCallFuncN</span><span class="pun">)(</span><span class="pln">m_pTarget</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
	</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">m_nScriptHandler</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
		</span><span class="typ">CCScriptEngineManager</span><span class="pun">::</span><span class="pln">sharedManager</span><span class="pun">()-></span><span class="pln">getScriptEngine</span><span class="pun">()-></span><span class="pln">executeCallFuncActionEvent</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">,</span><span class="pln"> m_pTarget</span><span class="pun">);</span><span class="pln">
	</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span>

通过(m_pSelectorTarget->*m_pCallFuncN)(m_pTarget);可以看到回调函数包含一个参数。

CCCallFuncND

CCCallFuncND也是执行对应的回调函数,其中回调函数可带两个参数。一般使用静态成员函数create创建实例,create声明如下:

<span class="kwd">static</span><span class="pln"> </span><span class="typ">CCCallFuncND</span><span class="pun">*</span><span class="pln"> create	</span><span class="pun">(</span><span class="pln">	</span><span class="typ">CCObject</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> 	pSelectorTarget</span><span class="pun">,</span><span class="pln">
SEL_CallFuncND 	selector</span><span class="pun">,</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> 	d
</span><span class="pun">)</span>

回调函数通过execute方法执行,CCCallFuncND中的execute的实现如下:

<span class="kwd">void</span><span class="pln"> </span><span class="typ">CCCallFuncND</span><span class="pun">::</span><span class="pln">execute</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">m_pCallFuncND</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="pun">(</span><span class="pln">m_pSelectorTarget</span><span class="pun">->*</span><span class="pln">m_pCallFuncND</span><span class="pun">)(</span><span class="pln">m_pTarget</span><span class="pun">,</span><span class="pln"> m_pData</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span>

通过(m_pSelectorTarget->*m_pCallFuncND)(m_pTarget,m_pData);可以看到回调函数包含两个参数。

CCCallFunc CCCallFuncN CCCallFuncND实例对比

<span class="pln">testCallFunc</span><span class="pun">.</span><span class="pln">h</span><span class="pun">中代码</span><span class="pln">
</span><span class="kwd">class</span><span class="pln"> testCallFunc </span><span class="pun">:</span><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">CCLayer</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
</span><span class="kwd">protected</span><span class="pun">:</span><span class="pln">
    </span><span class="typ">CCSprite</span><span class="pun">*</span><span class="pln">    sprite1</span><span class="pun">;</span><span class="pln">
    </span><span class="typ">CCSprite</span><span class="pun">*</span><span class="pln">    sprite2</span><span class="pun">;</span><span class="pln">
    </span><span class="typ">CCSprite</span><span class="pun">*</span><span class="pln">    sprite3</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">public</span><span class="pun">:</span><span class="pln">
    </span><span class="kwd">virtual</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onEnter</span><span class="pun">();</span><span class="pln">

    </span><span class="kwd">void</span><span class="pln"> callback1</span><span class="pun">();</span><span class="pln">
    </span><span class="kwd">void</span><span class="pln"> callback2</span><span class="pun">(</span><span class="typ">CCNode</span><span class="pun">*</span><span class="pln"> sender</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">void</span><span class="pln"> callback3</span><span class="pun">(</span><span class="typ">CCNode</span><span class="pun">*</span><span class="pln"> sender</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">void</span><span class="pun">*</span><span class="pln"> data</span><span class="pun">);</span><span class="pln">
</span><span class="pun">};</span><span class="pln">

testCallFunc</span><span class="pun">.</span><span class="pln">cpp</span><span class="pun">中代码</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> testCallFunc</span><span class="pun">::</span><span class="pln">onEnter</span><span class="pun">()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
    </span><span class="com">//CCCallFunc的使用</span><span class="pln">
    </span><span class="typ">CCFiniteTimeAction</span><span class="pun">*</span><span class="pln">  action </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCSequence</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="pln">
        </span><span class="typ">CCMoveBy</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> ccp</span><span class="pun">(</span><span class="lit">200</span><span class="pun">,</span><span class="lit">0</span><span class="pun">)),</span><span class="pln">
        </span><span class="typ">CCCallFunc</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">,</span><span class="pln"> callfunc_selector</span><span class="pun">(</span><span class="pln">testCallFunc</span><span class="pun">::</span><span class="pln">callback1</span><span class="pun">)),</span><span class="pln">
        NULL</span><span class="pun">);</span><span class="pln">

    </span><span class="com">//CCCallFuncN的使用</span><span class="pln">
    </span><span class="typ">CCFiniteTimeAction</span><span class="pun">*</span><span class="pln">  action2 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCSequence</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="pln">
        </span><span class="typ">CCScaleBy</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="lit">2</span><span class="pln"> </span><span class="pun">,</span><span class="pln">  </span><span class="lit">2</span><span class="pun">),</span><span class="pln">
        </span><span class="typ">CCFadeOut</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="lit">2</span><span class="pun">),</span><span class="pln">
        </span><span class="typ">CCCallFuncN</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">,</span><span class="pln"> callfuncN_selector</span><span class="pun">(</span><span class="pln">testCallFunc</span><span class="pun">::</span><span class="pln">callback2</span><span class="pun">)),</span><span class="pln">
        NULL</span><span class="pun">);</span><span class="pln">

    </span><span class="com">//CCCallFuncNC的使用</span><span class="pln">
    </span><span class="typ">CCFiniteTimeAction</span><span class="pun">*</span><span class="pln">  action3 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCSequence</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="pln">
        </span><span class="typ">CCRotateBy</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="lit">3</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span class="lit">360</span><span class="pun">),</span><span class="pln">
        </span><span class="typ">CCCallFuncND</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">,</span><span class="pln"> callfuncND_selector</span><span class="pun">(</span><span class="pln">testCallFunc</span><span class="pun">::</span><span class="pln">callback3</span><span class="pun">),</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">void</span><span class="pun">*)</span><span class="lit">0xbebabeba</span><span class="pun">),</span><span class="pln">
        NULL</span><span class="pun">);</span><span class="pln">

    sprite1</span><span class="pun">-></span><span class="pln">runAction</span><span class="pun">(</span><span class="pln">action</span><span class="pun">);</span><span class="pln">
    sprite2</span><span class="pun">-></span><span class="pln">runAction</span><span class="pun">(</span><span class="pln">action2</span><span class="pun">);</span><span class="pln">
    sprite3</span><span class="pun">-></span><span class="pln">runAction</span><span class="pun">(</span><span class="pln">action3</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> testCallFunc</span><span class="pun">::</span><span class="pln">callback1</span><span class="pun">()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
    </span><span class="typ">CCSize</span><span class="pln"> s </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCDirector</span><span class="pun">::</span><span class="pln">sharedDirector</span><span class="pun">()-></span><span class="pln">getWinSize</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">CCLabelTTF</span><span class="pln"> </span><span class="pun">*</span><span class="pln">label </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCLabelTTF</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="str">"callback 1 called"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"Marker Felt"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">16</span><span class="pun">);</span><span class="pln">
    label</span><span class="pun">-></span><span class="pln">setPosition</span><span class="pun">(</span><span class="pln">ccp</span><span class="pun">(</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">width</span><span class="pun">/</span><span class="lit">4</span><span class="pun">*</span><span class="lit">1</span><span class="pun">,</span><span class="pln">s</span><span class="pun">.</span><span class="pln">height</span><span class="pun">/</span><span class="lit">2</span><span class="pun">));</span><span class="pln">

    addChild</span><span class="pun">(</span><span class="pln">label</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> testCallFunc</span><span class="pun">::</span><span class="pln">callback2</span><span class="pun">(</span><span class="typ">CCNode</span><span class="pun">*</span><span class="pln"> pSender</span><span class="pun">)</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
    </span><span class="typ">CCSize</span><span class="pln"> s </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCDirector</span><span class="pun">::</span><span class="pln">sharedDirector</span><span class="pun">()-></span><span class="pln">getWinSize</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">CCLabelTTF</span><span class="pln"> </span><span class="pun">*</span><span class="pln">label </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCLabelTTF</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="str">"callback 2 called"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">16</span><span class="pun">);</span><span class="pln">
    label</span><span class="pun">-></span><span class="pln">setPosition</span><span class="pun">(</span><span class="pln">ccp</span><span class="pun">(</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">width</span><span class="pun">/</span><span class="lit">4</span><span class="pun">*</span><span class="lit">2</span><span class="pun">,</span><span class="pln">s</span><span class="pun">.</span><span class="pln">height</span><span class="pun">/</span><span class="lit">2</span><span class="pun">));</span><span class="pln">

    addChild</span><span class="pun">(</span><span class="pln">label</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> testCallFunc</span><span class="pun">::</span><span class="pln">callback3</span><span class="pun">(</span><span class="typ">CCNode</span><span class="pun">*</span><span class="pln"> pTarget</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">void</span><span class="pun">*</span><span class="pln"> data</span><span class="pun">)</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
    </span><span class="typ">CCSize</span><span class="pln"> s </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCDirector</span><span class="pun">::</span><span class="pln">sharedDirector</span><span class="pun">()-></span><span class="pln">getWinSize</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">CCLabelTTF</span><span class="pln"> </span><span class="pun">*</span><span class="pln">label </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CCLabelTTF</span><span class="pun">::</span><span class="pln">create</span><span class="pun">(</span><span class="str">"callback 3 called"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">16</span><span class="pun">);</span><span class="pln">
    label</span><span class="pun">-></span><span class="pln">setPosition</span><span class="pun">(</span><span class="pln">ccp</span><span class="pun">(</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">width</span><span class="pun">/</span><span class="lit">4</span><span class="pun">*</span><span class="lit">3</span><span class="pun">,</span><span class="pln">s</span><span class="pun">.</span><span class="pln">height</span><span class="pun">/</span><span class="lit">2</span><span class="pun">));</span><span class="pln">
    addChild</span><span class="pun">(</span><span class="pln">label</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span>
原文链接:https://www.f2er.com/cocos2dx/346274.html

猜你在找的Cocos2d-x相关文章