发送参数到CCCallFuncND :: create Cocos2d-X [英] Sending Paramters to a CCCallFuncND::create Cocos2d-X

查看:64
本文介绍了发送参数到CCCallFuncND :: create Cocos2d-X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Cocos2d-X应用程序中有以下代码

I have the following code in my Cocos2d-X application

void SampleRequest::setResponseCallback(CCCallFuncND* cb){
    if(cb){
        cb->retain();
        stored_cb=cb;
    }
}


void SampleRequest::executeStoredCallback(){
    if(stored_cb)
        stored_cb->execute();
}

void SampleRequest::releaseCallback(){
    if(stored_cb){
        stored_cb->release();
        stored_cb=NULL;
    }
}

和一个简单的课程

void RequestHandler::handleSampleRequest(int data){
    CCLog("--------------------------------------------> Its here for me to do %d",data);
}

和另一个和平的代码

    int i=10;
    SampleRequest *t=new SampleRequest();
    t->setResponseCallback(
                CCCallFuncND::create(
                this,
                callfuncND_selector(RequestHandler::handleSampleRequest),
                (void*)&i));

但是我收到的值是0.如何将I的值发送回回调函数,以及如何向该函数发送多个参数.

but the value of i recieved is 0. How can i send the value of I back to the call back function, and how can i send multiple parameters to this function.

亲切的问候,

推荐答案

int i=10;

您是否将i声明为堆栈上的临时变量,而不是堆上的临时变量,或声明为请求对象实例数据?如果是这样,则在其创建的块退出时(变量作用域结束),您的i变量将被销毁.

Are you declaring i as a temporary variable on the stack, rather than on the heap, or as request object instance data? If so, your i variable will be destroyed when the block within which it is created exits (variable scope ends).

这可以解释为什么回调函数收到一个指向未定义内存的值,该值在调用时已被破坏.

That could explain why the callback receives a value pointing to undefined memory, that has been destroyed at the time of the call.

尝试使用new运算符,或者将i值存储在请求对象中,直到进行cb调用为止.

Try using the new operator, or storing your i value inside your request object up until the cb call is made.

如何向该函数发送多个参数

how can i send multiple parameters to this function

你不会;只需将指针传递给结构或对象即可.如果所有存储的数据都在请求"实例中,那么您也可以传递实例本身.

You would not ; Simply pass a pointer to a structure or object. If all your stored data is in your "request" instance, you can pass the instance itself, as well.

例如,再次假设传递给回调的数据将在调用回调函数(即下面的"RequestData"实例)时保留在内存中:

For an example, assuming, again, that the data passed to the callback is going to remain in memory at the time of the call to the callback function (ie, the "RequestData" instance below):

    struct RequestData
    {
      int value1 ;
      int value2 ;
      // ....
    } ;

   class RequestHandler: public cocos2d::CCObject
    {
       // ...
       public: 
         void requestCallback( CCNode* sender, void* pData ) ;
    }

在您的实现中:

RequestHandler::requestCallback( CCNode* sender, void* pData ) 
{
   RequestData* pRequestData = static_cast<RequestData*>( pData ) ;

  if ( pRequestData )
  {
     // do something ...
  }
}

要构造您的调用,请构建一个RequestData实例,其中包含您需要传递给回调的所有数据,并确保使用"new"或其他对象的一部分(例如,在队列中)将其分配到堆上以便在调用回调时其数据在内存中仍然有效.我坚持一点,因为您需要某种数据存储机制作为设计的一部分,否则您的回调可能会发现自己正在处理内存中的无效地址(悬空指针).

To construct your call, build an instance of RequestData containing all the data you need to pass to the callback, make sure it is allocated on the heap with "new" or part of another object (in a queue, for instance) so that its data will still be valid in memory at the time the callback is called. I insist a bit on this point because you need some kind of data storage mechanism as part of your design, otherwise your callbacks may find themselves working off invalid addresses in memory (dangling pointers).

本质上,根据您之前的代码:

Essentially, from your previous code:

RequestData* pRequestData = new RequestData();
// fill in the structure data here...

SampleRequest *t=new SampleRequest();
t->setResponseCallback(
            CCCallFuncND::create(
            this,
            callfuncND_selector(RequestHandler::requestCallback),
            (void*)pRequestData));

这篇关于发送参数到CCCallFuncND :: create Cocos2d-X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