Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,startButton10,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.**

like image 238
Srinivas Avatar asked Jan 25 '11 11:01

Srinivas


2 Answers

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.

like image 75
lomanf Avatar answered Nov 14 '22 20:11

lomanf


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];   
}
like image 42
John Pavley Avatar answered Nov 14 '22 21:11

John Pavley