Changing Label/Sprite in CCMenu - cocos2d-iphone

I'm new to Cocos2D and ran into a problem when I'm trying to implement a CCMenu. I am wondering how to change the label/sprite in the CCMenu after it is added to the layer. The code below is what I've tried so far.
CGSize screenSize = [CCDirector sharedDirector].winSize;
CCLabelTTF *label1 = [CCLabelTTF labelWithString:#"HELLO!!!" fontName:#"Arial" fontSize:20];
CCLabelTTF *label2 = [CCLabelTTF labelWithString:#"BYE!!!" fontName:#"Arial" fontSize:20];
CCMenuItemLabel *labelItem1 = [CCMenuItemLabel itemWithLabel:label1];
[labelItem1 setTag:111];
CCMenu *menu = [CCMenu menuWithItems:labelItem1, nil];
[self addChild:menu];
[menu setPosition:ccp(screenSize.width/2, screenSize.height/2)];
[(CCMenuItemLabel*)[labelItem1 getChildByTag:111] setLabel:label2];
I can't figure out why the screen is still showing "HELLO!!" instead of "BYE!!!". Please help me out. I would really appreciate for your help.

hmmm the way I read this, labelItem1 is a child of menu. Try
[(CCMenuItemLabel*)[menu getChildByTag:111] setLabel:label2];

Related

Hide and Remove Admob banner

I have developed a game in cocos2dx for iOS.Now i integrated banner view for show admob banner ad.
i write code for admob in appController class like that.
NSLog(#"ADMOB");
CGSize winSize = [[CCDirector sharedDirector]winSize];
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2-160,
size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
bannerView_.adUnitID =#"a*******";
bannerView_.delegate=self;
[viewController.view bannerView_];
bannerView_.rootViewController = viewController;
[bannerView_ loadRequest:[GADRequest request]];
GADRequest request = [[GADRequest alloc] init];
request.testing = [NSArray arrayWithObjects:
GAD_SIMULATOR_ID, nil];
[bannerView_ loadRequest:request];
Now I have to hide or remove admob banner ad from cpp class. So I have to call C++ class to objective. C++ call and then call to appcontroller class. and suppose to de remove banner view like that
[bannerView removeFromSuperview];
[bannerView setDelegate:nil];
[bannerView release];
bannerView = nil;
but banner view not removing from that call.
please help me to getting out from this issues.
any help will be appreciated.
Is your ad getting displayed properly,
since this line does nothing:-
[viewController.view bannerView_];
Shouldn't be this like
[viewController.view addSubview:bannerView_];

Cocos2d - retina images not displaying

Simply trying to test retina display. I setup the director like this:
CCDirectorIOS* director = (CCDirectorIOS*)[CCDirector sharedDirector];
director.wantsFullScreenLayout = NO;
director.projection = kCCDirectorProjection2D;
director.animationInterval = 1.0 / 60.0;
director.displayStats = YES;
[director enableRetinaDisplay:YES];
I create two versions of the file in Photoshop - outline-hd.png and outline.png. I color the HD version red so I can tell if it's being displayed.
Display code:
CCSprite *border = [CCSprite spriteWithFile:#"outline.png"];
[self addChild:border];
Yet it is the non-hd image that gets displayed on my iPhone5. Why?
I came across this question while trying to solve the exact same problem in my own project. Had to dig around in the cocos2d source to figure it out. The problem is that the director's enableRetinaDisplay:YES method doesn't work unless the director's view is set. So, it needs to be called after the glView is set up, and you've called setView on the director:
CCGLView *glView = [CCGLView viewWithFrame:aFrame
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
[[CCDirector sharedDirector] setView:glView];
NSLog(#"glView is set, enable retina...");
[[CCDirector sharedDirector] enableRetinaDisplay:YES];
This should fix the problem for you!
May be you forgot:
CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setEnableFallbackSuffixes:NO];
[sharedFileUtils setiPhoneRetinaDisplaySuffix:#"-hd"];

How to enable setAliasTexParameters for CCMenuItem with Cocos2D

I'm having really hard time with my pixelated 2D game. I'm using Cocos2D framework and the menu is driving me insane. I can change the setAliasTexParameters parameter with Sprites, but not for CCMenuItemImage.
I believe it is possible with CCMenuItemSprite, but I red you can't set it straight with that either and I can't seem to even get the menu work with CCMenuItem Sprites. It works nicely with CCMenuItemImages thought.
Here's the blurry buttons:
http://cl.ly/0i1V3Y0e3u2k1t102H2a
For some unknown reason the left arrow button is sharp. I tried to change achor points, positions and paddings by 1 and 0.5 pixels but it didn't help. All the button sizes are even numbers.
This is my code for one of the MenuItems:
CCMenuItemImage *buttonMoveL = [CCMenuItemImage itemFromNormalImage:#"buttonMoveLeft.png" selectedImage:#"buttonMoveLeft2.png" block:^(id sender) {
// Do something
}
}];
And this is the Menu itself:
ActionsMenu = [CCMenu menuWithItems: buttonAttack, buttonMoveL, buttonMoveR, buttonDefend, nil];
ActionsMenu.position = ccp(240, 38);
[ActionsMenu alignItemsHorizontallyWithPadding:8];
All help is really much appreciated!
I got it working by using sprites in the menus. Here's my solution:
CCSprite *spriteDefend1 = [CCSprite spriteWithSpriteFrameName:#"buttonDefend1.png"];
CCSprite *spriteDefend2 = [CCSprite spriteWithSpriteFrameName:#"buttonDefend2.png"];
CCMenuItemImage *buttonDefend = [CCMenuItemSprite itemFromNormalSprite:spriteDefend1 selectedSprite:spriteDefend2 block:^(id sender) {
// Do something cool
}
}];

Cocos2d menu programming *without* images

Good evening all,
I'm trying to code a menu, but I keep getting
Thread 1: Program received signal: "SIGABRT".
My code is minimal at the moment, just trying to get it to work!
#implementation Menu
+(id)scene{
CCScene *scene = [CCScene node];
CCLayer *layer = [Menu node];
[scene addChild:layer];
return scene;
}
-(id)init{
if((self = [super init])){
CCLabelTTF *playLabel = [CCLabelTTF labelWithString:#"Play" fontName:#"Marker Felt" fontSize:40];
CCMenuItemLabel *play = [CCMenuItemLabel itemWithLabel:playLabel target:self selector:#selector(doPlay:)]; //This is where SIGABRT happens//
menu = [CCMenu menuWithItems:play, nil];
[self addChild:menu];
}
return self;
}
-(void)doPlay{
CCLOG(#"doPLay");
}
#end
Any help would be greatly appreciated :)
There seems to be quite little on coding menus without images.
First order of business:
Go to Build Settings
Locate the compiler warning flag "Undeclared Selector"
Set it to YES
This will catch a lot of similar errors, and I really don't understand why this setting isn't turned on by default in all Xcode projects.
Let me explain what your error is, it's easy to overlook without that warning enabled. The selector passed to menu item is this:
#selector(doPlay:)
The selector that's implemented is this:
-(void) doPlay
{
}
They don't match! The menu item is expecting a selector that takes one parameter, as denoted by the : (colon). Change selector to this:
#selector(doPlay)
You'll be fine. Next time, the compiler will warn you about that mishap.

How to transfer a CCSprite from one parent to another?

I have a CCSprite called sprite that is a child of a CCLayer called movingLayer that is itself a child of the current CCLayer running my game logic, so it is self in this case. movingLayer is moving back and forth across the screen in a repeat forever action and sprite is riding along on it. I want to get sprite to "get off" of movingLayer and "get on" to self where it can do some actions of its own.
When I try to do this, I need to tell movingLayer to remove sprite and self to add sprite. This is what I did...
- (void)attack:(id)sender
{
[sprite stopAllActions];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
CGFloat distance = ccpDistance(sprite.position, ccp(sprite.position.x, -sprite.contentSize.height/2));
id goDown = [CCMoveTo actionWithDuration:distance/moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)];
id repeatDown = [CCRepeatForever actionWithAction:[CCSequence actions:[CCMoveTo actionWithDuration:0 position:ccp(sprite.position.x, sprite.contentSize.height/2+self.contentSize.height)], [CCMoveTo actionWithDuration:moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)], nil]];
id attackAction = [CCSequence actions:goDown, repeatDown, nil];
[sprite runAction:attackAction];
}
I think stopAllActions is redundant, but that's not the problem. If I remove sprite from movingLayer before adding it to self I crash for accessing a zombie, and if I try to add it to self first, I crash for trying to add something already added.
Have you tried setting cleanup to NO?
Alternatively, try to retain sprite before you remove it from movingLayer, then release it when you're done with it:
[sprite retain];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
[sprite release];