I am creating a program with cocos2d where I have 2 ccMenuItems each linked to an image: In this case, one is a left arrow, the other is a right arrow. I also have an image in the center of my view that will rotate depending on the arrow pressed.
When I put my finger on one of the two menu items, either left or right, I want the center image to rotate as long as my finger is on the button. That is where I got lost. I tried using the following code:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint p = [touch locationInView:[touch view]];
if (CGRectContainsPoint(leftArrow, p) || CGRectContainsPoint(rightArrow, p)) {
return YES;
}
return NO;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint p = [touch locationInView:[touch view]];
if (CGRectContainsPoint(leftArrow, p)) {
[gun runAction:[CCRepeatForever actionWithAction:[CCRotateTo actionWithDuration:0.2 angle:180]]];
}
if (CGRectContainsPoint(rightArrow, p)) {
[gun runAction:[CCRepeatForever actionWithAction:[CCRotateTo actionWithDuration:0.2 angle:0]]];
}
}
Using this code, when I press on one of the two menu items, the ccTouchBegan method isn’t even called. The method is only called when I touch elsewhere.
How do I work with continuous actions when holding down a ccMenuItem.
Thank you for your help!
As I understand, two methods you posted are in your CCLayer’s subclass.
First of all, if you want to handle touches by yourself, you must remove menu items. CCMenu has much more touch priority and if it handles that the touch hit any menu item, it will swallow it. Thats why you receive touches only when touch is outside menu items.
The second one, what is
leftArrowandrightArrow? Rects of your arrows?And as for me, in such case I can propose you to run update method and change
rotationproperty of your center image in it on every tick. I mean, that rotation value will be 0 if no one arraw was touched and will have positive or negative value due to the arrow you touched.