Is there a Button such as UIButton in COCOS2D? - cocos2d-iphone

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.

Related

Handle Button in CCNode from CCScene

I'm creating a game in cocos2d v3. In the CCScene, I add a CCNode that contains all the components for my HUD. In the CCNode, there are CCButtons added. I want to be able to handle the touch of these buttons in my CCScene. Is that possible? And if so, how is it done elegantly?
Code that I tested this morning and is working:
CCNode header file (Header class):
#property (nonatomic, retain) CCButton *goldButton;
I set up the basics of the gold button in the implementation file of the CCNode (positioning, sprite frame, etc).
CCScene implementation file (PlayScene class):
-(void) setup {
_header = [[Header alloc] init];
[_header.goldButton setTarget:self selector:#selector(goldButtonTapped)];
}
Seems a little weird setting the target of the button inside the scene, but it works. I am wondering of the 'best practice', if you will, for this situation.

Cocos2d CCMenuItem not responding

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.

Is it possible to attach a UISwitch to a certain CCLayer in cocos2d?

I am using a UISwitch in my cocos2d project like so:
//header file
UISwitch *musicCtrl;
//implementation file
musicCtrl = [[UISwitch alloc] initWithFrame:CGRectMake(100, 50, 0, 0)];
musicCtrl.on = YES;
[musicCtrl addTarget:self action:#selector(musicOnOff) forControlEvents:UIControlEventValueChanged];
[[[CCDirector sharedDirector]openGLView] addSubview:musicCtrl];
musicCtrl.transform = CGAffineTransformMakeRotation(M_PI/2);
[musicCtrl release];
I have attached the UISwitch to the openGLView but I need to be able to attach it to a certain CCLayer on the GLView. Is that possible? I can't seem to find a UISwitch alternative for cocos2d.
It's not possible.
You can only add UIKit views to the cocos2d view, or the main window. Either way, the UIKit view will be drawn above all of the cocos2d nodes. You can not add a UIKit view to a specific layer.
Specifically you can't add UIKit views to a cocos2d app in a way where some cocos2d nodes are drawn in front of and other nodes drawn behind the UIKit view. That is impossible.

trying to detect touch ended and touch moved for CCSprite

I have a CCSprite subclass, and initially I had set it up with a
So I had the following code:
-(void)onEnter {
[super onEnter];
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(void)onExit {
[super onExit];
[[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if ([self containsTouch:touch]) {
// do stuff
return YES;
}
return NO;
}
But then I realized that I actually didn't want to use touchBegan, because I want to detect if a sprite has been dragged downward-- So I wanted to use touchMoved and touchEnded instead of touchBegan.
However, when I implement those methods, they are not called...
How can I tell when the sprite's touch ended, and if it was "swiped"?
Enabling multiple touches: In the applicationDidFinishLaunching:application method in your appdelegate, set multiple touches to YES: [glView setMultipleTouchEnabled:YES];
Then in your CCLayer subclass (the class you are working in for detecting touches), in the init method, add self.isTouchEnabled = YES;
Now your multi touch methods should get called.
Swiping: cocos2d does not support gestures out of the box. You will likely have to work yourself. You can start with the apple event handling guide about gestures. The How To Drag and Drop Sprites with Cocos2D totorial at raywenderlich.com hepled me.
In order for the dispatcher to call your methods (moved, ended, cancelled), you have to first claim the touch ie. you will process the events. That is done in ccTouchBegan, when you return YES. After that you will receive the other events.
CCTouchableSprite - my touchable sublcass of CCSprite with Objective-C blocks, you can use touchMoved to detect what you want.

UIButton of previous scene overlaps CCSprite

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)].