activate ccTouchesBegan with a button in cocos2d and make it work just one time - iphone

I want to make something happen with ccTouchesBegan but i want to restrict that action to a button, meaning that the action by the button should trigger ccTouchesBegan but only one time. After the code inside ccTouchesBegan has finished, the interface should go back to normal and wait for the button to be pressed to trigger the action again.
I have made the button trigger ccTouchesBegan when its pressed but the problem is that once the button is pressed, the code inside ccTouchesBegan keeps working and doing the same thing from then on when a touch action on the simulator is done.
This is the code that i have so far.
this is a flag method that i have created so that i know the button has been pressed and i can control the actions on ccTouchesBegan.
- (void) selector{
click = true;
}
this is the button which calls the method selector.
- (void) button{
CCMenuItemImage *touchesMovedButton = [CCMenuItemImage itemFromNormalImage:#"ActionButton-Normal.png"
selectedImage:#"ActionButton-Selected.png"
target:self
selector:#selector(selector)
];
CCMenu *selectorButton = [CCMenu menuWithItems: touchesMovedButton, nil];
selectorButton.position = ccp(64, 64);
[self addChild: selectorButton];
}
and this is the method ccTouchesBegan
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//this part doesn't let the interface start the ccTouchesBegan code until the button has been pressed.
if (click == false) {
return;
}
// the else code starts when the button has been pressed.
else{
CCLOG(#"you have touched the interface!!");
}
}
That's the code, the interface doesn't do any action until the button is pressed but after it's pressed it keeps printing the CCLOG each time i touch the interface. i just want it to do it once and then when i press the button again it should do it just one time again.
does anyone know how to do this? or maybe point out my mistake?

