ControlSwitch is not visible but it works

Hi,
I have a rather strange problem.
I add in my proj a ControlSwitch:

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();
    Point center = Point(origin.x + visibleSize.width/2,
                         origin.y + visibleSize.height/2);

    auto maskSprite = Sprite::create("onoff.png");
    auto onSprite = Sprite::create("onoff.png");
    auto offSprite = Sprite::create("onoff.png");
    auto thumbSprite = Sprite::create("onoff.png");
    
    ControlSwitch * controlswitch = ControlSwitch::create(maskSprite,onSprite,offSprite,thumbSprite, LabelTTF::create("", "Helvetica-Bold", 10), LabelTTF::create("", "Helvetica-Bold", 10));
    controlswitch->setPosition(center);
    this->addChild(controlswitch);

I believe that this code is correct.

I build my app, the controlSwitch is not visible in the screen, but if I click in the position that I believe has been added, the log are printed…Why???:;angry

Help me
Thanks
Valerio

Why do you use the same graphic for all 4 sprites?
The mask sprite specifies the area for which the control switch is visible, so if the switch is invisible, it’s most likely because of the mask sprite. Try the graphics from this example and if it works, just modify the example files to your needs

Yes but there is always the problem :;angry …
I fix the code:

 Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();
    Point center = Point(origin.x + visibleSize.width/2,
                         origin.y + visibleSize.height/2);
    // Create the switch
    cocos2d::extension::ControlSwitch *switchControl = cocos2d::extension::ControlSwitch::create
    (
     Sprite::create("switch-mask.png"),
     Sprite::create("switch-on.png"),
     Sprite::create("switch-off.png"),
     Sprite::create("switch-thumb.png"),
     LabelTTF::create("On", "Arial-BoldMT", 16),
     LabelTTF::create("Off", "Arial-BoldMT", 16)
     );
    //switchControl->setPosition(Point(layer_width + 10 + switchControl->getContentSize().width / 2, 0));
    // Set the layer size
    switchControl->setContentSize(Size(300, 200));
    switchControl->setAnchorPoint(Point(0.5f, 0.5f));
    switchControl->setPosition(center);
    this->addChild(switchControl);

But Nothing…the ControlSwitch is not visible …:;qst

You shouldn’t use setContentSize to resize the ControlSwitch, because the ControlSwitch uses the content size to determine the layout of the individual sprites. Use scaleX/scaleY instead.

You can try to add a sprite to your ControlSwitch. So you can see if the position of the control switch is correct. If you can’t see the child sprite that means your position is incorrect. If you can see the sprite but not the switch, then you there probably is a problem with the mask sprite.

Nada! I used the code of example but it doesnt work. Is there a bug of framework?

Did you try the code without the line
switchControl->setContentSize(Size(300, 200))
?
I tested it in my menu. When I set the content size, the switch can be toggled, but it’s rendered outside of the screen.

yes! But in your demo proj swithControl ready?

I am seeing the same problem with Cocos 3.0-beta, even when running the TestCpp project. The switch behaves, but it is not visible on screen. ControlSwitch also crashes if you leave out the Label parameters, even though in the code there is create function that sets these to NULL, it crashed further on because of null referencing. I ran it on an iPhone4, iOS7.

There is actually a thread that says there is a bug in Cocos2d-x V3 that causes the error.