如何更换图层?

时间:2011-07-21 16:59:37

标签: ios cocos2d-iphone cclayer

我开始学习Objective-C并在1个月前使用cocos2D。

我想通过点击按钮来替换三个不同的图层。 起初,我尝试使用“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

1 个答案:

答案 0 :(得分:0)

由于您已经为所有三个图层加载了纹理,因此您可以让所有三个坐在单独的CCLayers中,其中两个在任何给定状态下使用类似于以下的方法隐藏:

- (void) setLayer:(uint)show {
    layerOne.visible = (show == 0);
    layerTwo.visible = (show == 1);
    layerThree.visible = (show == 2);
}