开发者

how to change the lock images to unlock images in cocos2d

am having game with 10 levels. i want to change the second level lock image to unlock when first level is completed.

am using 20 images ( 10 locked and 10 unlocked).

am using cc menus to display the number images. for example(code):-

 CCMenuItemImage *startButton12 = [CCMenuItemImage itemFromNormalImage:@"ten_new-lock.png"
        selectedImage:@"ten_new-lock.png" target:self
        selector:@selector(ten:)];

    menu1  = [CCMenu menuWithItems: startButton3,startButton4,startButton5,startButton6,startButton7,startButton8,startButton9,startButton1开发者_开发问答0,startButton11,startButton12, nil];
      menu1.position = ccp(240,30);
      [menu1 alignItemsHorizontally];
      [menuLayer1 addChild: menu1];

am using below code for remember the level completed.

 int lastLevelCompleted= [[NSUserDefaults standardUserDefaults] integerForKey:@"levelCompleted"];
    if(currentLevel >lastLevelCompleted){
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     [defaults setInteger:currentLevel forKey:@"levelCompleted"];

**now, how to change the lock to unlock images.

( if am doing here wrong)there is other way to solve means provide that. i have to implement that one.**


You could set a disabledImage when you create each CCMenuItemImage:

// create items by delclaring also a "disabled" image
CCMenuItemImage *menuItem = [CCMenuItemImage itemFromNormalImage:normalImage 
                                                   selectedImage:selectedImage 
                                                   disabledImage:disabledImage 
                                                          target:self 
                                                        selector:@selector(callbackMethod)];

Then just switch the state of the button as requested:

// then just use setIsEnabled to switch the state
[menuItem setIsEnabled:NO];

Cocos swaps the images for you.


I have the same idea in one of my games. I solved it with separate lock and done icons that I have positioned on top of each menu item that represents a level.

Just create your menu items normally. Don't try to represent locked or done states with the menu item's icon. Instead create a smaller locked and done icons that you will instantiate as sprites and position on top of each menu item.

Here is the relevant part of my menu layer's init method (I'm using a sprite atlas to store all my images):

// I save the state of each level as a character in a NSMutableString: 
self.completedState = 0x0043; // "C" (Completed)
self.openState = 0x004f; // "O"
self.lockedState = 0x004c; // "L"
self.dungeonAvailabilityState = @"COLLLLLLLLLLLLLLLL"; // in reality I get this string from a global object

// calc the position for the dungeon icon at row, column
x = (column*56)+148;
y = 244-(row*56);

// get the dungeon state
stateIndex = (row*columns)+column;
dungeonState = [self.dungeonAvailabilityState characterAtIndex:stateIndex];


// calc the position of the badges using offset from the menu item's icon
lockedX = x - DungeonsScreen_BadgeXoffset;
lockedY = y - DungeonsScreen_BadgeYoffset;
doneX = x - DungeonsScreen_BadgeXoffset;
doneY = y + DungeonsScreen_BadgeYoffset;

if (dungeonState == self.lockedState) {
 // add the lock icon
 [super badgeIconFromFrame:@"icon_lock.png" xPos:lockedX yPos:lockedY spriteTag:t++];
}

and my helper method badgeIconFromFrame looks like this:

- (void) badgeIconFromFrame:(NSString*)spriteName xPos:(float)x yPos:(float)y spriteTag:(int)t {
        CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);

        CCSprite* badgeSprite = [CCSprite spriteWithSpriteFrameName:spriteName];
        badgeSprite.position = CGPointMake(x, y);
        [self addChild:badgeSprite z:zIndexDecoration tag:t];   
}


What about (void) - setIsEnabled: method of CCMenuItem ?


Not sure if it works:

if(unlock){
 [menuLayer1 removeChild: menu1 cleanup:YES];

CCMenuItemImage *startButton12 = [CCMenuItemImage itemFromNormalImage:@"ten_new-unlock.png"
    selectedImage:@"ten_new-unlock.png" target:self
    selector:@selector(ten:)];

menu1  = [CCMenu menuWithItems: startButton3,startButton4,startButton5,startButton6,startButton7,startButton8,startButton9,startButton10,startButton11,startButton12, nil];
  menu1.position = ccp(240,30);
  [menu1 alignItemsHorizontally];
  [menuLayer1 addChild: menu1];
}
else
{
CCMenuItemImage *startButton12 = [CCMenuItemImage itemFromNormalImage:@"ten_new-lock.png"
    selectedImage:@"ten_new-lock.png" target:self
    selector:@selector(ten:)];

menu1  = [CCMenu menuWithItems: startButton3,startButton4,startButton5,startButton6,startButton7,startButton8,startButton9,startButton10,startButton11,startButton12, nil];
  menu1.position = ccp(240,30);
  [menu1 alignItemsHorizontally];
  [menuLayer1 addChild: menu1];
}


did u try using db (sqlite) to store the value of locked or unlocked. It will definitely work. But it is a big process.


Just use [menuItem setNormalImage:lockedButton];

LockedButton being another CCMenuItemImage with a new file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