1

I have a cocos2d CCLayer class that has both CCSprites as well as UIButtons. I would like the player to be able to touch and drag the UIButtons around on the screen, when the finger is released I want to perform some action.

Here is the snippet of my code that creates UIButton and adds to the layer -

UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(240, 200, 50, 50)];

[[[[CCDirector sharedDirector] openGLView] window] addSubview:button]; 

[button addTarget:self action:@selector(buttonMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];

I am able to move the UIButton inside the cocos2d layer. My question is, how can I find out when the finger is released from the UIButton?

PS/ I read through Ray Wenderlich's tutorial on UIView in cocos2d as well as Learn Cocos2d Game Development by Steffen Itterheim but neither of them cover how to manage touches to the UIKit elements inside a CCLayer or so I think.

Madhav Sbss
  • 325
  • 2
  • 7

1 Answers1

0

did you try using UIControlEventTouchUpInside/UIControlEventTouchUpOutside control events?

Kreiri
  • 7,840
  • 5
  • 30
  • 36
  • I tried the UIControlEventTouchUpInside, UIControlEventTouchUpOutside, UIControlEventTouchDragInside, UIControlEventTouchDragOutside events. None of them trigger the action when the finger is released! It makes sense because UIControlEventTouchUpOutside requires that the finger touch outside the bounds of the control, I don't want that but just releasing the finger from the control should trigger the action! – Madhav Sbss Oct 22 '12 at 13:08
  • Thanks Kreiri, I am not sure what happened but after trying a bunch of things I tried UIControlEventTouchUpInside and that works! I don't understand why but I am glad it works. Only quirk is that the event triggers when I first touch the button and it also gets called when I lift the finger from the button, I just need the latter! – Madhav Sbss Nov 05 '12 at 06:07