0

This seems like it should be simple enough, but I cannot find the answer. I have created a CCButton using the function below. Now when an event occurs, I want the CCButton that I return from this function to have its image (spriteFrame) change to another image. Can someone tell me what I need to do?

+ (CCButton*) drawBitmap :(id)not_self :(NSString*)normalImage :(double)x :(double)y :(double)w :(double)h withSelector:(SEL)sel :(NSString*)buttonName
{
    CGSize size = [[CCDirector sharedDirector] viewSize];
    double middleX = size.width * (w/2 + x);
    double middleY = size.height * (1 - y - h/2);

    CCButton *btn = [CCButton buttonWithTitle:@""
                                      spriteFrame:[CCSpriteFrame frameWithImageNamed:normalImage]
                           highlightedSpriteFrame:[CCSpriteFrame frameWithImageNamed:normalImage]
                              disabledSpriteFrame:[CCSpriteFrame frameWithImageNamed:normalImage]];
    [btn setName:buttonName];
    [btn setTarget:not_self selector:sel];

    btn.positionType=CCPositionTypeNormalized;
    [btn setScaleX: (size.width * w)/btn.contentSize.width];
    [btn setScaleY: (size.height * h)/btn.contentSize.height];
    btn.position = ccp(middleX/size.width , middleY/size.height);
    btn.cascadeOpacityEnabled=YES;

    [not_self addChild:btn];

    return btn;
}
Ryan Tensmeyer
  • 865
  • 10
  • 26
  • Have you tried `setBackgroundSpriteFrame:forState:`? http://www.cocos2d-iphone.org/docs/api/Classes/CCButton.html#//api/name/setBackgroundSpriteFrame:forState: – Insomniac Jun 30 '14 at 19:08
  • Do you have an example of using setBackgroundSpriteFrame:forState? I tried a few things (see the code below), but I couldn't get it to work. `CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache]; CCSpriteFrame* frame = [cache spriteFrameByName:IMG_RIGHT_GREEN];[image setBackgroundSpriteFrame:frame forState:CCControlStateNormal];` – Ryan Tensmeyer Jun 30 '14 at 22:44
  • Posted reply as an answer to show better formatted code. Your code should work too, but of course there can be many reasons why it doesnt (for example IMG_RIGHT_GREEN can be the same image the button already has and thus you won't see effect, it is quite often that we try to find the issue in a different place). – Insomniac Jul 01 '14 at 10:16

1 Answers1

0

Here is the code that works perfectly for me:

//Creating button
CCSpriteFrame *aboutNormalImage = [CCSpriteFrame frameWithImageNamed:@"btn_about.png"];
CCSpriteFrame *aboutHighlightedImage = [CCSpriteFrame frameWithImageNamed:@"btn_about_pressed.png"];
CCButton *btnAbout = [CCButton buttonWithTitle:nil spriteFrame:aboutNormalImage highlightedSpriteFrame:aboutHighlightedImage disabledSpriteFrame:nil];

//..the button is added to scene here and so on

//Loading another frame
CCSpriteFrame *startNormalImage = [CCSpriteFrame frameWithImageNamed:@"btn_start.png"];

//Replacing
[btnAbout setBackgroundSpriteFrame:startNormalImage forState:CCControlStateNormal];
Jonny
  • 15,955
  • 18
  • 111
  • 232
Insomniac
  • 3,354
  • 2
  • 23
  • 24
  • I have added your code (copied below to make sure I did it right, sorry about formatting), but I am getting an exception that says: -[CCSprite setBackgroundSpriteFrame:forState:]: unrecognized selector sent to instance 0x14e54980. Any ideas? – Ryan Tensmeyer Jul 02 '14 at 15:59
  • `CCButton *image; //button attributes added if(event){ CCSpriteFrame *startNormalImage = [CCSpriteFrame frameWithImageNamed:IMG_RIGHT_GREEN]; [image setBackgroundSpriteFrame:startNormalImage forState:CCControlStateNormal]; }` – Ryan Tensmeyer Jul 02 '14 at 16:00
  • The exception you mention can't happen if the `image` is a `CCButton`. Could you paste your full code somewhere? – Insomniac Jul 02 '14 at 16:16
  • I think I found my problem, at least in part. I have a couple of helper functions. One will add a CCSprite and the other a CCButton. I thought I was creating a CCButton, but I actually created a CCSprite. That would explain the exception. I switched to the function that returns the CCButton and the image now changes, but it is distorted. Similar to this post that doesn't have an answer: http://stackoverflow.com/questions/24358292/cocos2d-3-x-ccbutton-set-the-sprite-frame-image-distorted-and-callback-block-not – Ryan Tensmeyer Jul 02 '14 at 16:42
  • I was able to change it to a CCSprite without affecting functionality and then I used this post to change the image: http://stackoverflow.com/questions/6896075/changing-the-image-of-a-ccsprite – Ryan Tensmeyer Jul 02 '14 at 16:57