Cocos2d 3.x CCButton sets a distorted image of a sprite frame, and callback / block does not work

I need to set the sprite frame of a button.

When I use the constructor, it works:

    CCSprite *sprite = [CCSprite spriteWithImageNamed:@"but.png"];
    CCButton *button = [CCButton buttonWithTitle:@"" spriteFrame:sprite.spriteFrame];
    [button setBlock:^(id sender){...}]

enter image description here

But when I need to set the sprite frame,

    CCButton *button = [CCButton buttonWithTitle:@""];
    button.background.spriteFrame = sprite.spriteFrame;

or

    [button setBackgroundSpriteFrame:sprite.spriteFrame forState:CCControlStateNormal];

both do not work, the result is a distorted image, and the callback function or block is not called.

enter image description here

+4
source share
1 answer

Hi, I had the same problem and found a solution. Just do it like this:

CCSpriteFrame *spriteFrame = [CCSpriteFrame frameWithImageNamed:@"Icon-Small.png"];
button = [CCButton buttonWithTitle:@"" spriteFrame:spriteFrame];

It works like a charm!

0
source

Source: https://habr.com/ru/post/1545614/


All Articles