首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个参数调用coco2d-x函数?

使用多个参数调用coco2d-x函数?
EN

Stack Overflow用户
提问于 2014-07-31 20:30:28
回答 1查看 209关注 0票数 0

我正在做一个cocos2d-x项目,结果被卡住了。我想调用一个函数,延迟,然后再次调用相同的函数。我使用的是cocos2d-x 2.2.5。以及针对Android的开发。

这就是我到目前为止得到的:

代码语言:javascript
复制
CCArray *arr = CCArray::create();
arr->addObject(pSprite);
arr->addObject(pGetal);

CCFiniteTimeAction *fun1 = CCCallFuncO::create(this, callfuncO_selector(GameLayer::animateFlip), arr);
CCDelayTime *delay = CCDelayTime::create(1.0);
pSprite->runAction(CCSequence::create(fun1, delay, fun1, NULL));

我想调用的方法:

代码语言:javascript
复制
void GameLayer::animateFlip(CCObject *pSObj, CCObject *pGObj){

   CCSprite *pSprite = (CCSprite *) pSObj;
   CCLabelTTF *pGetal = (CCLabelTTF *) pSObj;
   ...
   ...
}

该函数在同一个类中,需要两个参数。我尝试将两个参数(CCSprite和CCLabelTTF)都放入一个数组中,但在运行时崩溃...当我像这样调用函数时,没有发生错误:

代码语言:javascript
复制
this->animateFlip(sprite1, getal1);

有谁知道吗?

EN

回答 1

Stack Overflow用户

发布于 2014-08-01 04:32:33

感谢你的回答,我已经按照Joachim的建议为我的按钮创建了一个结构,里面有sprite和标签。所有的按钮都放在一个数组中。我还向其中添加了函数animateFlip和showFlip。AnimateFlip运行良好,我可以从每个独立按钮的gamelayer中调用函数。

代码语言:javascript
复制
void GameLayer::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{

    CCTouch *touch = (CCTouch *)pTouches->anyObject();
    CCPoint location = touch->getLocationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);

    for(int i = 0; i < 12; i++){
        if(butArr[i].sprite->boundingBox().containsPoint(location)){
            butArr[i].animateFlip();
            break;
        }
    }
 }

结构:

代码语言:javascript
复制
struct Button
{
    cocos2d::CCSprite *sprite;
    cocos2d::CCLabelTTF *getal;
    int getalINT;
    void showFlip();
    void animateFlip();
};

但正如编程中的第一条规则告诉我们的那样,只要解决了一个问题,就会出现两个问题,我偶然发现了一个新问题:

代码语言:javascript
复制
void Button::showFlip()
{
    CCFiniteTimeAction *fun1 = CCCallFunc::create(this, callfunc_selector(Button::animateFlip));
    CCFiniteTimeAction *fun1 = CCCallFunc::create(this, callfunc_selector(Button::animateFlip));
    CCDelayTime *delay = CCDelayTime::create(1.0f);
    this->runAction(CCSequence::create(fun1, delay, fun2, NULL));
}

CCCallFunc::create()请求一个上下文(我猜?),通常是'this',但在我的struct中'this‘指向Button。我怎样才能得到玩家的上下文?

再次感谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25058901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档