更改CCButton的图像

时间:2014-06-30 17:38:14

标签: ios xcode cocos2d-iphone

这看起来应该很简单,但我找不到答案。我使用下面的函数创建了一个CCButton。现在当一个事件发生时,我想要从这个函数返回的CCButton让它的图像(spriteFrame)变成另一个图像。有人能告诉我我需要做什么吗?

+ (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;
}

1 个答案:

答案 0 :(得分:0)

以下是适合我的代码:

//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];