简体   繁体   English

更改CCButton的图像

[英]Change the image of a CCButton

This seems like it should be simple enough, but I cannot find the answer. 这似乎应该足够简单,但是我找不到答案。 I have created a CCButton using the function below. 我使用下面的函数创建了一个CCButton。 Now when an event occurs, I want the CCButton that I return from this function to have its image (spriteFrame) change to another image. 现在,当事件发生时,我希望从此函数返回的CCButton将其图像(spriteFrame)更改为另一个图像。 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;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM