一个函数可以从两个不同的函数接收两个参数吗?

发布于 2024-12-28 15:04:07 字数 3408 浏览 0 评论 0原文

我有 2 个 void 函数(尝试实现单选按钮),我希望它们通过交换值将值发送到第三个函数。该函数将值返回给主函数?

我的 MyScene.h 文件的代码

#ifndef __MY_SCENE_H__
#define __MY_SCENE_H__

#include "cocos2d.h"
USING_NS_CC;

class MyScene : public cocos2d::CCLayerColor
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();


    CCMenuItemToggle *R;

    CCMenuItemToggle *L;

    // a selector callback
    void swapL(CCObject *sender);
    void swapR(CCObject *sender);

    // implement the "static node()" method manually
    LAYER_NODE_FUNC(MyScene);
};

#endif // __HELLOWORLD_SCENE_H__

我的 MyScene.cpp 文件的代码

#include "MyScene.h"

USING_NS_CC;

CCScene* MyScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::node();

    // 'layer' is an autorelease object
    MyScene *layer = MyScene::node();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MyScene::init()
{


    //////////////////////////////
    // 1. super init first
    if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255) ))
    {
        return false;
    }

    //////////////////////////////
    // 2. add your codes below...

    CCSize WinSize= CCDirector::sharedDirector()->getWinSizeInPixels();

    CCSprite * fish=CCSprite::spriteWithFile("fish_bg.png");
    fish->setPosition(CCPointZero);
    fish->setAnchorPoint(CCPointZero);
    fish->setScaleX(WinSize.width/480);
    fish->setScaleY(WinSize.height/395);
    this->addChild(fish,0,0);


    CCSprite * on1=CCSprite::spriteWithFile("on.png");
    CCSprite * on2=CCSprite::spriteWithFile("on.png");
    CCSprite * on3=CCSprite::spriteWithFile("on.png");
    CCSprite * on4=CCSprite::spriteWithFile("on.png");

    CCSprite * off1=CCSprite::spriteWithFile("off.png");
    CCSprite * off2=CCSprite::spriteWithFile("off.png");
    CCSprite * off3=CCSprite::spriteWithFile("off.png");
    CCSprite * off4=CCSprite::spriteWithFile("off.png");


    CCMenuItem *LeftOn=CCMenuItemSprite::itemFromNormalSprite(on1,on2);
    CCMenuItem *RightOn=CCMenuItemSprite::itemFromNormalSprite(on3,on4);
    CCMenuItem *LeftOff=CCMenuItemSprite::itemFromNormalSprite(off1,off2);
    CCMenuItem *RightOff=CCMenuItemSprite::itemFromNormalSprite(off3,off4);

    CCMenuItemToggle *Left  = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapL),LeftOn,LeftOff,NULL);

    CCMenuItemToggle *Right = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapR),RightOn,RightOff,NULL);

    CCMenu *Radio= CCMenu::menuWithItems(Left,Right,NULL);

    Radio->alignItemsHorizontallyWithPadding(20);
    Radio->setPosition(ccp(WinSize.width/2,WinSize.height/2));


    this->addChild(Radio);
    //////////////////////////////
    return true;
}

void MyScene::swapL(CCObject *sender)
{
    L= (CCMenuItemToggle*)sender;
    CCLOG("L= %d",L->getSelectedIndex());

    int i=(L->getSelectedIndex());


}

void MyScene::swapR(CCObject *sender)
{
    R= (CCMenuItemToggle*)sender;

    CCLOG("R= %d",R->getSelectedIndex());
    int j=(R->getSelectedIndex());


}

i have 2 void functions(trying to implement radio button), i want them to send value to a third function by swapping values. and that function returning value to main function?

CODE OF MY MyScene.h FILE

#ifndef __MY_SCENE_H__
#define __MY_SCENE_H__

#include "cocos2d.h"
USING_NS_CC;

class MyScene : public cocos2d::CCLayerColor
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();


    CCMenuItemToggle *R;

    CCMenuItemToggle *L;

    // a selector callback
    void swapL(CCObject *sender);
    void swapR(CCObject *sender);

    // implement the "static node()" method manually
    LAYER_NODE_FUNC(MyScene);
};

#endif // __HELLOWORLD_SCENE_H__

CODE OF MY MyScene.cpp FILE

