Я начал изучать Objective-C и использовать cocos2D около 1 месяца назад.
Я хочу заменить три разных слоя нажатием кнопок.
Сначала я попытался использовать «CCLayerMultiplex». Затем используйте «если предложение». Но тогда слои просто перекрываются или падают, когда нажимаются кнопки. Я хочу, чтобы предыдущий слой исчезал при появлении нового слоя, но старый слой теперь остается с моим кодом.
Я думаю, что использование "CCLayerMultiplex" - мой лучший вариант, но я не могу заставить его работать так, как я хочу.
Ниже мой код. Боюсь, что есть плохие предложения ...
@interface GSLayout : CCLayer {
// button items
CCMenuItemImage *file1;
CCMenuItemImage *file1Pushed;
...
// Layer (replace)
CCLayer *layer1;
CCLayer *layer2;
CCLayer *layer3;
// replace layers
CCLayerMultiplex* mpLayer;
}
@end
@implementation GSLayout
-(id) init{
if( (self=[super init])) {
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
self.isTouchEnabled = YES;
// buttons
file1 = [CCMenuItemImage itemFromNormalImage:@"Icon-Small-50.png"
selectedImage: @"Icon-Small.png"
target:nil
selector:nil];
file1Pushed = [CCMenuItemImage itemFromNormalImage:@"Icon-Small.png"
selectedImage:@"Icon-Small-50.png"
target:nil
selector:nil];
CCMenuItemToggle *toggleFile1 = [CCMenuItemToggle itemWithTarget:self
selector:@selector(selectOne:)
items:file1,file1Pushed, nil];
toggleFile1.anchorPoint = CGPointMake(0.5f, 0.5f);
file2 = [[CCMenuItemImage itemFromNormalImage:@"Icon-Small-50.png"
selectedImage: @"Icon-Small.png"
target:nil
selector:nil]retain];
file2Pushed = [[CCMenuItemImage itemFromNormalImage:@"Icon-Small.png"
selectedImage:@"Icon-Small-50.png"
target:nil
selector:nil]retain];
CCMenuItemToggle *toggleFile2 = [CCMenuItemToggle itemWithTarget:self
selector:@selector(selectTwo:)
items:file2,file2Pushed, nil];
toggleFile2.anchorPoint = CGPointMake(0.5f, 0.5f);
...
CCMenu *toggleMenu = [CCMenu menuWithItems:toggleFile1,toggleFile2,toggleFile3, nil];
[toggleMenu alignItemsHorizontally];
toggleMenu.anchorPoint = CGPointMake(0, 1.0f);
CGSize screenSize = [[CCDirector sharedDirector] winSize];
toggleMenu.position = CGPointMake(screenSize.width/2, screenSize.height);
[self addChild:toggleMenu];
// create layers
layer1 = [GameFile1 node];
layer2 = [GameFile2 node];
layer3 = [GameFile3 node];
mpLayer = [CCLayerMultiplex layerWithLayers:layer1,layer2,layer3, nil];
}
return self;
}
- (void) selectOne: (CCMenuItem*) menuItem
{
NSLog(@"The first menu was called");
if([layer1 isRunning])
{
nil;
NSLog(@"The layer1 is running");
} else if([layer2 isRunning]) {
[mpLayer switchTo:0];
NSLog(@"The layer2 was replaced");
}else if([layer3 isRunning]) {
[mpLayer switchTo:0];
NSLog(@"The layer3 was replaced");
} else{
[self addChild:layer1];
NSLog(@"The layer1 was called");
}
}
- (void) selectTwo: (CCMenuItem*) menuItem
{
NSLog(@"The second menu was called");
if([layer2 isRunning])
{
nil;
NSLog(@"The layer2 is running");
} else if([layer1 isRunning]) {
[mpLayer switchTo:1];
NSLog(@"The layer1 was replaced");
} else if([layer3 isRunning]) {
[mpLayer switchTo:1];
NSLog(@"The layer3 was replaced");
}else{
[self addChild:layer2];
NSLog(@"The layer2 was called");
}
}
- (void) selectThree: (CCMenuItem*) menuItem
{
NSLog(@"The third menu was called");
...
}
Пожалуйста, дайте мне совет!
Заранее спасибо!
Я добавлю несколько кодов.
@interface GameFile1 : CCLayer {
CCSprite* sprite;
}
@implementation GameFile1
-(id) init{
if( (self=[super init])) {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CCMenuItemImage *soundItem1 = [CCMenuItemImage itemFromNormalImage:@"button1.png"
selectedImage: @"Icon.png"
target:self
selector:@selector(doSomethingOne:)];
CCMenuItemImage *soundItem2 = [CCMenuItemImage itemFromNormalImage:@"button2.png"
selectedImage: @"Icon.png"
target:self
selector:@selector(doSomethingTwo:)];
...
CCMenu * myMenu = [CCMenu menuWithItems:soundItem1, soundItem2,soundItem3,soundItem4, soundItem5,soundItem6,soundItem7, soundItem8,soundItem9,soundItem10,soundItem11,soundItem12, nil];
[myMenu alignItemsInRows:[NSNumber numberWithInt:4],[NSNumber numberWithInt:4],[NSNumber numberWithInt:4] ,nil];
myMenu.position = CGPointMake(370, 120);
[self addChild:myMenu];
sprite = [CCSprite spriteWithFile:@"o0400026611355530621.jpg"];
sprite.scale = 0.5f;
sprite.position = CGPointMake(screenSize.width/2, screenSize.height/2);
sprite.anchorPoint = CGPointMake(1.0f, 0);
[self addChild:sprite];
- (void) doSomethingOne: (CCMenuItem *) menuItem
{
NSLog(@"The first menu was called");
[[SimpleAudioEngine sharedEngine] playEffect:@"one.wav"];
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage: @"one.jpg"]];
}
- (void) doSomethingTwo: (CCMenuItem *) menuItem
{
NSLog(@"The second menu was called");
[[SimpleAudioEngine sharedEngine] playEffect:@"two.wav"];
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage: @"one.jpg"]];
}
...
@end