I'm having some trouble with have a CCMenuItem button work. I've followed all instructions/advice given in previous examples and questions, but nothing seems to work. Whenever I click the button, the image doesn't even change to the new image.
CCMenuItem *startButton = [CCMenuItemImage itemWithNormalImage:#"Start Button.png" selectedImage:#"Start Button Selected.png" target:self selector:#selector(startGame:)];
startButton.position = ccp(0, 0);
CCMenu *menu = [CCMenu menuWithItems:startButton, nil];
menu.position = ccp(winSize.width/2, winSize.height/6);
[self addChild:menu];
menu.touchEnabled = YES;
you probably want to use the
menu.enabled=YES;
that property will control whether touch events are processed. The property you are using (touchEnabled) is a basic property of the CCLayer object (CCMenu extends CCLayer), and that controls whether the layer will receive touch events or not.
Although, the 'enabled' property is set to YES during initialization (its default state). It is possible that by using the other method, you are altering the propagation of touch events. Try first to comment out the line.
Related
I have one CCScene, in which their is About us ICON. When I tap on it, goes to the next CCScene where the information to display.
Now, what happen
When I touch on the back option of the android phone, Game closed.
what I need
when I click the back, It takes me to the First CCscene, where the Icon of ABOUT us present.
What should I do for this ???
Solution : From what I solve
CCDirector.sharedDirector().popScene();
You can use the CCDirector to push scenes on top of running scenes.
Say you have scene1 and scene2 both instances of CCScene (or a subclass of CCScene) then when the user taps the about icon :
[[CCDirector sharedDirector] pushScene:scene2]; // Assuming scene1 is already running
when the user taps the back button :
[[CCDirector sharedDirector] popScene];
EDIT : According to your comment (and in java this time :)) :
public void onBackPressed() {
super.onBackPressed();
if(backPressFlag==1) {
CCDirector.sharedDirector().popScene();
}
}
And of course this will work only if you pushed the About scene with :
CCDirector.sharedDirector().pushScene(AboutScene.scene());
you can do in this way on clicking back button
CCDirector.sharedDirector().getRunningScene().removeAllChildren(true);
Now create new scene
sceneIndex--;
CCScene scene = CCScene.node();
scene.addChild(backAction());
CCDirector.sharedDirector().replaceScene(scene);
Here backAction() is same as in cocos examples
I have a CCMenu with CCMenuItems that I add on a CCLayer. When I click on the CCMenuItems, my CCTouchesBegan doesn't fire up.
How can I call this method also when I touch my menu items?
CCMenu registers as targeted touch delegate and swallows touches on menu items. You can try to create your subclass of CCMenu and override it registerWithTouchDispatcher method like this
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:NO];
}
This should work as you want, but maybe can cause other problems with menu behavior.
The documentation for CCMenuItemImage doesn't actually say what it does.
There are quite a few subclass CCMenuItem.
I've inherited a project that's using it as a button.
CCMenuItem *start;
start = [CCMenuItemImage itemFromNormalImage:[self prefixedImage:#"start button.png"]
selectedImage:[self prefixedImage:#"start button selected.png"]
target:myTarget
selector:#selector(start:)];
It was using the same button for both states.
I modified it to have a different image for the selected state.
I was expecting/hoping that when I touch the item it will be highlighted, and when I release the button it will send my target action (which it does).
(aside: in iOS parlance, i know that highlighted and selected are two different things. But this library does not seem to have that difference.)
So:
Is it intended to use this "menu item" as a button?
When is the selected image of this menu item displayed?
How should I go about making it display as selected?
CCMenuItem is an abstract class from which all of the other menu items inherit so what you did there in the code is technically wrong.
On the other hand you could subclass CCMenuItem to make your own custom class (for example: you can't use a button and a label on it as a menu item, you have to use either the button itself and the label is on top..just for show, or use the label and the button below is...pointless)
Subclassing CCMenuItem and making your own class would fix that problem (i mean you could make a method that would take an image and a string and returns a button)
What you want to do there is this:
CCMenuItemImage *button= [CCMenuItemImage itemFromNormalImage:#"start button.png"
selectedImage:#"start button selected.png"
target:self
selector:#selector(start:)];
CCMenu *start=[CCMenu menuWithItems:button,nil];
start.position=ccp(200,200);
[self addChild:start];
When you put your finger on the menu it will replace the normal image with the selected one, but will only activate of you release it in the boundingbox of the button (aka..you can press on the button, move your finger away from the button and it wont activate).
So in a sence the button is highlighted untill you release your finger, then its selected.
Did that answer your question?
Try this code...
CCMenuItemImage *backbtn = [CCMenuItemImage itemFromNormalImage:#"backbtn.png" selectedImage:#"backbtn_selected.png" target:self selector:#selector(LBback)];
CCMenu *Menu1 = [CCMenu menuWithItems:backbtn,nil];
[Menu1 alignItemsVerticallyWithPadding:15];
Menu1.position = ccp(160, 240);
[self addChild:Menu1];
By the help of this..when you touch on image is shows selected image other wise normal image...:)
and later when your function get called and you want to change its image then you can set like this..
[backbtn setNormalImage:[CCSprite spriteWithFile:#"backbtn_selected.png"]];
The code above is correct.
The image resource for selection was not added to the project, so was not being displayed. It may have output an error message on creation (buried in other output), but did not output error message when tapped.
The silent/safe failure made the user error harder to track down.
I try to search a button such as UIButton in cocos2d..
UIButton can press several buttons at same time.
but CCmenuitem can't..
Is there anyclass in cocos2d such as uibutton?
Extend CCMenu to support multitouch. It was designed to use targeted touches, that is, single touch interactions. With a little elbow grease, it wouldn't be difficult at all to extend it to support multitouch.
CCMenuItems are used instead of UIButton in cocos2d. A single CCMenuItem inside your CCMenu can exactly work like the UIButton.
You can create that in the following way.
//inside your .m file
-(id) init
{
if( (self=[super init] )) {
CCMenuItem *yourMenuItem = [CCMenuItemImage itemFromNormalImage:#"normalImage.png" selectedImage:#"selectedImage.png" target:self selector:#selector(menuItemPressed:)];
yourMenuItem.position = ccp(60, 60);
CCMenu *yourMenu = [CCMenu menuWithItems:yourMenuItem, nil];
yourMenu.position = CGPointZero;
[self addChild:yourMenu];
}
}
There's a good article by Ray Wenderlich on this here.
However, if your main requirement is to add a UIButton only then this discussion might be helpful.
I would also take a look at SneakyInput if you are having trouble setting up your own buttons; it's very easy to use.
I have a UIButton that moves around randomly on the screen. On clicking on the button, a new scene is loaded that, for now, contains a CCSprite. Here is the code:
//in init
CCSprite *a = [CCSprite spriteWithFile:#"a.png"];
[a setPosition:ccp(0,0)];
[self addChild:a];
Pretty straightforward, and it stumps me why after 'replaceScene' the UIButton of theHelloWorldScene.m is still visible, right on top of the Sprite. Where am I going wrong?
I'm assuming since you're adding a UIButton into a cocos2d Scene you're using the openGLView
something like:
[[[CCDirector sharedDirector] openGLView] addSubView:button];
If this is the case, then before you replace your HelloWorldScene you'll need call something similar to
[button removeFromSuperview]
Where button is the name of your UIButton (in both instances).
A suggestion though would be to use a CCMenu with a CCMenuItem on your HelloWorldScene as UIKit objects don't really mesh very well with Cocos2d.
You really aren't giving enough information. The simplest answer however is that if you do not want the button to be visible anymore then remove the child (the button). You can set a tag on the button and then use [layer getChildByTag:(NSInteger)].