如何在cocos2d中实现按下按钮后由菜单切换成另一张图片 并且隐藏原来的按钮!!急 20
我的代码我切换了图片但是按钮还在图片上下面是我的代码.h文件#import<Foundation/Foundation.h>#import"cocos2d.h"@inte...
我的代码我切换了图片但是按钮还在图片上下面是我的代码
.h文件
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface MenuLayer : CCLayer {
}
- (void) stratGame: (id)sender;
- (void) scores: (id)sender;
@end
.m文件
#import "MenuLayer.h"
@implementation MenuLayer
- (id) init
{
self = [super init];
if (self) {
[CCMenuItemFont setFontName:@"Helvetica"];
[CCMenuItemFont setFontSize:30];
CCMenuItem *start = [CCMenuItemFont itemFromString:@"Start Game"
target:self
selector:@selector(startGame:)];
CCMenuItem *scores = [CCMenuItemFont itemFromString:@"Scores"
target:self
selector:@selector(scores:)];
CCMenu *menu = [CCMenu menuWithItems:start, scores, nil];
[menu alignItemsVertically];
[self addChild:menu];
}
return self;
}
- (void) startGame: (id)sender {
CGSize winSize = [[CCDirector sharedDirector]winSize];
CCSprite *bg = [CCSprite spriteWithFile:@"gamestart.png"];
bg.position = ccp(winSize.width / 2,winSize.height / 2 );
[self addChild:bg];
[self addChild:[MenuLayer node]];
}
- (void) scores: (id)sender {
}
@end 展开
.h文件
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface MenuLayer : CCLayer {
}
- (void) stratGame: (id)sender;
- (void) scores: (id)sender;
@end
.m文件
#import "MenuLayer.h"
@implementation MenuLayer
- (id) init
{
self = [super init];
if (self) {
[CCMenuItemFont setFontName:@"Helvetica"];
[CCMenuItemFont setFontSize:30];
CCMenuItem *start = [CCMenuItemFont itemFromString:@"Start Game"
target:self
selector:@selector(startGame:)];
CCMenuItem *scores = [CCMenuItemFont itemFromString:@"Scores"
target:self
selector:@selector(scores:)];
CCMenu *menu = [CCMenu menuWithItems:start, scores, nil];
[menu alignItemsVertically];
[self addChild:menu];
}
return self;
}
- (void) startGame: (id)sender {
CGSize winSize = [[CCDirector sharedDirector]winSize];
CCSprite *bg = [CCSprite spriteWithFile:@"gamestart.png"];
bg.position = ccp(winSize.width / 2,winSize.height / 2 );
[self addChild:bg];
[self addChild:[MenuLayer node]];
}
- (void) scores: (id)sender {
}
@end 展开
3个回答
展开全部
cocos2d一版是用CCMenuItemToggle来处理一个button来回切换不同的图片,不知道能不能符合你的要求。这样做起来比较简单。
这里是实现的部分代码:
CCMenuItemImage *plusItem = [CCMenuItemImage itemFromNormalImage:@"ButtonPlus.png"
selectedImage:@"ButtonPlusSel.png" target:nil selector:nil];
CCMenuItemImage *minusItem = [CCMenuItemImage itemFromNormalImage:@"ButtonMinus.png" selectedImage:@"ButtonMinusSel.png" target:nil selector:nil];
CCMenuItemImage *starItem = [CCMenuItemImage itemFromNormalImage:@"ButtonStar.png"
selectedImage:@"ButtonStarSel" target:nil selector:nil];
CCMenuItemToggle *toggleItem = [CCMenuItemToggle itemWithTarget:self selector:@selector(plusMinusButtonTapped:) items:plusItem, minusItem, starItem,nil];
CCMenu *toggleMenu = [CCMenu menuWithItems:toggleItem, nil];
toggleMenu.position = ccp(60, 120);
[self addChild:toggleMenu];
这样能看懂吗?plusMinusButtonTapped方法定义。
这里是实现的部分代码:
CCMenuItemImage *plusItem = [CCMenuItemImage itemFromNormalImage:@"ButtonPlus.png"
selectedImage:@"ButtonPlusSel.png" target:nil selector:nil];
CCMenuItemImage *minusItem = [CCMenuItemImage itemFromNormalImage:@"ButtonMinus.png" selectedImage:@"ButtonMinusSel.png" target:nil selector:nil];
CCMenuItemImage *starItem = [CCMenuItemImage itemFromNormalImage:@"ButtonStar.png"
selectedImage:@"ButtonStarSel" target:nil selector:nil];
CCMenuItemToggle *toggleItem = [CCMenuItemToggle itemWithTarget:self selector:@selector(plusMinusButtonTapped:) items:plusItem, minusItem, starItem,nil];
CCMenu *toggleMenu = [CCMenu menuWithItems:toggleItem, nil];
toggleMenu.position = ccp(60, 120);
[self addChild:toggleMenu];
这样能看懂吗?plusMinusButtonTapped方法定义。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询