You simply have the logical mistake. Use the Code below in the ccTouchesBegan It would work perfectly as you need...
if (click == false)
{
return;
}
// the else code starts when the button has been pressed.
else if(click == true)
{
CCLOG(#"you have touched the interface!!");
click=false; // You need to make false if you want to make touch single time enabled
}

Related

Detecting Single OR Double Tap

I'm trying to add 2 actions inside an IBAction, one for 1 tap and a different one for a double tap, and I came up with this code:
.h
UITouch * touch;
.m
- (IBAction) button {
BOOL tappedTwice = NO;
if ([touch tapCount] == 2) {
tappedTwice = YES;
// Action
}
else if ([touch tapCount] == 1 && !tappedTwice) {
// Action
} }
No errors, no warnings, but nothing happens when I tap the button... Any ideas???
You cannot capture events like that. The Action is tagged to touchUpInside event so you cannot capture single tap or double tap
Try this link
http://sree.cc/iphone/handling-touche-events-for-uibuttons-in-iphone

how to detect button pressed and released in iphone

how to detect a button is pressed and released because i want to perform two actions
first is auto increment when a button is pressed and hold a...
and to stop the auto increment when a button is released .......
please tell me what button actions are to be used to do this ....
i tried with touchup inside touch down touch up out side but it is not working correctluy can any one please help me how to make it working correctly.....
thank you.
Ok, first you need an interface button. The Button's target action for Touch Down should trigger an action like this:
if (someTimer == nil) {
someTime = [NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:#selector(theDecreasingMethod) userInfo:nil repeats:YES];
}
Then you need another IBAction set to that same button (YES you can set multiple actions to one button) and link that action to Touch Up Inside
Then in the method associated with the touch up inside action:
if (someTimer != nil) {
[someTimer invalidate];
someTimer = nil;
}
Finally, in theDecreasingMethod, you need this
-(void)theDecreasingMethod{
//do what ever you need to incrementally do
//ie. someInt--;
}
I hope this helps; comment if you need anything else

detecting long tap on iPhone

I am working on an iPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of sound.
How should I detect this 6 second tap?
On the other hand the user can also keep on tapping button for 6 seconds & then the same action should fire.
What should I do with multiple taps, how would I know that all the taps fall under the 6 second bracket?
For a six second long press, use a UILongPressGestureRecognizer with its minimumPressDuration property set to 6.
Write your own gesture recognizer (say, LongTappingGestureRecognizer) for continuous tapping for a given period; it shouldn't be too tricky. Give it a property like UILongPressGestureRecognizer's minimumPressDuration (say, minimumTappingDuration) and a property (say, maximumLiftTime) that determines how long a finger can be lifted off before it's not considered to be a long tapping gesture.
When it first receives touchesBegan:withEvent:, record the time.
When it receives touchesEnded:withEvent:, start an NSTimer (the lift timer) that sends the gesture recognizer a cancel message (e.g. cancelRecognition) after maximumLiftTime.
When it receives touchesBegan:withEvent: when there's a start time, cancel the lift timer (if any).
The cancelRecognition will transition to the failed state.
There are various strategies for handling recognizing when the end of the gesture is reached, after minimumTappingDuration. One is to check in both the touchesBegan:withEvent: and touchesEnded:withEvent: handlers if the difference between the current time and the start time is >= minimumTappingDuration. The problem with this is that it will take longer than minimumTappingDuration to recognize the gesture if the user is tapping slowly and hir finger is down when the minimumTappingDuration is reached. Another approach is to start another NSTimer (the recognition timer) when the first touchesBegan:withEvent: is received, one that will cause transition to the recognized state and that is cancelled in cancelRecognition. The tricky thing here is what to do if the finger is lifted when the timer fires. The best approach might be a combination of the two, ignoring the recognition timer if the finger is lifted.
There's more to the details, but that's the gist. Basically, it's a long press recognizer that lets the user lift hir finger off the screen for brief periods. You could potentially use just the tapping recognizer and skip the long press recognizer.
I realize this is quite dated question, however answer should be pretty simple.
In your View controller viewDidLoad:
//create long press gesture recognizer(gestureHandler will be triggered after gesture is detected)
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureHandler:)];
//adjust time interval(floating value CFTimeInterval in seconds)
[longPressGesture setMinimumPressDuration:6.0];
//add gesture to view you want to listen for it(note that if you want whole view to "listen" for gestures you should add gesture to self.view instead)
[self.m_pTable addGestureRecognizer:longPressGesture];
[longPressGesture release];
Then in your gestureHandler:
-(void)gestureHandler:(UISwipeGestureRecognizer *)gesture
{
if(UIGestureRecognizerStateBegan == gesture.state)
{//your code here
/*uncomment this to get which exact row was long pressed
CGPoint location = [gesture locationInView:self.m_pTable];
NSIndexPath *swipedIndexPath = [self.m_pTable indexPathForRowAtPoint:location];*/
}
}
Here is my solution.
- (IBAction) micButtonTouchedDownAction {
self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(micButtonAction:) userInfo:nil repeats:YES];
self.micButtonReleased = FALSE;
}
- (IBAction) micButtonTouchedUpInsideAction {
self.micButtonReleased = TRUE;
}
- (IBAction) micButtonTouchedUpOutsideAction {
self.micButtonReleased = TRUE;
}
- (void) micButtonAction:(NSTimer *)timer {
[self.micButtonTimer invalidate];
self.micButtonTimer = nil;
if(self.micButtonReleased) {
NSLog(#"Tapped");
}
else {
NSLog(#"Touched");
}
}

How to Recognize a Hold Button {iPhone SDK}

Hi i have a Button i want hold this button to write something but i don't know how can i recognize hold button , can you help me ? thank you
TouchDownInside event triggered, start a NStimer.
TouchUpInside event triggered, cancel the timer.
Make the timer call your method to execute if the user holds the button : the timer delay will be the amount of time required to recognize hold.
You can also use UILongPressGestureRecognizer.
In your initialization method (e.g. viewDidLoad), create a gesture recognizer and attach it to your button:
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(myButtonLongPressed:)];
// you can control how many seconds before the gesture is recognized
gesture.minimumPressDuration = 2;
// attach the gesture to your button
[myButton addGestureRecognizer:gesture];
[gesture release];
The event handler myButtonLongPressed: should look like this:
- (void) myButtonLongPressed:(UILongPressGestureRecognizer *)gesture
{
// Button was long pressed, do something
}
Note that UILongPressGestureRecognizer is a continuous event recognizer.
While the user is still holding down the button, myButtonLongPressed: will be called multiple times.
If you just want to handle the first call, you can check the state in myButtonLongPressed::
if (gesture.state == UIGestureRecognizerStateBegan) {
// Button was long pressed, do something
}

Touch in cocos2d?

Cocos2d question??
How do you change the touch type? Look below
image = [MenuItemImage itemFromNormalImage:#"image1.png" selectedImage:#"image2.png" target:self selector:#selector(step1:)];
Menu *menu = [Menu menuWithItems:image, nil];
image.position = cpv( -135, -185);
[self addChild: menu z:2]
step1 is a void defined to do something later in the code. My problem isn't that step1 doesn't work, my problem is that step1 goes when the user touches up inside button. I would like it to work when the user touches down inside button. Thanks for the help!
MenuItem's are not currently capable of responding to 'touch began', and are hard-coded to respond to 'touch end' only.
In Menu.m, starting on line 105, you'll see the ccTouchesBegan declaration.
If you want to modify the current behavior of Menu, you could subclass it like so:
#interface MenuDown: Menu
{
}
#end
#implementation MenuDown
-(BOOL)ccTouchesBegan:(UITouch *)touches withEvent:(UIEvent *)event {
[self ccTouchesBegan:touches withEvent: event];
if(item) { [item unselected]; [item activate]; }
}
#end
This is untested, but basically ... I just grabbed some code from Menu.m in the ccTouchesEnded, and copied it over into an overriden version of ccTouchesBegan for the new MenuDown class.
You would then define your menu as:
MenuDown *menu = [MenuDown menuWithItems: image, nil];
This -should- give you a 'react on touch began' response from the Cocos2D MenuItem's ...
However, this isn't really suggested ... as I can't see any reason why you would want the 'button' to respond to the touch, as opposed to the 'final action' ... as it's written, Menu currently allows the user to press down, then slide off ... to cancel the menu selection action.
Menu/MenuItem's are not intended to be used as 'touch reactive objects' (ie; actual game objects) if that is, by any chance, what your attemping to do.