Une fonction peut-elle recevoir deux arguments chacun de deux fonctions différentes?

StackOverflow https://stackoverflow.com/questions/8983319

  •  13-11-2019
  •  | 
  •  

Question

J'ai 2 fonctions annulées (essayer de mettre en oeuvre le bouton radio), je souhaite qu'ils envoient une valeur à une troisième fonction en échangeant des valeurs.et cette fonction de la valeur renvoyée à la fonction principale?

code de mon fichier 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__

code de mon fichier 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());


}

Était-ce utile?

La solution

est-il possible d'avoir 2 fonctions annulées pour envoyer des arguments à une troisième fonction chacune de ces 2 fonctions?

Oui, c'est possible, pourquoi pensez-vous que ce n'est pas possible?

échantillon en ligne :

#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;
}

Autres conseils

Vous pouvez avoir une méthode entre ces appels.Cette fonction stocke la première valeur d'appel dans un membre et sur le deuxième appel, il appelle la fonction que vous souhaitez transmettre les deux paramètres (à l'aide de l'un + transmis précédemment sur le membre

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top