#include "MyScene.h"

USING_NS_CC;

CCScene* MyScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::node();

    // 'layer' is an autorelease object
    MyScene *layer = MyScene::node();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MyScene::init()
{


    //////////////////////////////
    // 1. super init first
    if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255) ))
    {
        return false;
    }

    //////////////////////////////
    // 2. add your codes below...

    CCSize WinSize= CCDirector::sharedDirector()->getWinSizeInPixels();

    CCSprite * fish=CCSprite::spriteWithFile("fish_bg.png");
    fish->setPosition(CCPointZero);
    fish->setAnchorPoint(CCPointZero);
    fish->setScaleX(WinSize.width/480);
    fish->setScaleY(WinSize.height/395);
    this->addChild(fish,0,0);


    CCSprite * on1=CCSprite::spriteWithFile("on.png");
    CCSprite * on2=CCSprite::spriteWithFile("on.png");
    CCSprite * on3=CCSprite::spriteWithFile("on.png");
    CCSprite * on4=CCSprite::spriteWithFile("on.png");

    CCSprite * off1=CCSprite::spriteWithFile("off.png");
    CCSprite * off2=CCSprite::spriteWithFile("off.png");
    CCSprite * off3=CCSprite::spriteWithFile("off.png");
    CCSprite * off4=CCSprite::spriteWithFile("off.png");


    CCMenuItem *LeftOn=CCMenuItemSprite::itemFromNormalSprite(on1,on2);
    CCMenuItem *RightOn=CCMenuItemSprite::itemFromNormalSprite(on3,on4);
    CCMenuItem *LeftOff=CCMenuItemSprite::itemFromNormalSprite(off1,off2);
    CCMenuItem *RightOff=CCMenuItemSprite::itemFromNormalSprite(off3,off4);

    CCMenuItemToggle *Left  = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapL),LeftOn,LeftOff,NULL);

    CCMenuItemToggle *Right = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapR),RightOn,RightOff,NULL);

    CCMenu *Radio= CCMenu::menuWithItems(Left,Right,NULL);

    Radio->alignItemsHorizontallyWithPadding(20);
    Radio->setPosition(ccp(WinSize.width/2,WinSize.height/2));


    this->addChild(Radio);
    //////////////////////////////
    return true;
}

void MyScene::swapL(CCObject *sender)
{
    L= (CCMenuItemToggle*)sender;
    CCLOG("L= %d",L->getSelectedIndex());

    int i=(L->getSelectedIndex());


}

void MyScene::swapR(CCObject *sender)
{
    R= (CCMenuItemToggle*)sender;

    CCLOG("R= %d",R->getSelectedIndex());
    int j=(R->getSelectedIndex());


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2025-01-04 15:04:07

是否有可能有 2 个 void 函数将这两个函数中的参数分别发送到第三个函数?

是的,这是可能的,为什么你认为这是不可能的?

在线示例

#include<iostream>

void doSomething(int &i)
{
    i = 10;
}

void doSomethingMore(int &j)
{
    j = 20;
}

void FinalDoSomething(const int i, const int j, int &k)
{
    k = i + j;
}

int main()
{
    int i = 0;
    doSomething(i);    

    int j = 0;
    doSomethingMore(j);

    int k = 0;
    FinalDoSomething(i,j,k);

    std::cout<<k;

    return 0;
}

Is it possible to have 2 void functions to send arguements to a third function one each from those 2 functions ?

Yes, It's possible, Why do you think it is not possible?

Online Sample:

#include<iostream>

void doSomething(int &i)
{
    i = 10;
}

void doSomethingMore(int &j)
{
    j = 20;
}

void FinalDoSomething(const int i, const int j, int &k)
{
    k = i + j;
}

int main()
{
    int i = 0;
    doSomething(i);    

    int j = 0;
    doSomethingMore(j);

    int k = 0;
    FinalDoSomething(i,j,k);

    std::cout<<k;

    return 0;
}
末骤雨初歇 2025-01-04 15:04:07

您可以在调用之间有一个方法。该函数将第一个调用值存储到一个成员中,在第二次调用时,它调用您想要传递两个参数的函数(使用先前传递的一个 + 成员)

You could have a method between that calls. This function stores the first call value into a member and on the second call it calls the function you want to pass the two parameters (using the previously passed one + the member

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文